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

Variables Within Variables in GMS2

John Bailey

Member
Hello, I'm setting up some code in a parent event to permanently despawn objects in non-persistent rooms with. What i need to do in order for this to work is set the variable "counter" to whatever variable i input from instance creation code, but not what that variable is actually equal to (true, false, ect.), and then be able to check/modify it accordingly. Is there a GM function or anything that'll let me pull this off?
Code:
/// Object
// Create event
counter = noone;

// Destroy event
if (counter != noone) counter = true;

/// Room
// Instance creation code
counter = obj_player_stats.counter0;
if (counter == true) instance_destroy();
 
Last edited:

John Bailey

Member
@Kyon With the way my game is set up, variables tied to "obj_player_stats" essentially function like global variables. The main issue in the example is that I need to be able to set whatever "obj_player_stats" variable I input from instance creation code to true in the destroy event.
 
Last edited:

samspade

Member
Still looking for a solution to this if anyone has an idea.
I'm not really sure what you're trying to do. Are you saying that you have instances of an object in a non-persistent room that under some circumstances you want to not be there when you go to that room? (e.g. you kill an enemy in the room, leave the room, come back, and don't want it to be there when you come back?)

In that case I would say that an array or list in some kind of persistent object is probably easiest. For example, say you have five objects that you want to be contingent upon some other variable. In obj_player_stats have despawn_array = [false, false, false, false, false]; Then in the instance creation code of the five objects have each look up a position in that array and destroy themselves if true (e.g. if (obj_player_stats.despawn_array[0] == true) instance_destroy();). Then you simply change the value in that array when you don't want them to spawn.

Alternatively, you could spawn your instances in in code, rather than the room IDE and just put the check before spawning them in.
 

John Bailey

Member
@K3fka "variable_instance_set" was exactly what I was looking for, thanks for the help!

My revised code looks like this, for future reference:
Code:
/// Object
// Create event
despawn_counter = noone;

// Destroy event
if (despawn_counter != noone) variable_instance_set(obj_player_stats.id, despawn_counter, true);

/// Room
// Instance creation code
if (obj_player_stats.counter0 == true) instance_destroy();
despawn_counter = "counter0";
EDIT:
@samspade @jaydee Using arrays is a good idea, I don't use them often so I'm not too familiar with how they work, but your example helped samspade.
 
Last edited:
Top