GMS 2.3+ Random integer in Step Event, without changing


Cheers :

I have this need, I have a character that flies and an enemy that tries to knock him down with his abilities, when my flying character falls, he will be on the ground for a few seconds until he can take flight again, and a variable called "Down" is activated. "that changes from false to true. The idea is that with another variable called "Change_anim" a random integer is chosen, so that the enemy has to choose between 2 attack animations when my character falls. The problem is that I try to do it from a Step Event when the "Down" variable is activated but with this it does not work for me because the Step Event will change the variable with each frame in the game.

if flying_character.Down == true
randomize ()
Change_anim = choose (1,2)

// I use Spine, but it is almost the same as using sprite_index
if Change_anim == 1 {
if skeleton_animation_get ()! = "Attack1" {
skeleton_animation_set ("Attack1")}

if Change_anim == 2 {
if skeleton_animation_get ()! = "Attack2" {
skeleton_animation_set ("Attack2")}

How do I make the "Changeanim" variable stay at a single number until "Down" is false again.


Firstly, randomise is something you call once at the start of your game, and there is no need to call it again.

When your character lands it sets down to true. If you can make this happen once only instead of checking every step, you can have the enemy set down to false after it chooses a new attack, so it doesn't keep choosing a new attack. Having the player check once depends on how you have it set up though.


Just a thought. If that is in the step event wouldn't you have to set flying_character.Down == false first thing? Unless you have something later in the step to make it true before the next step.
Last edited:
I'm having a similar problem, instantiating a struct by passing a random value into the create event that seems to not work correctly despite having inserted the randomize () function at the beginning of the create event...


actor = new Actor(irandom(7)*45); //Random direction
Then, by running the game several times, the value is never randomized but always returns 315 as a result, so the problem is that it returns the multiplication of the number without randomizing it.
Any help? Thank you all.


A quick fix for the OP's question:

In the CREATE event:
randomize(); // You're better off calling this only once, e.g. in a GAME START event
Change_anim = -1;
In the STEP event:
if (flying_character.Down == true && Change_anim == -1)
    Change_anim = choose(1, 2);

    // do the other stuff
I generally like to declare my variables as -1 (or, in other words: have a unique value for "not set"). This way, you can easily check whether or not they've been set before.

PS: please do yourself a favor and try to get used to proper syntax, like ending lines on semicolons where applicable. GML is pretty much the only language I know that's so incredibly forgiving; but if you ever transition to something else, let's say C++/C# or whatever, you'll quickly run into all sorts of problems with this.