Tips and Tricks #1

Welcome!

This is a weekly section (which may get updated more than once a week!) where we give little tips and tricks for GameMaker: Studio. Everything from editing sprites, rooms, objects, to GML and everything in -between.

 

Tips and Tricks #1: instance_kill();


This might be a simple one, but for beginners out there, this can save you a lot of headache.

When you want to destroy an instance, you always use instance_destroy() however, instance_destroy() isn’t one of those fancy functions you can pass arguments to. The way this works is by calling this function from within the object/instance you wish to destroy.

Here  is an incorrect usage for instance_destroy();

// Invalid GML Code
instance_destroy(myObject);

To correctly use this function, most people do this by calling it inside a step or alarm of the object, like this:

// Inside Step or Alarm
instance_destroy();

Another method most people use is by calling the with() function. This allows you to call the function ‘inside’ the object or instance without the code actually being attached or running from the object or instance.
Example:

// Anywhere
with (myObject) {
instance_destroy();
}

However, there is a simple way to keep things neat and clean and give you access to killing objects and instances with a nice little function I call instance_kill().

First, create a script and give it the name instance_kill.

Then insert this code into the script:

// instance_kill() Script.
// Usage: instance_kill(object or instance ID);
 
with (argument0) {
instance_destroy();
}

Now, save your script and you’re done! Anytime you want to use destroy and instance, you can simply use the code:

// Anywhere
instance_kill(myObject);

or

// Anywhere
instance_kill(InstanceID);

But be careful, you’re still using a with() function which loops through every object if you use a specific object name. So if you have more than one “myObject” in the room, it will delete them all! It’s always best to get the instance ID of the object you wish to destroy and use that instead!

I hope this helps you clean up your code and keep things a little more organized! Until next time, keep on making those games!


Do you have a tip or trick for GameMaker you think developers should know about? Click here to submit your suggestions!

Be sure to follow @GMDeveloper on Twitter for updates on GameMaker news, tutorials, and more tips and tricks!

2 Responses to “Tips and Tricks #1”

  1. JVI i I{ I[ says:

    I don’t understand your point at the start.

    This to me also seems incorrect:
    “// Inside Step or Alarm
    instance_destroy(myObject);”

    It should be just:
    “// Inside Step or Alarm
    instance_destroy();”

    You said it yourself. “however, instance_destroy() isn’t one of those fancy functions you can pass arguments to.” So the parentheses should stay empty.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="" cssfile="">