Loading Variables from a Struct

BQubed

Member
I'm stuck pretty hard on the issue of coding persistence into my game. Here's the scoop:

GML:
/// @description Save to Struct
function npc_data(_name, _xx, _yy, _dialogue_state, _img_index) constructor
{
    name = _name //unique instance ID
    xx = _xx;
    yy = _yy;
    npc_dialogue_state = _dialogue_state;
    img_index = _img_index;
}

with(InteractableParent)
{
    var _inst_vars = new npc_data(name, x, y, dialogue_state, image_index);
    var _persist_map = ds_map_add(global.npc_map, name, _inst_vars);
}
This code is situated in Alarm[3] of the parent event of all my interactable instances. It works and successfully adds all those variables to global.npc_map (which is in my persistent game controller object).

In Alarm[4] I have this:
GML:
/// @description Load from Struct
if ds_map_exists(global.npc_map, name)
{
    x                = global.npc_map[? name].xx;
    y                = global.npc_map[? name].yy;
    dialogue_state    = global.npc_map[? name].npc_dialogue_state;
    image_index        = global.npc_map[? name].img_index;
}
This is what I'm using to try and load the variables upon entering the room. Also, inside InteractableParent I have alarm[3] = 1 at the end of the create event which is to have all active instances send their variables to the ds_map.

When I use show_message(global.npc_map[? name])I get:

1612863640337.png

This is exactly what I want.

Now here's the issue. Whenever I enter a new room, I don't know what happens to the data loaded into the ds_map. The next room I show_message(global.npc_map[? name])I get the same thing but for the instances in the current room, but not the former. I don't know if the map is being overwritten or what, but even though the variables are saved properly and I can pull them individually via show_debug commands, the rooms always revert back to the original.

This is my first coding of persistency so I may have missed something obvious. If you need to see more code or have any questions, you need only ask.
 

Attachments

matharoo

manualman
GameMaker Dev.
Are you sure your loading code is executing before your saving code? Since saving happens in alarm[3] which is running 1 step after entering a room, you can use Room Start or Create to see if the data from the previous room persists.

Try setting your alarm[3] to run for example 10 steps later, instead of 1.
 

BQubed

Member
Are you sure your loading code is executing before your saving code? Since saving happens in alarm[3] which is running 1 step after entering a room, you can use Room Start or Create to see if the data from the previous room persists.

Try setting your alarm[3] to run for example 10 steps later, instead of 1.
Currently, I have saving happening in my Room End event in my persistent game object and loading happening in Room Start.
 

matharoo

manualman
GameMaker Dev.
Currently, I have saving happening in my Room End event in my persistent game object and loading happening in Room Start.
What happens in those events, and how do they differ from/connect with the alarms 3 and 4 mentioned in the original post?
 

BQubed

Member
What happens in those events, and how do they differ from/connect with the alarms 3 and 4 mentioned in the original post?
They just contain with(InteractableParent) { alarm[3] = 1; } and with(InteractableParent) { alarm[3] = 1; }.

alarm[3] = 1; is also triggered inside the create event of InteractableParent.

The only time alarm[4] is triggered is in the Room Start event I mentioned above.
 

matharoo

manualman
GameMaker Dev.
They just contain with(InteractableParent) { alarm[3] = 1; } and with(InteractableParent) { alarm[3] = 1; }.

alarm[3] = 1; is also triggered inside the create event of InteractableParent.

The only time alarm[4] is triggered is in the Room Start event I mentioned above.
Okay, first of all it is weird that the same alarm is being set twice, once in global Room Start and again in the Create event. Choose one or the other to avoid confusion -- I'm guessing you will want to keep it in the Create event of the instances, so that newly created instances not present in Room Start are saved too.

Did you try delaying alarm[3] running at room start? You wanna make sure that alarm[4] runs before it, so it can load previous values before they are overwritten in alarm[3].
 

BQubed

Member
I did try delaying it but nothing happened. What I don't get is what happens to the variables after I leave the room? They're stored within a global variable in a persistent object so they should stay there, but when I take a look in the ds_map it's always just the instances in the current room.
 

Nidoking

Member
If I understand this correctly, you're setting an alarm to save the data one step after the Room End event?

The room is ending. One step later, and you'll be in the next room. You're saving the next room's data. You don't want to use an alarm at that point. You can call the alarm events directly, or use some other event. My general pattern is to put these events in User Events and call those from the Alarm Events if I want them to happen later, or directly if I want them to happen now. There's no reason for either of these to be alarms.
 
Top