instance_change

Changes the calling instance to an instance of a different object.

Syntax:

instance_change(obj, perf);


Argument Description
obj The new object the calling object will change into.
perf Whether to perform that new object's create and destroy events (true) or not (false).


Returns: N/A


Description

You can use this function to change one instance of an object into another instance of a different object, and while doing so decide whether to perform the initial instances destroy event and the new instances create event. In this way, you can have (for example) a bomb change into an explosion - in which case the "perf" argument would probably be true as you would want the bomb to perform its destroy event, as well as the explosion to perform its create event - or you could have your player character change into a different one - in which case the "perf" argument would probably be false as you do not want the instances to perform their create and destroy events.

It is worth noting that changing the instance means that you should perform no further actions with that instance until the next step, in particular trying to access variables etc... as that will cause an error. Basically, you change the instance but it is not actually available until the end of the current step, so to access any of the variables it contains directly (for example, calling obj_Changed.x) will not work.

WARNING! When changing a physics enabled instance, the physical properties will not be carried over to the new instance being changed into. Therefore you should have code in place to "transfer" the physics state of the current instance over to the new instance, or have defined the new instances physical properties in its Create Event, or in the object editor. For this reason it is recommended that you not use this function with physics enabled instances, but rather use a combination of instance_destroy() and instance_create().


Example:

if keyboard_check(vk_enter)
   {
   instance_change(obj_Player_Swimming, false);
   exit;
   }

The above code will change the calling instance into an instance of "obj_Player_Swimming" without performing the original instances destroy event nor the new instances create event when the user presses the "Enter" key, exiting the code block when done.


Back: Instance Functions
Next: instance_copy
© Copyright YoYo Games Ltd. 2018 All Rights Reserved