GML Variable references invalid object (-4)

Carloskhard

Member
So I have this code in an object which tries to destroy other object when it collides with it.
The code is the following("colliding" is a local variable):
Code:
with (colliding){
    show_debug_message(colliding)
    instance_destroy();
}
and in the debug I can see the ID of the object is correct,but on the error message it shows this:
Code:
Pop :: Execution Error - Variable references invalid object (-4).<unknown variable>
why is saying the object was "-4" if clearly the objects was something like "100045"?
I've also tried this:
Code:
with(colliding)instance_destroy(colliding);
but the same fail happened.
However if I write this:
Code:
instance_destroy(colliding);
then it works nice.
Do someone understand what is happening?Because the strangest thing is that this inly happen with some objects of the same kind.
I really need to understand why this donesn't work
Thanks!
 
Last edited:

YellowAfterlife

ᴏɴʟɪɴᴇ ᴍᴜʟᴛɪᴘʟᴀʏᴇʀ
Forum Staff
Moderator
Usually you get that specific error when you have

... = some.variable

and "some" is -4 (noone)
I'd suggest to look at it further in debugger
 
H

Homunculus

Guest
How do you set the “colliding” variable to begin with? If it’s the collision event, you have access to the colliding instance using “other”.
Can post the full error as well?
 

Nocturne

Friendly Tyrant
Forum Staff
Admin
Is "colliding" an instance variable or a local (var declared) variable. If it is a LOCAL variable, then they should all work equally, if not then that's the issue with the first two.
 

FrostyCat

Redemption Seeker
You forgot that with blocks flip around the perspective for code inside the braces. If colliding is an instance variable, then that error is normal because the instance referenced by it doesn't have its own colliding variable.

Either you move the show_debug_message() out of the with block:
Code:
show_debug_message(colliding);
with (colliding) {
  instance_destroy();
}
Or declare colliding as a local variable so that it remains valid within the with block:
Code:
var colliding = ...;
...

with (colliding) {
  show_debug_message(colliding);
  instance_destroy();
}
 

Carloskhard

Member
Usually you get that specific error when you have

... = some.variable

and "some" is -4 (noone)
I'd suggest to look at it further in debugger
But how is possible that its telling me it's "-4" or "noone" if the message in the debugger clearly says the right ID?
Also is working for some objects but not for others like the ones created on the room and not through code.
 
Last edited:

Carloskhard

Member
Is "colliding" an instance variable or a local (var declared) variable. If it is a LOCAL variable, then they should all work equally, if not then that's the issue with the first two.
@FrostyCat and @Nocturne
I didn't forget that, Its a local variable(if it wasn't,the debugger wont be showing the right ID) but no, it's not working unless I write "instance_destroy(colliding)".
As you can see it is very very strange.
 
Last edited:

Carloskhard

Member
UPDATE:
I rebooted my PC and now its working.
Thats it, I didn't changed anything more. I don't know if maybe later the same problem will come back again so I'm not totally happy but at least now is working.
 

Nocturne

Friendly Tyrant
Forum Staff
Admin
It was quite possibly a caching issue. Whenever weird things like this happen, the first thing I do is reach for the broom and clear the cache (the button beside play/debug at the top of the IDE)... Glad it's solved, though! Was a strange one...
 

Carloskhard

Member
It was quite possibly a caching issue. Whenever weird things like this happen, the first thing I do is reach for the broom and clear the cache (the button beside play/debug at the top of the IDE)... Glad it's solved, though! Was a strange one...
That's a nice tip, thanks!
 

Yal

🐧 *penguin noises*
GMC Elder
When you say "local variable", do you mean a var variable? Because with changes the scope, so if you used an instance variable, only the calling object would have it.

So all of these should work:
Code:
//Make "colliding" a global temporary variable
var colliding = 100045;
with (colliding){
   show_debug_message(colliding)
   instance_destroy();
}
Code:
//Refer to instance variable of caller of "with" loop
with (colliding){
   show_debug_message(other.colliding)
   instance_destroy();
}
Code:
//Use ID, which is what the "colliding" variable contains anyway
with (colliding){
   show_debug_message(id)
   instance_destroy();
}
 
Top