GameMaker How to apply code to only the first instance of an object?

Suzaku

Member
I have a character object in a room and a copy of the same object in the same room. When I move him to the sides both the original and the copy move together. In that case I would like to move only the first instance, not the second one. How do I make my code to apply only to the first instance of the object?
 

Gamebot

Member
Depends on how much control you will need.

1. You can loop through all instances of an object and should be able to get all the id's and store them within an array. You can then loop through the array for which character to control by that id. First character should be first on the list.

2. Create a controller/parent object that spawns each one and give them each a number (generally i in a for loop ect..) then simply check for character.num. With this you could create other instances of other objects.

3. I believe you could give each character a number in the creation event within a room. Though, the creation event I believe still triggers AFTER the create event? Therefore, you'll need any control code within step.
 

Evanski

Raccoon Lord
Forum Staff
Moderator
have the first object be a parent, have the others be childs, dont have the childs do event_inherited, make sure they have the same event but have it have some placeholder text
 

samspade

Member
I have a character object in a room and a copy of the same object in the same room. When I move him to the sides both the original and the copy move together. In that case I would like to move only the first instance, not the second one. How do I make my code to apply only to the first instance of the object?
I'm not quite sure I know what you're looking for. Do you have code in an object which you only want to apply to one instance of that object or do you have code outside of an object that you want to apply to one instance of an object? If the second, you might want to read up on these topics though:
If the first, it depends on what exactly you want to do. It may be better to not have that code in the object at all, but to move it outside of the object and into another - then see the above. If the code really needs to be in the object, then I would create a child of the object and put the more expansive code in the child.
 

Suzaku

Member
Sorry I didnt explain enough before and thank you for the answers. So basically I have 2 instances of the same object that I put in the room using the room editor. I just want to get the id of the first instance to do some specific stuff to it in a specific situation. So I tried to use the id variable of the object in a with statement but it didnt work, both instances act exactly the sameI I was hoping it would return at least the id of the first instance or anything. Then I tried using instance_find instead of id variable and now it seems to work. But now I want to know: why is the id variable not returning the id of any instance of my object?
 
Last edited:

Suzaku

Member
I used show_debug_message to see whats inside of id, and it actually returns the id of the 2 instances, 100000 and 100003, but how can the id variable hold 2 values at same time? It seems like its cycling through the instances of the same object, getting the id of each one. Is this how id variable works?
 
Last edited:

Rob

Member
I used show_debug_message to see whats inside of id, and it actually returns the id of the 2 instances, 100000 and 100003, but how can the id variable hold 2 values at same time? It seems like its cycling through the instances of the same object, getting the id of each one. Is this how id variable works?
How are you getting the id's? AFAIK instance ID's are assigned on creation. They don't change during the game.

As for your original question there's a few ways you can do it, depending on how you created the instances.

If you added them in the room yourself, you can just go into the creation code (from double clicking on the instance inside the room) and in your desired instance, do something like this:

Code:
global.chosenCharacter = id;
If you're creating them by another object, you'd do it like this:

Code:
global.chosenCharacter = instance_create_depth(....); //insert correct variables in place of the "....."'s
Plenty of built-in functions return the instance id, which is why the above code works. To see what functions will return id's, check the functions in the manual (and scroll down a bit, usually).

Whatever way you decide to use, you can then use global.chosenCharacter to do things like:

Code:
global.chosenCharacter.x -= 20; //moves the instance 20 pixels to the left

//or

with global.chosenCharacter{
   x -= 20;
}
 

FrostyCat

Redemption Seeker
id returns the currently running instance's ID. If you have 2 different instances of the same object, and in the object's code you tell it to display id, you should expect one value from first instance and another from the second instance.

Let's say you have two humans. In this situation, the object (type) is human, and you have two instances (independent examples) of it. If the behaviour implemented into the human object is that it tells you what its name is, then you should expect to to hear two distinct names (i.e. instance IDs), say "Alice" and "Bob" (functionally equivalent to the 100000 and 100003 in your scenario). The behaviour has not changed, but you get one result after another because they are distinct humans. You should NOT expect to hear "human". That's their type, not their individual identity.
 
I

immortalx

Guest
There's also something else, but I don't think it's recommended, so don't take my word on it. Instances placed in the room through the editor, get some kind of constant that holds the id. You can find it by double-clicking the instance in the room, taking note of the first field that looks like "Inst_xxxxxxxx" which is also actually editable.
I don't know how "safe" is it to use that to refer to a specific instance, at least for cases like, say, menus and buttons. I'd like to know myself from the more knowledgable guys.
 

Suzaku

Member
id returns the currently running instance's ID. If you have 2 different instances of the same object, and in the object's code you tell it to display id, you should expect one value from first instance and another from the second instance.

Let's say you have two humans. In this situation, the object (type) is human, and you have two instances (independent examples) of it. If the behaviour implemented into the human object is that it tells you what its name is, then you should expect to to hear two distinct names (i.e. instance IDs), say "Alice" and "Bob" (functionally equivalent to the 100000 and 100003 in your scenario). The behaviour has not changed, but you get one result after another because they are distinct humans. You should NOT expect to hear "human". That's their type, not their individual identity.
I see, good to know how it works, as im starting to mess with multiple instances in same object. Thank you everyone.
 

Attachments

Top