• Hey Guest! Ever feel like entering a Game Jam, but the time limit is always too much pressure? We get it... You lead a hectic life and dedicating 3 whole days to make a game just doesn't work for you! So, why not enter the GMC SLOW JAM? Take your time! Kick back and make your game over 4 months! Interested? Then just click here!

Coding Persistence Question

BQubed

Member
I've been told, by quite literally everyone I asked, not to use the 'persistent room' checkbox. So I've started trying to code some persistence into the room. Here's what I've started with:

GML:
for (var i = 0; i < instance_number(InteractableParent); ++i;)
{
instances[i] = instance_find(InteractableParent,i);
}
This successfully takes all the IDs of the instances and stores them in instances[i]. I'm looking three main variables from each instance: x, y, and dialogue_state. Problem is I'm not entirely sure where to go from here and I've found precious little in terms of videos or documentation regarding coding persistence. I was thinking that every time I transit to another room, the variables are saved into a ds_map, or with GMS 2.3, should I use a struct? If someone could point me in the right direction or even show me some helpful video/documentation, that'd be great.
 

Slyddar

Member
I have a video on how to do it, but it is part of a series, and it's in DnD, but the concept is the same, so it might be of help to you.

 

BQubed

Member
I have a video on how to do it, but it is part of a series, and it's in DnD, but the concept is the same, so it might be of help to you.

Cheers, I'll check it out.

EDIT: Alright, I watched the video. It was a little helpful to know that I'm at least heading in the right direction, but honestly I had trouble following DnD. Thanks though!
 
Last edited:

Yal

šŸ§ *penguin noises*
GMC Elder
I've been told, by quite literally everyone I asked, not to use the 'persistent room' checkbox.
Room persistence has its uses, but I definitely agree that you don't want to set it in the GUI: you'll want to set it using the room_persistent variable, so you can turn it off when the room needs resetting. But it has some caveats even if you do it that way:
  • Biggest one is, you can only turn off persistence if you're inside the room (you can turn it off from other rooms, but the persistent state WILL be loaded if it exists anyway - and the only way to remove a persisted state is to leave the room while it is not persistent), so things like a pause menu that uses persistence can't restart the room or return to the map unless you go back to the room and un-persist it.
  • Persistent rooms are saved to the default save/load system, but there's no way to access the data if you want to save it in your own file
  • Persistent rooms use a lot of memory, since it needs to store the current state of everything in the room, including every instance currently in it and all their variables, the entire tilemaps, all asset layer assets, etc.
  • Using persistent objects and persistent rooms at the same time has confusing precedence effects, so make sure you know what you're doing if you do this.
 
Personaly, I would use a persistent OBJECT/Game Manager, and make it save the data for when you come back.
Could look like this

GML:
//Array holding our struct
global.array = [];

//Function to create our struct
function monster_data constructor(){
    _xx = other.x;
    _yy = other.y;
    dialog_state = STATE_SOMETHING;
}

//Number of parents on the field
var _pl_numb = instance_number(InteractableParent);

//Loop to assign strtucts to the array
for(var _i=0; _i< _pl_numb; _i++){
    global.array[_i] = new monster_data();
}


//To access back
var _parent_x = global.array[$ "_xx"];                       //Access _xx
var _parent_y = global.array[$ "_yy"];                     //Access _yy
var _parent_dial = global.array[$ "dialog_state"];    //Access _dialog state
 

BQubed

Member
Personaly, I would use a persistent OBJECT/Game Manager, and make it save the data for when you come back.
Could look like this

GML:
//Array holding our struct
global.array = [];

//Function to create our struct
function monster_data constructor(){
    _xx = other.x;
    _yy = other.y;
    dialog_state = STATE_SOMETHING;
}

//Number of parents on the field
var _pl_numb = instance_number(InteractableParent);

//Loop to assign strtucts to the array
for(var _i=0; _i< _pl_numb; _i++){
    global.array[_i] = new monster_data();
}


//To access back
var _parent_x = global.array[$ "_xx"];                       //Access _xx
var _parent_y = global.array[$ "_yy"];                     //Access _yy
var _parent_dial = global.array[$ "dialog_state"];    //Access _dialog state
This is what I'm going for. I want to save the instance's x/y location and the state of their dialogue tree at the moment I leave the room. This will be helpful. Thank you.
 

BQubed

Member
@Slow Fingers I replaced other with InteractableParent. While debugging I saw that it was storing three (the number of instances in the room) sets of variables but all from the same instance.

GML:
function npc_data() constructor
{
    _xx = InteractableParent.x;
    _yy = InteractableParent.y;
    _dialogue_state = InteractableParent.dialogue_state;
    _img_index = InteractableParent.image_index
}
Do you happen to have some idea of what would work in that spot? The first snippet I posted above gets me all the IDs of the room instances so I even tried using the instances array variable instead of InteractableParent but I just got crashes because it says that it wasn't declared before use, which I am a bit confused about to be honest.
 
Last edited:

Nidoking

Member
How would it store ALL variables when you've only got one variable for each? One variable holds one value. That value could be an array, or the index of a data structure, but it's still a single value. If you want to store information from multiple instances of the same object, have a look at with loops and either make a bunch of this struct, or use arrays.
 
@Slow Fingers I replaced other with InteractableParent. While debugging I saw that it was storing three (the number of instances in the room) sets of variables but all from the same instance.

GML:
function npc_data() constructor
{
    _xx = InteractableParent.x;
    _yy = InteractableParent.y;
    _dialogue_state = InteractableParent.dialogue_state;
    _img_index = InteractableParent.image_index
}
Do you happen to have some idea of what would work in that spot? The first snippet I posted above gets me all the IDs of the room instances so I even tried using the instances array variable instead of InteractableParent but I just got crashes because it says that it wasn't declared before use, which I am a bit confused about to be honest.
My guess is you just have to call "other." instead of "InteractableParent." Sorry, it's still early and havent slept well, those guys who remove snow in the streets are noisy as **** lately! So maybe I don't make much sense, but I'm pretty sure.
 

BQubed

Member
My guess is you just have to call "other." instead of "InteractableParent." Sorry, it's still early and havent slept well, those guys who remove snow in the streets are noisy as **** lately! So maybe I don't make much sense, but I'm pretty sure.
I tried other in the beginning like your example showed, but I had a crash. Thing is, I've placed this code within the room transition code so it's the first thing that happens when the player makes contact with the transition object. So I think other is referring to the player character. I'm not sleeping well either. Feels like a gamedev trait.
 
Top