GameMaker Need Help With Data Structures [SOLVED]

ZyKro

Member
I am trying to do a save system for a game I am working on and I'm not sure how to go about it. I heard data structures are very useful, but I am having trouble trying to make it work.

I am using ds maps, but I don't know if it is the best way to go for me for what I am saving. I want to save information about a level, mainly when the player completes it, the next level is unlocked. I want to save data where it can't be modified easily. There's only 2 things I want to save at the moment, but it should get larger.

The level information doesn't seem to be saving because when I try to go to level 2, it is locked still, but I am able to go to it after completing stage 1 when I continue to next level (without exiting the room).

Checker Object (persistent)
Code:
/// obj_checker create
save_data = ds_map_create();
filename = "Savedata.sav";

ds_map_add(save_data, "stagenum2", false);
ds_map_add(save_data, "stagenum3", false);


if (file_exists(filename)) {
    ds_map_destroy(save_data);
    save_data = ds_map_secure_load(filename);
}
Stage Controller Object
Code:
///Create Event
stagenum[0] = true;
stagenum[1] = false;
stagenum[2] = false;

var _stage2, _stage3;
_stage2 = ds_map_find_value(obj_checker.save_data, "stagenum2");
_stage3 = ds_map_find_value(obj_checker.save_data, "stagenum3");
scr_check_undefined(_stage2, stagenum[1]);
scr_check_undefined(_stage2, stagenum[2]);
Player Object Step Event (partial)
Code:
///Step
switch (global.stage) {
    case 1:
        if (global.enemy_count >= 25)  {
            ds_map_replace(obj_checker.save_data, "stagenum2", true);
            ds_map_secure_save(obj_checker.save_data, obj_checker.filename);
            instance_change(obj_player_end, true);
        }
        break;
    case 2:
        if (global.enemy_count >= 50)  {
            ds_map_replace(obj_checker.save_data, "stagenum3", true);
            ds_map_secure_save(obj_checker.save_data, obj_checker.filename);
            instance_change(obj_player_end, true);
        }
        break;
    case 3:
        if (global.enemy_count >= 75)  {
            //do something
            instance_change(obj_player_end, true);
        }
        break;
}
global.stage shouldn't be the problem because it was created in the create event of another object.
I appreciate any help.
 
K

kevins_office

Guest
In the object_checker create event, you preset 2 and 3 to false. Shouldn't you also set 1 as true?
Or why do you only have stagenum2 and 3, but then have stagenum[0] [1] and [2]?
The code is not clear what statenum[] even does, what purpose does it serve?

Don't understand the logic happening in the stage controller code, however...
Code:
scr_check_undefined(_stage2, stagenum[1]);
scr_check_undefined(_stage2, stagenum[2]);  //  <-- _stage3 ???
What does the undefined script do? Why are you calling it?
What purpose does changing the instance to player_end serve?
Why have a switch case on global.stage? None of your code shows it being set or changed, so i can't tell if that could be one of the problems.
From what i can see case 2 and 3 will never be triggered so 2 and 3 will never be saved. I personally would not combine a switch and if() statements.

Can't really offer more help because i dont understand what its doing that is wrong, and what is it you expect it to do instead.
 

ZyKro

Member
In the object_checker create event, you preset 2 and 3 to false. Shouldn't you also set 1 as true?
Or why do you only have stagenum2 and 3, but then have stagenum[0] [1] and [2]?
The code is not clear what statenum[] even does, what purpose does it serve?

Don't understand the logic happening in the stage controller code, however...
Code:
scr_check_undefined(_stage2, stagenum[1]);
scr_check_undefined(_stage2, stagenum[2]);  //  <-- _stage3 ???
What does the undefined script do? Why are you calling it?
What purpose does changing the instance to player_end serve?
Why have a switch case on global.stage? None of your code shows it being set or changed, so i can't tell if that could be one of the problems.
From what i can see case 2 and 3 will never be triggered so 2 and 3 will never be saved. I personally would not combine a switch and if() statements.

Can't really offer more help because i dont understand what its doing that is wrong, and what is it you expect it to do instead.
I probably should have set a thing for stage 1, but I thought it wasn't needed since it will always be accessible. Stagenum2 and stagenum3 are the keys for saving stagenum[1] and stagenum[2]. The arrays are for storing the actual value to be set as true or false. The instance_change is for when the player completes a stage and runs off the room for effect. The switch statement on global.stage is for what stage the player is on and sets scripts for spawning, so it is easier for me to keep track of rather than putting different objects in each room. The scr_check_undefined is to check if the values are undefined or not and sets values according to what it reads.

scr_check_undefined
Code:
if (!is_undefined(argument0) && argument0 == true) {
        argument1 = true;
}else {
    argument1 = false;  
}
Level Control Object (create)
Code:
global.point_total = 0;
global.enemy_count = 0;
boss = false;
score_check = false;
rate_c = 80;
rate_b = 140;
rate_a = 210;
rate_asteroid = 100;

global.bossdefeat = false;

//room checking
switch (room) {
    case rm_stage_1:
        global.stage = 1;
        break;
    case rm_stage_2:
        global.stage = 2;
        break;
    case rm_stage_3:
        global.stage = 3;
        break;
}
timer = true;
alarm[0] = room_speed*4;

audio_play_sound(mus_theme1, 10, true);
 
Last edited:
Top