• Hey Guest! Ever feel like entering a Game Jam, but the time limit is always too much pressure? We get it... You lead a hectic life and dedicating 3 whole days to make a game just doesn't work for you! So, why not enter the GMC SLOW JAM? Take your time! Kick back and make your game over 4 months! Interested? Then just click here!

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

R

renfors

Guest
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?
 
D

dannyjenn

Guest
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).
 
R

renfors

Guest
"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".
 

CloseRange

Member
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
 
Top