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

[SOLVED] Change instance back

J

jana

Guest
After changing an obj_player to a different object, in the collision action:
Code:
instance_change(obj_player_neutral,0);
I'm trying to change it back to an obj_player after a delay, but nothing is working. I tried putting this in the step event of the obj_player:
Code:
/// counter is declared and initialized in the create event

if (object_exists(obj_player_neutral))
{
     if (counter > 0)
     {
          counter--;
     }
     else
     {
          instance_change(obj_player_neutral,obj_player)
     }
}
No error, and the instance changed into obj_player_neutral, but it doesn't change back. I also tried putting this in the step event of obj_player_neutral, and also creating a separate object and putting the above in the step event. Same non-response both times.

I also put the following in the step event of the obj_player_neutral and got the same non-response:
Code:
/// counter is declared and initialized in the create event
 if (counter > 0)
     {
          counter--;
     }
     else
     {
          instance_change(neutral,obj_player)
     }
}
I did a search, and I see a lot of comments that refer to changing the new instance back, but I don't see any code or specifics on how to work with the new instance, except that you have to wait until the next step (how do you make sure you're not accessing it in the same step?), and that all the instance variables transfer over to the new instance.

Any help on how to change the instance back are gratefully appreciated.
 

Perseus

Not Medusa
Forum Staff
Moderator
Use a with statement, the second argument is meant for telling if the Create event of the new instance and the Destroy event of the current instance are to be triggered.

Code:
with (obj_player_neutral) {
   instance_change(obj_player, false);
}
 
J

jana

Guest
Thanks, this worked. This in the create event of obj_player_neutral:
Code:
neutral_step_count = 180; // wait 6 seconds before change back
and this in its step event:
Code:
if (instance_exists(obj_player_neutral)) // if a player got changed into a player neutral
{
    // countdown to change back
    if (neutral_step_count > 0)
    {
        neutral_step_count--;
    }
    else
    {
        with (obj_player_neutral)
        {
           instance_change(obj_player, false);
        }
        neutral_step_count = 180; // reset the counter for next time
    }
}
 
Last edited by a moderator:
Top