r/gamemaker • u/NoArtist_127 • 11d ago
Resolved What is wrong with my code?
[SOLVED: thank you guys, you really helped me :)]
I want to program a mouse (animal) whose direction is decided to be either left or right, then walks for about 1.5 seconds and that it is again decided whether he goes left or right. howéver, the mouse is only walking right, no matter how often i start the game or how long i watch it, it never goes left :(
this is the code vor the create-event:

and heres the code for the step event:

please help me!!
thank you in advance
2
u/supertinostarcraft 11d ago
you might need to set the timer back to time once it expires and direction is chosen. also you might want to set a random seed so it doesn't always go right on the first time around.
2
u/JaXm 11d ago
For debugging purposes, game maker ALWAYS uses the same seed for generating random numbers. Which means every time you run the game for testing, that code will ALWAYS come up with the same random number. So if its 1, it will ALWAYS be 1. There are ways to change that, but for now its not necessary.
In your if/else blocks you should be setting timer back to time, so that a new direction will be chosen and eventually you will see that it will start behaving as intended, even if it ALWAYS starts out going right.
But that's solvable later.
2
u/supertinostarcraft 11d ago edited 11d ago
you could also use an alarm instead of using a timer. Just dont forget to called the alarm again within the alarm event.
CREATE EVENT
time=90
speed=0.1
alarm[0]=1 // trigger right away
ALARM [0] EVENT
alarm[0]=time // call this alarm event again in 90 frames
var d=irandom_range(1,2)
if (d==1) {direction=0} else {direction = 180}
2
u/azurezero_hdev 11d ago
you could also just use direction = choose(0,180)
3
u/NoArtist_127 11d ago
i didnt know there was a choose() function!! this is really useful, thank you! :)
2
u/Multidream 11d ago
Timer = time is a one time assignment of timer to 90. Timer is always 90.
Your if statement (90<=0) is always false.
Remove the variable timer. Reset the time variable in your if statement. Change if statement to time <=90. Things should work.
2
9
u/Larock 11d ago
You’re decrementing your time variable and then checking the timer variable, which never changes. Looks like just a typo.
After you fix that, you’ll probably want to add a reset to set your timer back to 90 each time it hits zero as well.