Yesterday I worked on a way to store a replay as a saveable file. The goal is to use the GameJolt API to upload these replays with the scores to the leaderboards. As such, I am especially watchful for ways to reduce the file size of these replays to make uploading them as smooth as possible.
This is how replays work in Mobility: Once the players hits a button, the timer will start running and the playthrough will be recorded. During the level, an array is updated each step/frame with the buttons that register presses. If the player reaches the finish, the game restarts the level and starts the replay, and will repeat all button presses the player made. The game is re-enacting the playthrough.
The big upside of this replay system: very low file sizes. The game records the state of five buttons, so for the length of the replay, that theoretically means five bits (or rounded up to one bit) for each recorded frame is enough. The downside is that the replay could desync, not be identical to the original play anymore, if the game logic during the replay is handled differently somewhere in the game. Even a single desync could quickly ruin the replay. Today I've been hard at work minimizing the possibility of desyncs.
To submit the replay to the GameJolt API, it needs to be formatted as a string, but the replays are recorded in-game as an array. So we need both a script to turn that replay array into a string, as well as translate it back to an array.
This is what the script that turns the array into a string does:
- It checks for the frame which buttons where pressed, then turn that into a single number/real. (It adds one if Left is pressed, two if Right is pressed, four if up, eight if down, 16 if [R].)
- It turns that number into a single string character that represents that number. (so 1 becomes '1', 10 becomes 'A', for example)
- It adds the new character at the end of the current replay string.
It does these steps for as long as the replay array is. If you want to get an array out of the replay string, you just do these steps in reverse: take a character from the string, find out which value that character represents and set that value, and from that value, figure out which buttons should be pressed on this frame.
Next up, I'm going to look into a way to reduce the file size of that string. Right now, the amount of frames in a replay equals the amount of bytes. I'm sure I can get that smaller somehow.
And a small update to the map screen: