• Hey Guest! Ever feel like entering a Game Jam, but the time limit is always too much pressure? We get it... You lead a hectic life and dedicating 3 whole days to make a game just doesn't work for you! So, why not enter the GMC SLOW JAM? Take your time! Kick back and make your game over 4 months! Interested? Then just click here!

GameMaker Help running condition once only

I

Iceniq

Guest
Hello. I am using this code in the tree's Collision Event to lower its hp by 1 every time the floor(image_index) is 3:

Code:
if (sprite_index = sPlayerChopRT || sprite_index = sPlayerChopLT)
   {
       if (floor(image_index = 3))
           {
               other.hp -= 1;
           }
   }
How can I make this run only once when floor(image_index) hits 3 and then wait until it hits 3 again instead of it running repeatedly until it hits 4?

Thank you for your help!
 

Tthecreator

Your Creator!
What you wrote is weird.
I would have honestly thought your code would have crashes something yet it didn't.
First of all I doesn't make sense to put floor(image_index=3) there because it's not a comparison. Yes, you did say image_index=3 but because you put floor around that again, image_index=3 becomes something else than a comparison. instead use: floor(image_index)=3. That's still a comparison.
Also why use floor()? Floor just rounds any number downwards. However, image_index is already a completely round number, so why use floor?

Then finally to your problem, you could keep a special variable holding the previous state of image_index. Let's say image_index_old. To do this, you want to set the old variable equal to new variable AFTER your code in which you are checking things. Also put that line outside any if statements.

Now you can use that in an if:
If image_index=3 and image_index_old!=3 then{}
This says if image index equals 3 and the previous image index wasn't 3 then do.....

I hope this helps you and I hope you understand
 
E

Elson Ng

Guest
By the way, I'm pretty sure = is an assignment operator and == is the comparison operator you should be using.
 
L

Lysho

Guest
Here's what I would do:

In your Create Event, add the line:

Code:
chopping = false;
In your Step Event, add the line:

Code:
if image_index = 4 then chopping = true;
In your collision event:

Code:
if (sprite_index = sPlayerChopRT || sprite_index = sPlayerChopLT) && chopping = true && image_index = 3
{
    other.hp --;
    chopping = false;
}
 
I

Iceniq

Guest
What you wrote is weird.
I would have honestly thought your code would have crashes something yet it didn't.
First of all I doesn't make sense to put floor(image_index=3) there because it's not a comparison. Yes, you did say image_index=3 but because you put floor around that again, image_index=3 becomes something else than a comparison. instead use: floor(image_index)=3. That's still a comparison.
Also why use floor()? Floor just rounds any number downwards. However, image_index is already a completely round number, so why use floor?

Then finally to your problem, you could keep a special variable holding the previous state of image_index. Let's say image_index_old. To do this, you want to set the old variable equal to new variable AFTER your code in which you are checking things. Also put that line outside any if statements.

Now you can use that in an if:
If image_index=3 and image_index_old!=3 then{}
This says if image index equals 3 and the previous image index wasn't 3 then do.....

I hope this helps you and I hope you understand
Thanks for your reply. The bracket was a typo and it was supposed to be as you wrote it. The reason I use float is because when I drew the image_index to the screen it was not an integer, rather a 2 decimal float. I understand your solution and will give it a shot and let you know. As for the why, I'm completely new to programming and game making in general, learning as I go and right now I'm set on finishing this game just to learn as much as I can.
 
I

Iceniq

Guest
By the way, I'm pretty sure = is an assignment operator and == is the comparison operator you should be using.
It seems this works! I've run into another problem (probably unrelated) but that was definitely one of my mistakes. Thanks for pointing it out!
 
I

Iceniq

Guest
Here's what I would do:

In your Create Event, add the line:

Code:
chopping = false;
In your Step Event, add the line:

Code:
if image_index = 4 then chopping = true;
In your collision event:

Code:
if (sprite_index = sPlayerChopRT || sprite_index = sPlayerChopLT) && chopping = true && image_index = 3
{
    other.hp --;
    chopping = false;
}
That's an elegant solution! I tried something similar but I must have made a mistake somewhere. I'll try it again. Thanks!
 
I

Iceniq

Guest
What you wrote is weird.
I would have honestly thought your code would have crashes something yet it didn't.
First of all I doesn't make sense to put floor(image_index=3) there because it's not a comparison. Yes, you did say image_index=3 but because you put floor around that again, image_index=3 becomes something else than a comparison. instead use: floor(image_index)=3. That's still a comparison.
Also why use floor()? Floor just rounds any number downwards. However, image_index is already a completely round number, so why use floor?

Then finally to your problem, you could keep a special variable holding the previous state of image_index. Let's say image_index_old. To do this, you want to set the old variable equal to new variable AFTER your code in which you are checking things. Also put that line outside any if statements.

Now you can use that in an if:
If image_index=3 and image_index_old!=3 then{}
This says if image index equals 3 and the previous image index wasn't 3 then do.....

I hope this helps you and I hope you understand
This worked beautifully! But I still need to use the floor(image_index) as it seems image_index is not returning whole numbers. I've drawn image_index onto the screen to see its values and it changes in 0.01 increments. Is that not supposed to be the case?
 
Last edited by a moderator:

Hysler

Member
No it's fine, it would only be displaying a whole number if your animation speed was incremented by a whole number.
 
Top