GML Rhythm-Based Game, Lag

S

Sunshine Games

Guest
Hi! I am making a rhythm-based game, and essentially, if the game lags the music does not, so it becomes out of sync with the game and things do not happen on-beat. How do I fix this? Thank you!
 

Binsk

Member
That extension is completely unrelated, @Edwin. Also, it's usually better to explain a solution than to just plop in an extension when possible.

@Sunshine Games,
This will need to be a matter of design on your part. The step event gets executed every frame, as I'm sure you are aware. When the framerate is solid and at the value you specify in the editor you can assume that there is the same amount of time delay between each frame. As such, you generally set movement speed and update rates to values assuming that this "time between frames" is always constant. This is why, when you have lag, things slow down. You designed your game to expect a certain amount of time between each frame and now suddenly the time is getting longer as the game lags.

The answer to this is to use what is called "delta timing". Instead of expecting this time to be constant you use what is called "delta_time" to modify the speed at which things move in the game in order to compensate for lag. Take a look at the delta_time variable here. It essentially reports the amount of actual time that occurred between frames. If you know how much time your game is expecting (e.g., a 60fps game has roughly 16.6 milliseconds between each frame) and how much actually occurred (for example, a lagging game might report 22 milliseconds) then you can compensate.

Let's say your note is supposed to move down the screen at 5 pixels a step. You are expecting 5 pixels per 16.6ms. If the game lags over the next step and takes 22ms to compute, you will need to compensate and instead move the note 22 / 16.6 * 5 = 6.63 pixels for that one step. This is what delta timing is and is also what you will need to be doing. It will require you to rejig a bit of your game logic to use instead of relying on the framerate but it shouldn't be too hard.

EDIT: Rewrote my answer for better clarity.
 
Last edited:
E

Edwin

Guest
That extension is completely unrelated, @Edwin. Also, it's usually better to explain a solution than to just plop in an extension when possible.

@Sunshine Games,
This will need to be a matter of design on your part. The step event gets executed every frame, as I'm sure you are aware. When the framerate is solid and at the value you specify in the editor you can assume that there is the same amount of time delay between each frame. As such, you generally set movement speed and update rates to values assuming that this "time between frames" is always constant. This is why, when you have lag, things slow down. You designed your game to expect a certain amount of time between each frame and now suddenly the time is getting longer as the game lags.

The answer to this is to use what is called "delta timing". Instead of expecting this time to be constant you use what is called "delta_time" to modify the speed at which things move in the game in order to compensate for lag. Take a look at the delta_time variable here. It essentially reports the amount of actual time that occurred between frames. If you know how much time your game is expecting (e.g., a 60fps game has roughly 16.6 milliseconds between each frame) and how much actually occurred (for example, a lagging game might report 22 milliseconds) then you can compensate.

Let's say your note is supposed to move down the screen at 5 pixels a step. You are expecting 5 pixels per 16.6ms. If the game lags over the next step and takes 22ms to compute, you will need to compensate and instead move the note 22 / 16.6 * 5 = 6.63 pixels for that one step. This is what delta timing is and is also what you will need to be doing. It will require you to rejig a bit of your game logic to use instead of relying on the framerate but it shouldn't be too hard.

EDIT: Rewrote my answer for better clarity.
There is no way to stop the music if game freezes.
 

Binsk

Member
There is no way to stop the music if game freezes.
He wasn't asking about the game freezing, he was asking about it lagging. That is to say, how to handle the gamer's computer not able to keep a solid framerate. If the game slows down because the computer can't keep up but the audio keeps playing at regular speed, that completely breaks a rhythm game if you don't compensate for the lag. You wouldn't want to slow down / stop the music in case of lag either because that has detrimental effects on the enjoyment of the game.

An extension that prevents lockup when the window is moving is completely unrelated.
 
E

Edwin

Guest
He wasn't asking about the game freezing, he was asking about it lagging. That is to say, how to handle the gamer's computer not able to keep a solid framerate. If the game slows down because the computer can't keep up but the audio keeps playing at regular speed, that completely breaks a rhythm game if you don't compensate for the lag. You wouldn't want to slow down / stop the music in case of lag either because that has detrimental effects on the enjoyment of the game.

An extension that prevents lockup when the window is moving is completely unrelated.
If I understand correctly lagging is same as you holding the window with left mouse — then game freezes, all functions except sounds are paused.
 
Last edited:

Alice

Darts addict
Forum Staff
Moderator
@Sunshine Games Another tool aside from delta time could be a nifty audio_sound_get_track_position function, which retrieves the soundtrack position in milliseconds (with fractionasl part, so you can make it as precise as you need).

You might also want to check out this thread, as it apparently discusses the same problem.
 
S

Sunshine Games

Guest
@Alice
@Binsk

Thank you so much! I will look in to both of these options and see if it works. I'll report back when I have!
 
Top