This code makes no sense.
Posted by Ded_doctor@reddit | learnprogramming | View on Reddit | 20 comments
In the code (below) i’m learning from the free GDscript tutorial from GDquest, makes no sense. How does it know the perameter is -50 from the health variable? The script I out below subtracts 50 from the total 100, but how does this even work if there’s no “50” in the code. Can someone with GDscript experience please explain this.
var health = 100
func take_damage(amount): health -= amount
Monk481@reddit
The "take damage" function reduces the health variable by 50 when called. ..
CeReal_DoughBoy@reddit
The problem is the tutorial not only doesn't show the function you're calling (so you would never know that amount is predefined), but further instructs you that "robot takes right amount of damage" as the first check. In goals it describes "the robot should start with 100 health and take 50 damage".
The wording encourages you to write a line subtracting 50 from health, and like I mentioned previously, you have absolutely no way of knowing what the function is actually doing. What you're reading in OPs post is exactly the information they have available to them.
The issue here is not OP being thick, it's a fundamental issue with the wording of the tutorial. I believe they refrain from showing you the actual functions as to not overwhelm beginners, but I can absolutely see why they would get confused. Literally just take the aforementioned things out of "goals" and "checks" and I doubt anyone would get confused, because that section of the tutorial literally doesn't want you to do that, despite clearly asking you to. Either that or add to the interface a way to see the actual function you're calling.
Independent-Disk-390@reddit
lol
TJATAW@reddit
Here is what they are working on:
https://gdquest.github.io/learn-gdscript/#course/lesson-14-multiplying/practice-CQiGjB7t.tres
@Ded_doctor what is happening when you hit run, the system is calling the take_damage function and putting in 10 as the amount.
Think of it this way, somewhere is a function that determines if the robot gets hit.
If the robot is hit it calls the take_damage function, and puts the amount of damage in as the amount. So, if the weapon does 10 points of damage it calls take_damage(10). It is all part of a series of things.
Something like:
function fire_gun():
damage = 25
try_to_hit(damage)
function try_to_hit(damage):
odds = random()
if odds > 50:
take_damage(damage)
else:
print("Miss")
func take_damage(amount):
health -= amount
if health < 0:
health = 0
print("You died")
firing the gun > try to hit > do damage with the damage getting passed from each function to the next.
You could then have different weapons, like a knife:
function throw_knife():
damage = 3
try_to_hit(damage)
function stab_knife():
damage = 5
try_to_hit(damage)
Both of those then go find the try_to_hit function, passing along the damage from that weapon, and if it hits they call on the take_damage function, passing along the damage number.
LinkleEnjoyer@reddit
I have had a quick look at the GDQuest tutorial, and found the part you're looking at, 9. Adding and Subtracting -> 1. Damaging the Robot:
I can see how that last line would be confusing. The code that you write doesn't "know" to subtract 50. Another piece of code that you cannot see right now is calling the function:
take_damage(50)
If you were programming a game, you could call that function yourself with any value you'd like:
take_damage(19)
take_damage(50000)
take_damage(-100) # Passing a negative number here would actually heal the robot, because subtracting a negative number from another number is the same as adding it instead.
Ded_doctor@reddit (OP)
Ohh that clears things up, thanks!
AntitheistMarxist@reddit
You do not need GDScript experience. In the example, amount = 50. The goal is only to understand that you are taking away an amount from health. I looked at the lesson and this is what it says:
It is saying that your focus is on understanding the effect of damage on the health. The value of 50 would be defined in a different part of the code. In reality, your code should not work without the rest of the program.
This is all it gives you:
var health = 100
func take_damage(amount):
No matter what you type before the function for amount, it uses 50.
var health = 100
var amount = 25
func take_damage(amount):
health -= amount
It still returns 50 health.
Ded_doctor@reddit (OP)
Ooohh that makes a lot of sense, thank you!
AntitheistMarxist@reddit
No problem!
grantrules@reddit
Whatever is calling take_damage() is passing it a parameter (amount) with the value of 50
Ded_doctor@reddit (OP)
So the function take damage just knows it’s 50? How does it do that? Is 50 the base damage unless specified?
AionAlgos@reddit
Functions are "called" (aka "invoked", "executed", "ran") from other locations in code; this other location is passing in the value for the
amount
variable it takes as an argument / parameter.For example:
print
functions often take a string, and would be called like:print("hello world")
... Theprint
function is passed some representation of the string"hello world"
as an 'argument'. Some hypothetical definition would look something like...So, the 'value' of
message
is whatever the caller provides:print("like this")
Ded_doctor@reddit (OP)
So because the tutorial said to just put amount for the 50 point of damage, it’s telling the computer that amount = 50?
AionAlgos@reddit
Well, the value depends on the caller; where the function is being invoked from. Perhaps this 50 is being defined somewhere else? If this is a game engine or something, it may be a bit difficult to track down where exactly that '50' is coming from... It sounds the the tutorial may be doing a bad job explaining, or maybe this just isn't the primary focus of the tutorial you're watching and it's covered elsewhere...
Ded_doctor@reddit (OP)
I think I figured it out. The app i’m using is a beginner tutorial so I think it’s using “amount” in sub for “50”
Clueless_Otter@reddit
It doesn't "just know," no. There must be more code besides what you've pasted here. Something like
Ded_doctor@reddit (OP)
No that was the code word for word, it makes zero sense. Maybe because it was a tutorial that’s why but idek. Will the function need the 50 in the parentheses when I actually make it to godot?
Clueless_Otter@reddit
If you link the whole tutorial, someone might be able to give a better explanation.
If you want the entity to take 50 damage, then yes you have to write take_damage(50). If you want it to take 25 damage, then take_damage(25), etc.
spellenspelen@reddit
It doesn't magically know. It has to be specified when calling the function. The code you showed does not include the call to the function itself.
Psudo code:
take_damage(50);
grantrules@reddit
No, it has no idea what the value is. It's whatever is passed to it. Like someone gives you the instructions: multiply two numbers that I give you, you know what to do regardless of what numbers I give you. I tell you 2, you say 4. I tell you 10, you say 100.