instance exist, destroy

Chaser

Member
Im a bit baffled as to why my code isn't working as i think it should, think i have got my code the wrong way round or what not, so could someone look at this and enlighten me.

Basically an object is pushed into a 'Zone', which is the margin of an object, whilst the object is in this zone i want to create an instance:
step event
if (place_meeting(x,y,instance1))
{ instance_create(x,y,instance2}
if (!place_meeting(x,y,instance1) && (instance_exists(instance2)
{
with(instance2)
{
instance_destroy()
}
^^ this seems to work fine, but because its in the step event i don't want it to keep creating the instance every second so i amended the code to read this:


if (place_meeting(x,y,instance1))
{
if (!instance_exists(obj_instance2)
{
instance_create(x,y,instance2}
}
}
if (!place_meeting(x,y,instance1) && (instance_exists(instance2)
{
with(instance2)
{
instance_destroy()
}

So, whats happening now is as soon as the instance2 is created it is destroyed. I just cant figure it out as to why its doing it, im clearly missing something, in my head its, hey when this collision happens and this object isn't there, then created it, and if the collision isn't happening anymore but the object remains then get rid of it.
sorry for my stupidness, but i need help. :)
 

Attachments

Chaser

Member
ok, this is working, but whats happening is, when the object is being created, i have set the x and y coordinates to be on top of another object in its step event, so x = object.x, y = object.y, which is fine all for about a split second, then it chucks the object 100 pixels to the right off the screen, why is it doing that? there is nothing in my code that i can find that tells it to do this. there are three objects that do what i need it to do absolutly fine, but this one just doesn't want to play nice. they are all the same code other then the x & y cordinates changing in the step event.
il keep checking....
 

Chaser

Member
im done with this now, its just driving me mad, i mean c'mon. if variable equals 1, create_instance_depth, and it wont even do this instruction. arggghhhhhhh. enough is enough i think.
 

Nocturne

Friendly Tyrant
Forum Staff
Admin
In the original code you use "obj_block.x" and obj_block.y". How many instances of obj_block are in the room? If it's more than one, then GMS can't know which obj_block you are referring to, and so chooses one and uses that, and it may not be the one you think it should be! This would transport the instance to another part of the room, as you describe. To resolve this use the actual ID of the object where you want to position it.
 

Chaser

Member
Thanks, I think I was just overthinking it , and I had been on the computer solidly for like 12 hours, I should really take better break intervals.sorry to be a time waster.
 
Top