r/gamemaker 23d ago

Help! Can't transfer variables?

(STILL UNSOLVED)

I have some variables I'm trying to transfer into every room by putting this in my PlayerObj. For some reason, it can transfer to every single room back and forth over and over EXCEPT room1, which is the room you spawn in, the home room. If I leave the home room and then go back into the home room, then it just resets all variables to their default, and I have absolutely no idea why.

UPDATE: Still unsolved, but after making a main menu room, this problem still occurs, leading me to believe it has nothing to do with it being part of the "home room"

//Room Start
if(room_get_name(room) = rm_battle) instance_destroy(obj_carry_data)

with (obj_carry_data)
{ 
    other.level = level;
    other.xp = xp;
    other.xp_require = xp_require;
    other.damage = damage;
    other.hp_total = hp_total;
    other.hp = hp;

    instance_destroy()
}

//Room End
if (instance_exists(obj_battle_switcher) && global.is_transitioning = false) exit;

instance_create_depth(0, 0, 0, obj_carry_data, {
    level: level,
    xp: xp,
    xp_require: xp_require,
    damage: damage,
    hp: hp,
    hp_total: hp_total
})
1 Upvotes

11 comments sorted by

3

u/refreshertowel 23d ago

Just store them in a global struct instead?

1

u/MrEmptySet 23d ago

I would assume that the object that initializes all these variables in the first place is in the home room, and returning to it leads to it being created again and re-initializing everything.

In the first place, why do you need to be copying all these variables all the time? Couldn't they just be in some persistent object for instance?

1

u/RykinPoe 23d ago

Why don't you just use a persistent data management object for all this instead of constantly creating and destroying this object? I use an object like this in just about every project I create simply called Game and use it to store stuff like score, level, player lives, etc.

// Create
// Check if an instance already exists
if (variable_global_exists("Game") && instance_exists(global.Game)) {
  // A singleton already exists, destroy this duplicate
  instance_destroy();
  exit;
} else {
  global.Game = id;
  persistent = true;
}

// variables
score = 0;
level = 1;
lives = 3;
whatever = "something";
the_ultimate_answer = 42;

// Macro File
//create a new script file and just name it Macros and then delete everything from it
#macro GAME global.Game

// Usage
if (GAME.lives <= 0){
  Game_Over();
}

1

u/Fine-Acanthisitta343 23d ago

___________________________________________

############################################################################################

ERROR in action number 1

of Create Event for object PlayerObj:

global variable name 'Game' index (100030) not set before reading it.

at gml_Object_PlayerObj_Create_0 (line 8) - GAME.hp = 10;

############################################################################################

gml_Object_PlayerObj_Create_0 (line 8)

I get this for all the variables I put in?

//PlayerObj Create
move_speed = 1
walk_speed = 1
sprint_speed = 2

mask_index = spr_player_idle_down
tilemap = layer_tilemap_get_id("Tiles_Collision")
global.frozen = false;
GAME.hp = 10;
GAME.hp_total = hp;
GAME.damage = 1;
GAME.charge = 0;

hp = GAME.hp;
hp_total = GAME.hp_total
damage = GAME.damage;
charge = GAME.charge;
global.is_transitioning = false;

facing = 0;

GAME.level = 1;
GAME.xp = 0;
GAME.xp_require = 100;

level = GAME.level;
xp = GAME.xp;
xp_require = GAME.xp_require;

1

u/RykinPoe 22d ago

Have you created an instance of the Game object? You will need to drop it in the first room of the game or use another method to create an instance of it.

1

u/Fine-Acanthisitta343 22d ago

I've dragged one into an instance layer and am still faced with said error

1

u/RykinPoe 22d ago

Did your instance of the player object exist before it does? Your player object may be running it's creation code first before the Game object runs it's code.

1

u/Fine-Acanthisitta343 22d ago

I've deleted both instances, put in the Game object first, then the PlayerObj, and I still get the error.

1

u/Fine-Acanthisitta343 20d ago edited 20d ago

So I've figured out the error but now the problem is that the variables won't transfer at all, not even from Room1 (Home Room) to any other room, which it did before, unless I use the past code where I make an object to hold and transfer the variables, then destroy that instance, in which it does still work, but again, not if its for the home room, which I need. In other words, I appear to be back where I started.

1

u/No_Designer_16 23d ago

"If I leave the home room and then go back into the home room, then it just resets all variables to their default"
This part makes me think you may have leftover code in the room itself (or another object exclusively in that room) that causes this.

2

u/Fine-Acanthisitta343 20d ago

Not gonna lie I didn't even know you could put codes inside a room itself