Difference between revisions of "Gameplay synchronization"

From MSX Game Library

(Created page with "Synchronization is the process that allows a code loop to run at a constant intended speed. == Why should I synchronize? == Let say you want a monster to move from left to ri...")
 
Line 2: Line 2:
  
 
== Why should I synchronize? ==
 
== Why should I synchronize? ==
Let say you want a monster to move from left to right of the screen. You create a loop where you increment a X coordinate and set a sprite position accordingly. If you test your program, the monster will move at a constant speed… so why the hell should I bother with synchronization?
+
Let's say you want a monster to move from left to right of the screen. You create a loop where you increment a X coordinate and set a sprite position accordingly. If you test your program, the monster will move at a constant speed… So why the hell should I bother with synchronization?
  
Let continue the exemple. Now you add a simple code to handle player movement (with some input reading and basic physics and collision). You launch your program and the first thing you notice is the monster move slower than before. And when you start moving you character it's even worse: Not only does the monster move even slower, but their speed is no longer constant.
+
Let's continue the example: Now you add a simple code to handle player movement (with some input reading and basic physics and collision). You launch your program again and the first thing you notice is the monster moves slower than before. And when you start moving your character it's even worse: Not only does the monster move even slower, but their speed is no longer constant.
  
 
Why?
 
Why?
Line 10: Line 10:
 
The speed is a quantity of movement over time. So, for a constant quantity of movement (eg. +1 pixel to the right) the longer the time, the slower the speed.
 
The speed is a quantity of movement over time. So, for a constant quantity of movement (eg. +1 pixel to the right) the longer the time, the slower the speed.
  
So if your gameplay loop is not synchronized, the time the CPU need to execute it will change the movement speed. And it's where code branching (‘if’ statement) make it even worse because code execution time become not constant (for exemple a part of the code may be executed only if a collision is detected).
+
So if your gameplay loop is not synchronized, the time the CPU needs to execute it will change the movement speed. And it's where code branching (‘if’ statement) makes it even worse because code execution time becomes not constant (for example a part of the code may be executed only if a collision is detected).
  
Synchronize your code insure your gameplay loop always execute at a given  
+
Synchronizing your code ensures your gameplay loop always executes at the same given speed.<sup>#1</sup>
  
 
== How do I synchronize? ==
 
== How do I synchronize? ==
 
...
 
...

Revision as of 13:38, 16 October 2025

Synchronization is the process that allows a code loop to run at a constant intended speed.

Why should I synchronize?

Let's say you want a monster to move from left to right of the screen. You create a loop where you increment a X coordinate and set a sprite position accordingly. If you test your program, the monster will move at a constant speed… So why the hell should I bother with synchronization?

Let's continue the example: Now you add a simple code to handle player movement (with some input reading and basic physics and collision). You launch your program again and the first thing you notice is the monster moves slower than before. And when you start moving your character it's even worse: Not only does the monster move even slower, but their speed is no longer constant.

Why?

The speed is a quantity of movement over time. So, for a constant quantity of movement (eg. +1 pixel to the right) the longer the time, the slower the speed.

So if your gameplay loop is not synchronized, the time the CPU needs to execute it will change the movement speed. And it's where code branching (‘if’ statement) makes it even worse because code execution time becomes not constant (for example a part of the code may be executed only if a collision is detected).

Synchronizing your code ensures your gameplay loop always executes at the same given speed.#1

How do I synchronize?

...