[Solved] Need help storing and recalling variables in order

Hi everyone,

I’m currently working on what seemed to be a simple game but I’m having a hard time with this step.

I want to create a game where the player can record a series of input that can be recalled to control a robot.

In other words, I want the player to be able to press ‘‘Up’’, ‘‘Left’’, ‘‘Forward’’, ‘‘Forward’’, ‘‘Right’’, Backward, etc… and then press play to have the robot do all of these movements one after the other.

At this point I can control the robot exactly as I want (pressing left and right will simply rotate the robot in the same square and forward and backward will make him move relative to the direction it is facing). I can control it within boundaries with the keyboard.

Here are a few images showing the basic layout of the game as well as most of the code (you will be able to understand what I’ve done so far I hope).

I just need help finding a way to record and recall let’s say 10 moves that the robot will then do when the play button is pressed.

Thank you very much for your help.

Use an array. Each time a key is pressed, push a value representing the move onto the array. To replay, simply take the first array entry (i.e. the one at index = 0); so that is read it and then delete it.

Also, you can simplify setting the animation by setting it directly to the variable value. And you can set the direction variable by using the mod (aka modulus) operator. It returns the remainder when the first value is divided by the second value. For mod 4, adding 3 is the same as subtracting one while keeping the result positive (because mod(-1, 4) is -1).

Something like:

1 Like

I love the concept. Instead of regular arrows, I would use navigation style icons to make it clear that it’s turning and not moving right.

1 Like

Thank you very much for the answers !

I am getting closer but I really need help understanding how to use the arrays, it looks like I can’t wrap my head around how to access them.

I can store the variables for each directions in the array (as you can see in the picture) but I just can’t find how to “take the first array entry (i.e. the one at index = 0); so that is read it and then delete it.” like you suggested. I posted a few images to show what I’ve done so far, hoping I’m at least on the right track.

Get and remove the first element of an array as follows:


After those actions, the scene variable move will have the first element’s value, and the array will be one element shorter.

Also, you can get the number of elements in an array with VariableChildCount(movement); no need to use a variable to track it.

1 Like

Thank you so much, It works !!!