Issues with instances and if statements

J

Jmarlin3

Guest
So the player object is supposed to cross over a floor object, with the floor object switching to a red floor object. If the player object is not touching the red floor object anymore, the red floor object dissapears and turns into a wall object, meaning they can't go back.the

Here's the code:

obj_floor STEP EVENT:
if place_meeting(x,y,obj_player){
instance_create_layer(x,y,4,obj_redfloor);
instance_destroy();
}

obj_redfloor STEP EVENT:

if !place_meeting(x,y,obj_player) {
instance_create_layer(x,y,4,obj_wall);
instance_destroy();
}

When I run the game, the player already has the floor its on gone, I assume because the step event has already started. And when I move to the next floor object, it immeadietly dissapears. I'm trying to make the code work only if the player has interacted with it.
 

samspade

Member
Your question is a little unclear. The code you posted seems like it should work, outside of using a number for the layer which you shouldn't do. Either use the keyword layer or use the actual layer name in quotes. If you want to use a number, get or save the layer_id to a variable and use that variable. Otherwise, you're relying on the layer to always match that number, and I'm not sure that is guaranteed. This could be causing your problem as if there isn't a layer with the layer_id of 4 it won't create anything.

Otherwise, it is difficult to tell exactly what is wrong or what is happening that you don't want to happen. You say when you run the game the player has the floor it is on already gone. That is what you are telling it to do with the code. Do you not want that to happen? You say, and when you move to the next floor object, it immediately disappears. Again, that is what you are telling it to do in the code. Is that not what you want to happen? Or do you mean obj_floor disappears and it is not replaced with obj_redfloor? You then say you're trying to make the code work only if the player has interacted with it, but from both of your prior sentences, the floor is only changing when it interacts with the player - i.e. the player 'collides' with it. So what does interact mean here? Do you want the floor the player starts on to not be affected?

Answering those questions (and fixing the layer problem) would let us provide a better answer.

Also, remember to use code tags and indent when you post code. It makes it easier to read and more likely that people will help (and not make a mistake themselves).
 
J

Jmarlin3

Guest
Alrighty, let me clarify (Sorry about that):

So my objective is to have the player go across each obj_floor, and when the player is on top of obj_floor, it's supposed to turn red(now it's obj_redfloor). Then, when the player leaves obj_redfloor, it turns into obj_wall, meaning they can't go back. I'm not able to provide a video at the moment, but I can describe what's going on as best as I can.

When I first run the game, I have the player placed right next to the first obj_floor. When I move the player until it touches obj_floor, it dissapears, not doing what I described above.
I have gotten help from someone else on reddit, here's the rewritten one:

obj_player CREATE EVENT:

Code:
move_speed = 2;
obj_player STEP EVENT:

Code:
if keyboard_check(ord("W")) {
    y -= move_speed;
} else if keyboard_check(ord("S")) {
    y += move_speed;
}
if keyboard_check(ord("D")) {
    x += move_speed;
} else if keyboard_check(ord("A")) {
    x -= move_speed;
}
obj_floor STEP EVENT:

Code:
if place_meeting(x, y, obj_player) {
    instance_create_layer(x, y, 2,obj_redfloor);
    instance_destroy();
}
obj_redfloor

Code:
if !place_meeting(x, y, obj_player) {
    instance_create_layer(x, y, 2,obj_wall);
    instance_destroy();
}
I'm also not entirely sure what "instance_layer" does, because I think the issue is that I have a number when I should have that.When I put "instance_layer" as the layer id, I get an error. What else am I missing to get this right? And also thank you so much for responding. I look forward to your answer.
 

samspade

Member
I think then the problem might actually be that you are using numbers for the layer argument as your code seems fine otherwise. If you use instance create layer but then specify a layer that doesn't exist, nothing will be created. Try either:

Code:
instance_create_layer(x, y, layer, obj_wall); //layer is actually the word layer and should turn green or whatever color you have set for keywords

//or

instance_create_layer(x, y, "Instances", obj_wall); //"Instances" should be the name in quotes of the layer you want it on. It must exist in that room.
If that doesn't fix it, it would be worth learning the debugger and walking through with that:

 
J

Jmarlin3

Guest
I used
Code:
instance_create_layer(x, y, layer, obj_wall);
Great news, it worked!
One final question though: how come just the word layer works but "instance_layer" doesn't for layer id.
Again thanks for the help!
 

samspade

Member
I don't think instance_layer is a thing? The keyword layer just uses the layer id of the instance that is using instance_create_layer. Otherwise, GMS2 allows you to specify the layer using a string or the layer id but if you're going to use the layer id you need to get it first. Just using a number isn't guaranteed to work.
 
J

Jmarlin3

Guest
Oh ok. I think what I had in mind was layer, but for some reason I wrote "instance_layer"
 
Top