1. Hello Guest! It's with a heavy heart that we must announce the removal of the Legacy GMC Archive. If you wish to save anything from it, now's the time! Please see this topic for more information.
    Dismiss Notice

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

Discussion in 'Programming' started by Suzaku, Feb 20, 2019.

  1. Suzaku

    Suzaku Member

    Joined:
    Jan 27, 2019
    Posts:
    51
    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?
     
  2. Gamebot

    Gamebot Member

    Joined:
    Jul 3, 2016
    Posts:
    191
    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.
     
  3. EvanSki

    EvanSki King of Raccoons

    Joined:
    Apr 17, 2018
    Posts:
    581
    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
     
  4. samspade

    samspade Member

    Joined:
    Feb 26, 2017
    Posts:
    2,124
    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.
     
  5. Suzaku

    Suzaku Member

    Joined:
    Jan 27, 2019
    Posts:
    51
    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: Feb 20, 2019
  6. Suzaku

    Suzaku Member

    Joined:
    Jan 27, 2019
    Posts:
    51
    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: Feb 20, 2019
  7. Rob

    Rob Member

    Joined:
    Jul 12, 2016
    Posts:
    717
    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;
    }
     
  8. FrostyCat

    FrostyCat Member

    Joined:
    Jun 26, 2016
    Posts:
    4,712
    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.
     
  9. immortalx

    immortalx Member

    Joined:
    Sep 6, 2018
    Posts:
    295
    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.
     
  10. Suzaku

    Suzaku Member

    Joined:
    Jan 27, 2019
    Posts:
    51
    I see, good to know how it works, as im starting to mess with multiple instances in same object. Thank you everyone.
     

    Attached Files:

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice