GameMaker: Studio

GameMaker: Studio

View Stats:
patben8 Jun 4, 2014 @ 1:06pm
Game Maker: Adding a wait in code.
Hi,
I would like to know how to add a wait in code, for example, if I want to run a line of code then wait 0.1 seconds, this is basic in 90% of all programing languages yet GM requires alarms. Please help! Thanks!
< >
Showing 1-12 of 12 comments
Sera Jun 4, 2014 @ 1:20pm 
GM originally had a wait() function that stalled the entire program for a given period of time, but when converting to Studio and its multiplatform intention it was discovered this made a lot of OSs incredibly angry, especially with mobile. You can still accomplish similar things manually by using timers and a boolean or two, only allowing objects to be active and respond to things so long as a global allows them to do so.

The thing with waits are that they do stall everything, and a lot of programming languages provide for that, but I don't hear much of a wait() being called in anything beyond a console application, because outside of multithreading you're stopping everything and that's generally not a wise decision. It's different in a command prompt situation where everything is conveyed as lines of text, but once you move past that it's generally wise to use timer variables or something like GM's alarms (which are the same concept, just automated to a fair degree for you). Generally, if you feel the need to call wait(), there's probably a better way to do what you're trying to do that won't tick off your entire program update cycle.
patben8 Jun 4, 2014 @ 1:43pm 
Ok so here's the situation: Im trying to get a object to rotate 90º in 3 seconds, this is what I've got so far:
repeat(90){
direction += 1
}
I need a 0.3 second wait right after the //direction +=1. How could I do that?
Thanks!
Sera Jun 4, 2014 @ 2:02pm 
You don't need a wait.

Objects update every step on their own. If you have a Step event, the code within it executes every frame.

The default room speed is 30, so Step would run about 30 times a second. That said, room speed can be changed, so let's make this a bit adaptable.

{ direction += 1/room_speed * 30; }

1/room_speed will get us the percentage of a second that has theoretically passed, while the * 30 multiplies that percentage by our target rotation amount per second. Over the course of three seconds, that will turn 90 degrees, though it will also keep turning past that unless we put some sort of check there to make sure it still needs to turn.

As an aside, direction influences an object's motion, but not so much it's visual orientation. image_angle will rotate the object's sprite around its origin point.
Last edited by Sera; Jun 4, 2014 @ 2:02pm
patben8 Jun 4, 2014 @ 2:10pm 
Thanks, how ever Im trying to make a level that will gradually turn 90º when the [ENTER] key is pressed.
Sera Jun 4, 2014 @ 2:23pm 
That's a slight bit trickier to manage. :s You can try rotating the viewport and gravity, or you could have the level itself be one large object that turns. The former case ends up requiring some interesting collision handling if there's any sort of gravity involved, while the latter raises the issue of keeping the player's position aligned when the stage moves. Either way, my advice past that becomes largely speculative, though I think I at least get the gist behind doing either.
patben8 Jun 4, 2014 @ 2:29pm 
Thanks, I guess I'll figure it out.
Daynar Jun 7, 2014 @ 3:02pm 
You could use alarms or add a fraction of what ur currently adding each step event
TheMathGeek_314 Sep 2, 2016 @ 10:21am 
I know this is two years after the initial post, but I haven't found what I'm looking for and this is relevant. However, I can't do a fraction of the movement required. I have 14 different frames in one sprite and I want it to take longer than 1/30th of a second to move
Sera Sep 2, 2016 @ 7:31pm 
Animation speeds can be controlled with the image_speed variable native to each object instance, where the value is how many frames of animation are processed per step (ie. a speed of 0.5 will make a 14 frame animation occur over 28 steps in-game).

Otherwise, I'm not sure what you mean.
sitebender Sep 3, 2016 @ 4:04am 
Someone recently asked this same question. Its still in the top threads. There are solutions there.
Whatever Oct 23, 2018 @ 1:15pm 
Is there any way to make one, singular sprite wait, on its own, as the rest of the game functions? I'm having trouble coding collision on my first game.
The Winter Bud Oct 23, 2018 @ 6:19pm 
Originally posted by cheesedisease873:
Is there any way to make one, singular sprite wait, on its own, as the rest of the game functions? I'm having trouble coding collision on my first game.
Please, in the future, try not to necro another's post. Just make a new post explaining your problem.

image_index[docs.yoyogames.com] : this is the variable that tells which frame of the sprite to draw
image_speed[docs.yoyogames.com] : this is the variable that says how fast it will cycle through the frames

with this knowledge you can Stop the animation, and say which frame of the sprite you want to draw.
EXAMPLE:
image_index=0; // first frame of sprite image_speed=0; // do not animate through frames

EDIT: if you want to start up the animation again, just set image_speed to a positive value.
Last edited by The Winter Bud; Oct 23, 2018 @ 6:22pm
< >
Showing 1-12 of 12 comments
Per page: 1530 50

Date Posted: Jun 4, 2014 @ 1:06pm
Posts: 12