GML Creating variables for copied instances

Discussion in 'Programming' started by RisingKane, Jun 4, 2017.

  1. RisingKane

    RisingKane Guest

    Hi everyone!

    Let's say I have an object in my room. Then, through some way, I copy this object, creating a new instance of it. I can keep copying it if I want, ending up with multiple instances of that object. What I would like to know is, if it's possible to address a new variable to the copied instance, as soon as it's copied from the original object, and then, through some input text box, modify the value stored in that variable. Also, is there some way to make this work for each copied instance? The final result would be multiple instances of the same object with one variable storing different values for each copied instance.

    Thanks!
     
  2. Badger

    Badger Member

    Joined:
    Mar 1, 2017
    Posts:
    97
    Have you tried using the "with" function?
    like -
    Code:
    with(newInstance)
    {
     newVariable=somethingTotallyUseful
    }
     
  3. RisingKane

    RisingKane Guest

    Yes, I tried doing that. It works for the first copied instance, but as soon as I copy more, I lose control of them. I'm trying to add the copied instance's IDs to an array or a ds_list, but it's not working. Thanks for your response Badger!
     
  4. Badger

    Badger Member

    Joined:
    Mar 1, 2017
    Posts:
    97
    Have you tried using the instance_find function?
    You can add id's with that like this -
    Code:
    var num=instance_number(yourObject);
    var i;
    for(i=0;i<num;i++;)
    {
      var inst=instance_find(yourObject,i);
      with(inst)
      {
      //your code
      }
    }
    I havent tested that, but if I remember correctly thats how u do it
     
    Last edited: Jun 6, 2017
  5. Perseus

    Perseus Not Ragarnak Forum Staff Moderator

    Joined:
    Dec 29, 2016
    Posts:
    953
    How are you copying the instances? Please post all the relevant code, since that information is not enough to understand what's going on.
     
  6. JackTurbo

    JackTurbo Member

    Joined:
    Oct 19, 2016
    Posts:
    828
    When you say copying, do you mean creating? If so just keep a variable with what ever is the newest created object in it as you create them.

    Code:
    latestInstance = instance_create(x, y, obj);
    
    latestInstance.variable = "what ever you want";
    
    
     
  7. FrostyCat

    FrostyCat Member

    Joined:
    Jun 26, 2016
    Posts:
    4,796
    There is such thing as copying an instance, see instance_copy(). It also returns an instance ID.

    Judging from the way the opening post is written, the root of the problem is --- yet again --- ignoring the difference between instances and objects. At least a dozen related topics appear on the Q&A every week because mainstream GML education fails to teach novices the difference.

    It's not what people who know their stuff do. Your instance_find() loop is redundant and slow.
    Code:
    with (yourObject) {
      //...
    }
    See this post for why you should never write another loop like that again.
     
  8. Badger

    Badger Member

    Joined:
    Mar 1, 2017
    Posts:
    97

    Good info! Thanks for that. Also, it might be useful for you to check out https://www.quora.com/How-do-I-not-end-up-being-a-rude-teacher
    and other posts like it.
     
    Last edited: Jun 6, 2017

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