Legacy GM Turn back time

D

DekuNut

Guest
Before I ask for help, I'd like to say that I do not want any finished code. Simply push me in the right direction, or throw me a link to a tutorial if you know of one. Pseudo-code is fine as well. I just want to be able to learn it, that way if I come across a similar situation in the future, I can tackle it on my own.

I want to be able to push a button, and have the player, enemies, and all objects revert to positions and animations that they were in - for example - 5 seconds ago. Problem is I have no idea how to go about this. It is easy enough to store the position of an object, but I can't seem to wrap my head around the concept of storing the position of an object every step, 5 seconds ago. Any ideas?

I'm working on a 2d mystery/action game. Player is a detective in a town infested with gangs, and he only real weapon besides his gun (limited ammo) and his fists is his mind. He can "predict" what enemies are going to do. That is the back (short) story if anyone was curious or needed more of an example.
 
A

Aura

Guest
A 2D array would suffice. At regular intervals, keep on adding your position to it, deleting the first set, and moving all the sets one index up.

Code:
pos[50, 0] = x;
pos[50, 1] = y;
for (var i = 1; i <= 49; i++)
{
   pos[i - 1, 0] = pos[i, 0];
   pos[i - 1, 1] = pos[i, 1];
}
This way the array holds the position of the instance of 50 steps, but you can make it efficient by storing position after a short interval, say once every 20 steps.
 

GMWolf

aka fel666
Aura's solution works but is rther ineficient as you hve to loopt hrought the whole array every step.

I would use a buffer. By using wrapping buffer you can ensure that you will overwrite the oldest value everytime.
The other nic thing about a wrapping buffer is that you do not have to change the seek position, the next galue you read will always be the value n seconds ago.

To do this, you first have to decide how many variables to store, and calculate how many bytes that would take.
Then create a buffer that is exactly that number of bytes times the number of step you want to go back.
Fill the array with the starting position of the object.
Every step, add each variable to the buffer. You do not need an undex as buffers will handle this for you.
When you want to turn back time, just read from the buffer (again, no need to change the seek position as the next value will be the oldest value).
 

GMWolf

aka fel666
How big is a value in gml?
They are 64 bits (i think) but when dealing with buffers, you can choose to use 8, 16, 32 or 64 bit values.
Say you value will always be an integer between 0 and 255, then a u8 is enough to store the value.
If your value is a room position, f32 or u32 is enough. If your value needs high precision, f64 is best.
 
G

graviax

Guest
can you set the size of a value outside of a buffer? like in C?

(char, short ect...)
 

GMWolf

aka fel666
can you set the size of a value outside of a buffer? like in C?

(char, short ect...)
No, i think gamemaker will stop you from doing that. (Buffer overflows are a serious safety threat).
You could always write a dll that does it if you really needed it though (though i can only think of malicious reasons...)
 
Top