My friend and I have been working on a replay system for our game for quite some time.The replay system works like this:
First,we initialize the replay system and we create an array containing the key inputs used by the game (as well as other variables used by the replay system's scripts):
// Map which key does what keys[0] = objParentPlayer.keyLeft; keys[1] = objParentPlayer.keyRight; keys[2] = objParentPlayer.keyUp; keys[3] = objParentPlayer.keyDown; keys[4] = objParentPlayer.keyFire; keys[5] = objParentPlayer.keyMelee; keys[6] = objParentPlayer.keyShield; keys[7] = objParentPlayer.keyFocus; frameNum = 0; //Which frame we are on frameKeys = 0; //Which keycombination we currently have at a frame record = true; //If it is recording playBack = false; //If it is replaying
Then we use another script (which,apparently,works just fine) to record the player actions and store it to an INI file:
//First a clean array to record the inputs. Each element represents a key.
for (i = 0; i <= 7; i += 1)
{
individualKeys[i] = 0;
}
//Record inputs from each key. 1 for push, 2 for release. Save in array each key state.
for (i = 0; i <= 7; i += 1)
{
a = 0;
switch (true)
{
case keyboard_check_pressed(keys[i]) == true:
a = 1 break;
case keyboard_check_released(keys[i]) == true:
a = 2 break;
}
individualKeys[i] = a * power(10,7-i);
}
//Now, translate the array to a number we can save to the ini-file
frameKeys = 0;
for (i = 0; i <= 7; i += 1)
{
frameKeys += individualKeys[i];
}
//Save the current key-state for current frame to the ini-file. Add extra zeroes if not 8 digit
// to make translation work properly
if frameKeys != 0
{
frameKeys = string(frameKeys);
while string_length(frameKeys)<8
{
frameKeys = "0"+frameKeys;
}
ini_open('LastReplay');
ini_key_delete('PLAYER ACTIONS','Frame ' + string(frameNum+1));
ini_write_string('PLAYER ACTIONS','Frame ' + string(frameNum+1),frameKeys);
ini_close();
}
//Go to next frame
frameNum += 1It will output something like this:
[PLAYER ACTIONS] Frame 30=00100000 Frame 40=00200000 Frame 44=01000000 Frame 58=00010000 Frame 59=02000000 Frame 74=10000000 Frame 75=00020000 Frame 89=00100000 ...
Each digit represents a key : the first digit is the left arrow,the second the right arrow,the third the up arrow and so on (see the array I've showed you before) and 1 means a key is pressed,2 a key is released and 0 means no action;for example at frame 30 the up arrow was pressed while at frame 40 it was released.
Finally,we have a script that reads the INI file and simulates keystrokes accordingly to the INI file that we generated before:
//Read the key-combination for the current frame
ini_open('LastReplay');
keyCombination = string(ini_read_real('PLAYER ACTIONS','Frame ' + string(frameNum+1),frameKeys));
ini_close();
//Each digit represent a key state. Translate the digits to keyinputs.
for (i = 0; i < 8; i += 1){
keyToSimulate = string_char_at(string(keyCombination),i+1);
key = keys[i];
if keyToSimulate == "1"
keyboard_key_press(key)
if keyToSimulate == "2"
keyboard_key_release(key)
}
//Go to next frame
frameNum += 1Howerer,for some reason,the playback script simply won't work and it will treat every key as key 0 (aka the left arrow).We've been trying to fix this for ages with no luck.
Any ideas?



This topic is locked







