itch.io is community of indie game creators and players

Devlogs

Variable Save System GMS2 [Prototype]

[Prototype] Variable Save System
A browser tool made in HTML5

As promised here's a devlog of the code for the variable save system prototype for Gamemaker Studio 2.There will be another devlog explaining how both save and load scripts work, it will be posted another day.

In-Game Rooms, Sprites, Fonts, Objects, and Scripts

1. Room -> HTMLClickerCounterRm

This room is just where we displayed how many times it clicked to see if it was saving the variables in our browser.

The room is 180 by 180. It has two instance layers. The first Layer has the visual objects, so the menu and button. The second Layer had invisible objects so the object controller and text.

 For the text we used a draw function which is why we have it under the invisible objects so it would show on top of the menu and button when it was on the other instance layer it wouldn't show up for some reason.

2. Sprites -> sBackground, sButtonEmpty, sButtonClickHere, sMenu

These are optional, they exist to use the prototype.  I'm just jotting these down for reference. if you are integrating this into a pre-existing project you won't need these sprites to save your variables.

3. Fonts -> Font1

I added in a custom font just cuz I like it. It's called Monogram. It's used in the prototype to display total clicks, on the top of the menu you can use any font you like.

Monogram font: https://datagoblin.itch.io/monogram

4. Objects -> oButton, oController, oDrawTotalClicks

oButton, oController and oMenuScreen are only relevant to the prototype. if you're integrating this into a new or pre-existing game you will need to have a controller or put it into your current game controller. oMenuScreen has no code in the object.

5. Scripts -> ScrLoadGame, ScrSaveGame 

First I'm going to just jot down the code for the prototype that isn't relevant to the save system but if you're trying to recreate the Prototype step by step you will need it.


Code For in-Game Objects (Prototype Only/Non-Mandatory) 

Objects -> oButton -> Create

// create variable
global.total_clicks = 0;

Objects -> oButton -> Left Pressed

//When player clicks, adds one to total_clicks
if (mouse_check_button_pressed(mb_left))
 {  global.total_clicks++;
}

Objects -> oDrawTotal_Clicks -> Draw

draw_self();
draw_set_colour(c_white);
draw_set_font(Font1);
draw_set_halign(fa_left);
draw_set_valign(fa_top);
draw_text(40,40,"Times Clicked:"+string(global.total_clicks));

Then drag and drop into the room, you may have to move around the object to have it appear where you would like.

Code For in-Game Scripts (Mandatory For Save Function to work) 

Objects -> oController -> Create

// Save Version
global.save_version = 1;
// Clicker Data
global.total_clicks = 0;
// Loads Game
ScrLoadGame();
// Sets Alarm
alarm[0] = room_speed * 10;

Objects -> oController -> Alarm0

//Saves Game Every 10s 
ScrSaveGame(); 
alarm[0] = room_speed * 10;

this works by firing the alarm, saves the run, resetting the alarm then repeating. oController need to be in each room or in the start room and marked persistent to work.

Scripts -> ScrSaveGame

function ScrSaveGame()
{
    var save_data = {
        version : global.save_version,
        total_clicks : global.total_clicks
    };
    var json = json_stringify(save_data);
    var buffer = buffer_create(string_length(json) + 1, buffer_fixed, 1);
    buffer_write(buffer, buffer_string, json);
    buffer_save(buffer, "save.json");
    buffer_delete(buffer);
}

Scripts -> ScrLoadGame

function ScrLoadGame()
{
    var path = "save.json";
    if (!file_exists(path)) return;
    var buffer = buffer_load(path);
    var json = buffer_read(buffer, buffer_string);
    buffer_delete(buffer);
    json = string_trim(json);
    if (string_length(json) <= 0) return;
    if (string_char_at(json, 1) != "{") return;
    var data;
    try
    {
        data = json_parse(json);
    }
    catch (e)
    {
        show_debug_message("SAVE LOAD FAILED: Invalid JSON");
        return;
    }
    if (!is_struct(data)) return;    
    if (!variable_struct_exists(data, "version")) return;
    if (data.version != global.save_version) return;
    global.total_clicks = data.total_clicks;
}


And Yeah, that's all Folks! go make some saveable games.

Download [Prototype] Variable Save System
Leave a comment