Variable not working?

So simple enough. I am trying to make a parent asset that utilizes the in room variable for what object is being controlled and/or called. But the game doesn't seem to be recognizing it. So here is the setup...

Inside the Step Event
GML:
if (triggered = 1) {
    instance_create_layer(oHero.x+150, oHero.y, "Hero", _NPC);
    with(_NPC) { image_xscale = -1; }
    triggered = 0;
    instance_destroy();
}
Inside the room the variable "_NPC" is set to the object I want to call in. It triggers fine, just never creates the object. It is worth nothing that if I replace the _NPC with the actual object name it works as expected.
Screenshot_3.jpg
 

TsukaYuriko

☄️
Forum Staff
Moderator
Put a breakpoint at the line that creates the instance and check the value of _NPC when it runs. That may give you an idea of what's going wrong.
 

kburkhart84

Firehammer Games
I should also mention...why are you setting triggered to 0 if you are destroying the instance in the line right after anyway?!?! I get the feeling you have a logic type of problem here based on that detail.

Also, _NPC, as of the variable you created, is going to be an object asset. However, using that with() statement on the same variable, if it works like it theoretically should, would change the image_xscale variable for ALL instances of the _NPC object...I assume that isn't what you want. What you would with instead is store the return value of instance_create_layer() into a variable, and then use that with the with() statement. This lets you access only the single instance instead of all instances of that object. Fixing this may or may not fix the problem considering the detail I noticed above and your possible logic problem, but it may be a good start.
 
I should also mention...why are you setting triggered to 0 if you are destroying the instance in the line right after anyway?!?! I get the feeling you have a logic type of problem here based on that detail.

Also, _NPC, as of the variable you created, is going to be an object asset. However, using that with() statement on the same variable, if it works like it theoretically should, would change the image_xscale variable for ALL instances of the _NPC object...I assume that isn't what you want. What you would with instead is store the return value of instance_create_layer() into a variable, and then use that with the with() statement. This lets you access only the single instance instead of all instances of that object. Fixing this may or may not fix the problem considering the detail I noticed above and your possible logic problem, but it may be a good start.
I set the triggered to equal 0 because I thought otherwise I would get an error if I didnt. But I took that out.
Adversely, I did get it working. I was also setting _NPC = 0; in the create event which I guess was over writing the in room variable setup.
 

kburkhart84

Firehammer Games
I set the triggered to equal 0 because I thought otherwise I would get an error if I didnt. But I took that out.
Adversely, I did get it working. I was also setting _NPC = 0; in the create event which I guess was over writing the in room variable setup.
Yup, setting triggered to 0 in the same code block you are destroying the thing in wouldn't cause errors, but it is a useless piece of code. The variable you waste time setting disappears anyway since you destroy the object.

And yes, you generally only want a variable either in the create event, OR the dialog, but not both.
 
Top