Windows Updating a Draw text?

L

leonardopixelpro

Guest
hello. I am trying to make the 20 under the word health change to a 19 if you accidentally click on the dog in the middle...
upload_2016-6-25_20-42-52.png

i have gone about it in many ways, however, none of them have worked.
one thing i tried is

HP as a Global variable set to 20

in the dog object i have
left pressed ~> code:
HP -= 1;

and in the draw event for what shows the 20

for (HP < 20) {
draw_text(344, 80, string (HP));
}

i am confused, because i have thoroughly looked through the Gamemaker documentation very thoroughly and not found anything...

:confused:
 

BLang

Member
That draw event makes no sense. You don't want a for loop, you don't even need an if statement in it.

Try something like this:


First, in whatever object you want, probably the one that the hp relates to, initialize the hp (I'm gonna go with the global variable method, because you already started that way:

Code:
globalvar HP;
HP = 20;
Then, in the dog object, your code is fine:
Code:
HP -= 1;
Then finally, in that draw event, just use

Code:
draw_text(344, 80, HP);
You don't have to convert it to a string unless you're gonna combine it with other strings.

Now, on the topic of why that "for" was wrong:
For loops are used when you want to repeat the same code block a certain amount of times in ONE STEP.
So if you have a variable called HP and you want to increase it five times, you would do something like this:

Code:
for (var i = 0; i<5; i++){
    HP+=1;
}
That might look a bit confusing, so here's a rundown:
The for loop takes 3 "arguments", so to speak. They are all separated with semicolons (;).

The first "argument" is actually a variable that you want to initialize, usually a counter variable. That's the i variable in the above code.
I initialized it as var i = 0. The var is there to convey to game maker that this is a temporary variable, i.e. one that will no longer exist after this code is run.

The second "argument" is the expression that gets checked. That's i<5 in our case. The for loop checks the validity of the expression before running the code. Then, if the expression is true, it runs the code (in our case, HP+=1;). Then it checks the validity of the expression again, and if it's true, it runs the code again. This goes on and on until the expression is false. When it's false, it goes on to do the code after the for loop.

The third "argument" is, in our case, increasing the counter variable. Usually, this is what you do with it. i++ just means "increase the variable i by 1 every time the loop runs". In simple terms, i++ is shorthand for i+=1 (not exactly, but that's the essential function it has here).

I'm sorry this got so wordy! Let me know if you need any more help or if I didn't explain something very well.
 

Yal

šŸ§ *penguin noises*
GMC Elder
The for-draw-code shouldn't even compile, for() needs three arguments and you've only put one in there. *scratches head* Do you get errors, or does the game simply not do anything at all?
 
L

leonardopixelpro

Guest
The for-draw-code shouldn't even compile, for() needs three arguments and you've only put one in there. *scratches head* Do you get errors, or does the game simply not do anything at all?
it does nothing
so yeah....
 
L

leonardopixelpro

Guest
That draw event makes no sense. You don't want a for loop, you don't even need an if statement in it.

Try something like this:


First, in whatever object you want, probably the one that the hp relates to, initialize the hp (I'm gonna go with the global variable method, because you already started that way:

Code:
globalvar HP;
HP = 20;
Then, in the dog object, your code is fine:
Code:
HP -= 1;
Then finally, in that draw event, just use

Code:
draw_text(344, 80, HP);
You don't have to convert it to a string unless you're gonna combine it with other strings.

Now, on the topic of why that "for" was wrong:
For loops are used when you want to repeat the same code block a certain amount of times in ONE STEP.
So if you have a variable called HP and you want to increase it five times, you would do something like this:

Code:
for (var i = 0; i<5; i++){
    HP+=1;
}
That might look a bit confusing, so here's a rundown:
The for loop takes 3 "arguments", so to speak. They are all separated with semicolons (;).

The first "argument" is actually a variable that you want to initialize, usually a counter variable. That's the i variable in the above code.
I initialized it as var i = 0. The var is there to convey to game maker that this is a temporary variable, i.e. one that will no longer exist after this code is run.

The second "argument" is the expression that gets checked. That's i<5 in our case. The for loop checks the validity of the expression before running the code. Then, if the expression is true, it runs the code (in our case, HP+=1;). Then it checks the validity of the expression again, and if it's true, it runs the code again. This goes on and on until the expression is false. When it's false, it goes on to do the code after the for loop.

The third "argument" is, in our case, increasing the counter variable. Usually, this is what you do with it. i++ just means "increase the variable i by 1 every time the loop runs". In simple terms, i++ is shorthand for i+=1 (not exactly, but that's the essential function it has here).

I'm sorry this got so wordy! Let me know if you need any more help or if I didn't explain something very well.
Theank you. I Probably didn't declare the variable right.
heres what it looks like now!
upload_2016-6-26_11-5-13.png
 
Last edited by a moderator:
Top