• 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!

GameMaker Keeping variables between rooms - only Global Variables and Persistent Objects?

Zahk

Member
I'm trying to do something very simple - I want to talk to an NPC, and then have them say something else after you talk to them the first time, even after you leave and return to the room they're in.

Now I know I can keep track of the variable that changes this with a Global Variable or by making the object persistent, but I don't really want the NPC to be a persistent object, and I also don't want to use a global variable, for two reasons: the first being that I'd rather use a universal but object-specific variable that I can just add to my NPC-interaction-script, and the second reason being that I'd rather the variable be tracked on the actual NPC, instead of having to constantly go back to a global variable object where I set up all my global variables for every single time I want to make a new one. I'm making an RPG, and there's bound to be a lot of little switches like this.

I feel like I'm missing something obvious. Is there any other way to set up a simple switch like this?
 

TheouAegis

Member
Nope, that's about it. You either dump everything into a file, which is just going to be a slow, waste of time, or you dump everything into some set of variables that won't get reset every time you leave or enter a room. You also have the option of using a buffer, list, map, or grid, all of which are global in nature and don't require a global object to keep track of them per se. The way GM runs, it allocates a certain block of memory in the RAM and recycles it every time you change rooms, then it also has a block of memory for the global unit that doesn't get reset when you change rooms. You don't have much control over which variables get reset and which don't outside of using the global unit or data structures like you would if you coded from the ground up, but even then you're still doing the same thing. So either you use one persistent unit (the global unit) or you make all units persistent. And since this is an RPG, I'm guessing you're going to be putting all the NPCs in by hand, in which case there's no reason you can't use the instance's creation code to specify in each instance what data pointer to use. Also don't forget that you can store more than one "variable" inside a variable with proper data manipulation, reducing the number of data points you need to use.
 
You can use ini_files to save data, and load data particular to the NPC, for example let's say you talk to them the first time right? This will then load in the information from the ini file to determine what should be said the character. I've written some code to augment it.

Writing the value...
Code:
/// @function write_npc_convo( )
/// @param fname
/// @param value
/*
    Feel free to add in extra stuff in here.
    make sure it's a real number...
*/

// Write the value...
ini_open( argument[ 0 ] );
ini_write_real( "Data", "Convo", argument[ 1 ] );
ini_close( );
Reading the value...
Code:
/// @function read_npc_convo( )
/// @param fname
/*
    Feel free to add in extra stuff in here.
    make sure it's a real number...
*/

var value;

// Read the value...
ini_open( argument[ 0 ] );
value = ini_read_real( "Data", "Convo", 0 );
ini_close( );

// Return the value to the variable...
return value;
A demo script to test it all together...
Code:
// Use for demo only, put this on an object in the Create Event...

// Reading this for the first time...
var val, fname = "DemoNpc";
val = read_npc_convo( fname );

// Say string respectively...
if ( val == 0 ) {
    show_message( "What\'s Good Fam." );
    write_npc_convo( fname, val + 1 );
} else {
    show_message( "I see you\'ve been here before, how\'s it going?" );
}
You're going to need to modify this for your game, but the concepts are there.
 

TheouAegis

Member
Also, consider making the dialogues dependent upon external references. For example, if a particular boss was flagged as defeated, you don't need to worry about setting the boss's kill flag AND the dialogue references as well, just make a dialogue option dependent on the boss. Or if you need a particular unique item brought back to the NPC, just check the item's pickup flag instead of setting a separate dialogue reference.
 

Miradur

Member
There is still one possibility and in my opinion, the best for an RPG .... lists. Lists and grids are also global in GMS
and I think that this is also solved in games when it comes to many switches.


Miradur
 
Top