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

New instances created instead of being updated with "with".

Discussion in 'Programming' started by renfors, Apr 14, 2019.

  1. renfors

    renfors Member

    Joined:
    Nov 28, 2016
    Posts:
    4
    Hi

    I'm creating an object with instance_create_layer that I later want to update with "with".
    If I do the update in the same loop-iteration, the object is moved. However, a new instance is created if I do the update in a later iteration.

    if(stepNumber == 1)
    {
    tilePosX = 50
    tilePosY = 50
    inst = instance_create_layer(tilePosX, tilePosY, "Instances", object5)
    }
    if(stepNumber == 2)
    {
    tilePosX = 100
    tilePosY = 100
    with(inst)
    {
    y = other.tilePosY
    x = other.tilePosX
    }
    }
    stepNumber++

    If I change the second IF-statement in the code above to "stepNumber == 1", the instance is correctly moved instead of having a new one created in the new position. How can i update instances with "with" without having them duplicated?
     
  2. dannyjenn

    dannyjenn Member

    Joined:
    Jul 29, 2017
    Posts:
    572
    As long as you don't change the value of inst, it should just work.

    What are you trying to do exactly? If you need to do this to multiple instances, you'll need to keep track of them in an array or data structure (so that you don't lose the pointer).
     
  3. renfors

    renfors Member

    Joined:
    Nov 28, 2016
    Posts:
    4
    "As long as you don't change the value of inst, it should just work.": I think so too but the results differ when i changee the second IF-statement.

    "you'll need to keep track of them in an array or data structure": Yes, I'm using an array in the real code since that are a few hundred instances but I decided to keep as simple as possible in the forum. I tried this example code and it has the same problem as the real one, another instance is created if the instance is updated in a later iteration with "with".
     
  4. CloseRange

    CloseRange Member

    Joined:
    Jul 2, 2016
    Posts:
    905
    the with statement will NOT duplicate your objects. Never has.
    Chances are you created 2 objects at some point in the same position. Because they are in the same position it looks like one object only (the other is hiding behind the first)
    then the with statment only moves one of the objects (not both) so it looks like it was duplicated when in reality you are just seeing the object that was behind the first.

    So why would chaning that second if 'fix' it?
    Because you end up moving the object the exact same moment it's created so you end up moving both objects as soon as they are created (so you never get a chance to see one not behind the other)

    You said you were using an array? Show us the real code, don't worry about it being too complicated as we are all capable of reading long bits of code.
    Though you might want to actually use a code block in the forums
    Code:
    like this
     

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