Keeping a value once you exit a room

pgvago

Member
Hello from Italy;

I have a question which is probably easy for expert programmers.
I have an object of which I created 4 instances in a room. These objects are switches of which I need to remember the position once I leave the room.
Making the object persistent would create a problem as I use this switch objects in many rooms and only in this particular case I need to remember the position.
I was thinking of using global variables but I wouldn't know how to reference them from within the object script.

Any suggestions?

Thanks, Pietro.
 

samspade

Member
You would use either a global variable or an instance variable inside of an instance of a persistent object. Global variables are never destroyed once created and objects (instances of objects) that are marked as persistent are not destroyed upon room change so you can use both of them to store variables even when switching rooms. Both are referenced normally (e.g. either with global.variable_name or for an object using with (inst_id) or inst_id.variable_name).

Which one you should use and how you name them depend a lot upon what your doing. Without more, I would probably recommend using a persistent object as it doesn't seem like these variables need to exist for the entire time your game is running (e.g. in the menu, etc). But for prototyping globals might be faster. With code, we could probably give more specific recommendations.
 

pgvago

Member
Hello Sam and thanks for your reply.

Here are some lines of code:

First Room Creation code:
global.blu_room_switch_00_pulled = false;

Second room creation code:
blu_room_tile_00 = instance_create_layer (100, 100, "blu_room_layer", obj_floor_tile);
blu_room_tile_00.pulled = global.blu_room_switch_00_pulled;

This creates a global variable which is then copied as an instance variable in the switch istance.
I then have a collision event that changes the value of "pulled" from false to true.
What I am not able to do is to send this "true" value to the global variable.
I could just write global.blu_room_switch_00_pulled = true but this code would be the same for every instance and I wouldn't be able to reference a different variable in a different instance.

I hope I have explained myself as English is not my mother tongue.

Thanks, Pietro.
 
Last edited:

samspade

Member
Yeah, for something like this you need a specific way of referring to multiple switches, both numbers and strings would work. I tend to use strings because they're human readable. For example the following code in a script asset would be a very straightforward way to track switches. You maintain an array of switch names, and if the array contains the name then the switch is on, otherwise it is off. If you were going to have a set amount of switches, you could also just hard code the names in:

GML:
///switch manager script asset
global.switch_array = [];

function turn_switch_on(_name) {
    array_push(global.switch_array, _name);
}

function turn_switch_off(_name) {
    for (var i = 0; i < array_length(global.switch_array); i += 1) {
        if (global.switch_array[i] == _name) {
            array_delete(global.switch_array, i, 1);
            break;
        }   
    }
}

function return_switch_state(_name) {
    for (var i = 0; i < array_length(global.switch_array); i += 1) {
        if (global.switch_array[i] == _name) {
            return true
        }   
    }
    return false;
}
Now you just need to have a variable in your switches and things the switches are attached to that you can set such as my_switch. If you are placing these instances in the room editor, you can make them an object variable (which is what I would do). If you are creating them through code, you would set their name just after creation as you do in your code above. Then you can just use functions like the ones above to toggle switches on or off.

As a note, I haven't tested the code, and if I were to do it, I'd probably actually use a struct, but arrays are a bit easier to understand.
 

pgvago

Member
Ciao Sam;

I fully understand the logic of your script.
Just one question: Did you creat the functions array_push() and array_delete()?
I don't seem to find them in GameMaker 2.25.

Pietro.
 

pgvago

Member
Thank you.
I have been waiting to upgrade as I am afraid something will brealk in my project.
 

curato

Member
I would back it up before you try just good practice but the conversion makes you convert it to a new name on top of that. Also, you can always log in and get the older revision if you need it. I think they fixed most of the serious bugs with the new version already.
 
Top