Just before we released our private BETA almost 2 weeks ago, Tim requested that I prepare a ‘save’ option so that testers can save the game to play later, but also send the save files to me if there were glitches so that I can see what’s going on. It sounded like an essential feature for our first round of BETA releases.
Unfortunately, Game Maker’s in-built “game_save” function just wouldn’t do. While we used it for Caveman Craig and the Special Edition, there was a lot of additional coding that had to be done to re-instate some data that’s lost during the game save. Namely, data lists (ds_list functions) and instance ID’s.
In CC: Classic and CC: SE, I used a simple ‘switch’ that told the game that a game had just loaded, which tries to ‘guess’ the contents of DS lists, to avoid errors, etc. It was as simple as:
The variable is quickly switched to false after the game saves, so that no other object thinks the game has just loaded when in fact it had just saved. But when the game is loaded, the switch was missed, and so it gives all the objects a chance to ‘work their crap out’ to avoid errors. It was messy, but it worked.
Not so for Caveman Craig 2, which has twists and turns in the code everywhere you look that the game_save function just doesn’t account for. I released the BETA without a save script, and made a note of looking at this as soon as we got back to work.
There is a lot to do, but I think I’ve finalised my concept for a custom save script. The simple part is as follows:
When saving the game, I use this code:
This tells every instance in the entire game to execute its 15th user event. If the object has nothing that it needs to ‘tell’ the save file (like the cursor object or something), I simply leave this event blank. Otherwise, the object writes code to the save file which involves creating a clone of itself, with all the variables set correctly.
The save file is then executed as a script when loading, which creates all the instances and sets their variables to as they were when the game was saved.
Seems pretty simple and solid, right? WELL. The tricky part is in the fact that instance ID’s change between games (from what I could tell, game maker’s “game_save” function does not preserve instance IDs). Caveman & dinosaurs interact a lot, and there are a lot of variables which contain IDs of instances with some significance to the object. Example, if a raptor is eating Craig, Craig has “raptorID” set to the Id of the raptor eating him. The raptor also has “targetID” set to Craig’s ID. In using game_save and game_load, this gets lost and suddenly Craig has no idea who’s eating him, and the raptor has no idea who he’s eating, resulting in way too many errors to try and work around.
So I came up with the idea for an “ID Translation List”. With this concept, there are two DS lists. An A list and a B list. The A list contains every ID of every instance in the game as it was when the game was saved. The B list contains the new ID of each instance when the game is loaded. Because the file is compiled as a script, and executed as a script, it can work this out in 2 parts.
Firstly, in the game save script I created, it creates some of its own lines of code in the save file before getting every instance to add theirs:
file_text_write_string(global.saveFile,"global.idTranslationA = ds_list_create();
global.idTranslationB = ds_list_create();
This creates the 2 lists, but also adds “-4″ (the special term ‘noone’ which does not change) to avoid errors when, for example, Craig is not being eaten by any raptor at all.
Additionally, the script now calls the user events from all instances twice. Once with the variable “idTranslate” set to false, and again with it set to true. This is to place the code into the file in the right order, as follows:
-> create each instance with basic variables
-> add each instance’s old and new ID into the translation list
-> set each instance’s ID variables such as ‘targetID’ or ‘raptorID’ correctly by using the translation list
It looks like this:
_"+string(id)+" = instance_create("+string(x)+","+string(y)+","+string(object_index)+")
//Uses the translation list to set ID variables like targetID
This script is for Craig, by the way.
So far I have only tested interaction with Craig and an Othneilia + his carcass, and it seems to work correctly. There is a LOT of work and testing to be done, but I’m confident this is going to be the most solid way of saving and loading in Caveman Craig 2.