GameMaker [SOLVED] Help With Creating Same Instance in Multiple Rooms

S

Solvir

Guest
Hopefully someone can help me with this..

When my character attacks it calls upon this function to create an obj in the room:
Code:
var damage = instance_create_layer(xx, yy, rm_1, obj_damage);
damage.creator = id;
damage.damage = obj_player_stats.attack;
attacked = true;
Now this works within rm_1. When I get into the next room, I can move around just fine, but when I attack the game crashes and gives me this:

############################################################################################
FATAL ERROR in
action number 1
of Step Event0
for object obj_player:

Pop :: Execution Error - Variable references invalid object (-4).<unknown variable>
at gml_Script_scr_attack_state (line 55) - damage.creator = id;

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

My first guess is the instance_create_layer() isn't assigning the proper layer ID. So my question is, how do I get my layer ID to work for multiple rooms? Or is there something else I need to do? Pretty stuck and very new to gamemaker so please be patient with me, thank you!
 
Last edited by a moderator:

obscene

Member
You made damage a temporary variable ( because you used var )

So when you refer to damage later, GM has no idea what you mean.

Also make sure it's persistent so it's still there when you change rooms.
 
S

Solvir

Guest
You made damage a temporary variable ( because you used var )

So when you refer to damage later, GM has no idea what you mean.

Also make sure it's persistent so it's still there when you change rooms.
I tried that but to no avail. instance_create_layer() is still trying to make it in rm_1 while I'm in rm_2 , so I might need to just make a whole new block of code for each room, but that seems so redundant. There has to be someway for you to just say, "create the object in the current room".
 
Last edited by a moderator:
M

Marcos Mena

Guest
I'm not completely sure what're you're trying to accomplish by creating an Object called obj_damage but you might want to think about using Global Variables for statements that need to run on several rooms/objects.

ex: global.damage = 0;

That would be creating a Global Variable called Damage which you can call in every room and every object.
 
S

Solvir

Guest
Replace the rm_1 to room in the instance_create_layer and then check.
Using room works, but only for the first room still. It crashes in my second room.

I'm not completely sure what're you're trying to accomplish by creating an Object called obj_damage but you might want to think about using Global Variables for statements that need to run on several rooms/objects.

ex: global.damage = 0;

That would be creating a Global Variable called Damage which you can call in every room and every object.
I tried this but I couldn't get it to work, I might keep at it but I'm trying other fixes currently.


Right now my method of traveling between rooms is handled by :

Code:
/// Teleport player
new_x = 0;
new_y = 0;
new_room = noone;
layer_has_instance(room, obj_dash_effect)
layer_has_instance(room, obj_damage)
and I just go into the creation code of the object I interact with in the room and put in this:

Code:
new_room = rm_2;
new_x = 80;
new_y = 140;
It doesn't look like my layer_has_instance() code is working but I'll keep seeing what I can do. Thank you everyone who has replied so far.
 
Last edited by a moderator:
M

Marcos Mena

Guest
Using room works, but only for the first room still. It crashes in my second room.



I tried this but I couldn't get it to work, I might keep at it but I'm trying other fixes currently.


Right now my method of traveling between rooms is handled by :

/// Teleport player
new_x = 0;
new_y = 0;
new_room = noone;
layer_has_instance(room, obj_dash_effect)
layer_has_instance(room, obj_damage)

and I just go into the creation code of the object I interact with in the room and put in this:

new_room = rm_2;
new_x = 80;
new_y = 140;

It doesn't look like my layer_has_instance() code is working but I'll keep seeing what I can do. Thank you everyone who has replied so far.
I get it. Again, I'm not sure how your game works but you could just create an Object that runs room_goto_next when it collides with the player and create the instance of the player where you want him to be at the beginning of the level.

I'd love to take a look at the actual project to see if I can be of help, if not I'm gonna surely will try to set you on the right direction or find someone that can help you with it.
 
A

Aura

Guest
Perhaps give the Manual entry for instance_create_layer() a read first?

http://docs2.yoyogames.com/source/_...instance_functions/instance_create_layer.html

The function needs a layer ID or name, you're feeding it a room index. Why do you even expect it to work? It might have been working in rm_1 if the room index is the same as a layer ID, but that's purely coincidence. My suggestion would be to create a seperate layer for that using the IDE, then you can do this:
Code:
instance_create_layer(xx, yy, "damage_layer", obj_damage);
Or dynamically create the layer using layer_create() and store that ID in a variable, then use the variable as the layer argument. Do this at room start, then you don't have to create a layer for each room using the GUI. And in case you find this troublesome, use the instance_create_depth() function instead.

http://docs2.yoyogames.com/source/_...instance_functions/instance_create_depth.html

Also, read the Manual entry for layer_has_instance() as well:

http://docs2.yoyogames.com/source/_...eference/rooms/layers/layer_has_instance.html

It returns true or false, use it as a conditional. Calling it as an independent function does nothing.
 
S

Solvir

Guest
Okay so switching all my instance_create_layer() to instance_create_depth() solved my problem, and so far everything is okay.

Code:
var damage = instance_create_depth(xx, yy, -y, obj_damage);
is the one for my damage and works perfectly from room to room. I also no longer need the layer_has_instance() code in the object that teleports my player from room to room.

Perhaps give the Manual entry for instance_create_layer() a read first?
The function needs a layer ID or name, you're feeding it a room index. Why do you even expect it to work? It might have been working in rm_1 if the room index is the same as a layer ID, but that's purely coincidence. My suggestion would be to create a seperate layer for that using the IDE, then you can do this:
Code:
instance_create_layer(xx, yy, "damage_layer", obj_damage);
I'll admit, my knowledge of layers is lacking and I'll do my best to become more familiar. I really want to experiment with this, thank you so much for your reply.

Also thank you for the quick fix with the instance_create_depth() suggestion, I'm sure there are a lot of ways to go about fixing my problem, but this did the trick.

I get it. Again, I'm not sure how your game works but you could just create an Object that runs room_goto_next when it collides with the player and create the instance of the player where you want him to be at the beginning of the level.

I'd love to take a look at the actual project to see if I can be of help, if not I'm gonna surely will try to set you on the right direction or find someone that can help you with it.
Thank you for the reply, I was able to solve the issue. I'll see if I can get your solution to work as well, or apply it elsewhere, the more I learn the better.
 
Last edited by a moderator:
Top