Why do instance_exists(-1) returns true in v1.4.1757 ?

Ronchon

Member
Hi,
-1 is not a valid instance ID ( or an object ID either ) and cannot be, considering all ids are positive numbers.
Yet when i check instance_exists(-1) , it returns true in the new version, while it returned false in 1.4.1657 and previous versions.

Why is that ? Am i missing something ?
 

Ronchon

Member
It used to return false.
Is it also normal that instance_exists(0) returns true ?
Is 0 standing for something ?
 

jo-thijs

Member
Well, strictly speaking, the documentation doesn't specify what instance_existing should return.
It would sound logically to me though that it returns true,
as instance_exists returns true if and only if there exists an active instance of the provided type.
As there is usually always an active instance executing the code.

And yes, 0 stands for the first object in the resource tree.
 

FrostyCat

Redemption Seeker
Well, strictly speaking, the documentation doesn't specify what instance_existing should return.
It would sound logically to me though that it returns true,
as instance_exists returns true if and only if there exists an active instance of the provided type.
As there is usually always an active instance executing the code.

And yes, 0 stands for the first object in the resource tree.
You have got to be kidding me with that comment on the documentation.
The Manual entry on instance_exists() said:
Returns: Boolean

Description
This function can be used in two ways depending on what you wish to check. You can give it an object_index to check for, in which case this function will return true if any active instances of the specified object exist in the current room, or you can also supply it with an instance id, in which case this function will return true if that specific instance exists and is active in the current room.
This is not my definition of "doesn't specify what instance_exists() should return".
 

TheouAegis

Member
To clarify: If the argument is less than 0, GM treats the argument as one of the keywords noone, self, other, and whatever other one I'm missing. If the argument is less than 100000, GM treats the argument as an object_index and will then check if there are any instances of that object in the room. If the argument is greater than 100000, it will look for a specific instance.

The fact that you're using -1 here and asking about -1 makes me wonder what our code is. If I am going to ever potentially have -1 passed as an id or object_index into a function, it's always a value stored in a variable, in which case I just check if the variable is true (although GMS has changed how -1 is handled before, too).
 

rwkay

GameMaker Staff
GameMaker Dev.
Please never use the constants -1 for self, this will NOT work properly on HTML5 or YYC and is generally not supported - if you want to use self then use the self constant not what you think it should be.

It is bad practice to use magic numbers like this as it will not work if we change the underlying mechanisms (which we reserve the right to do)

Russell
 

jo-thijs

Member
@FrostyCat,

I'm not kidding.
You're taking what I said out of context.
I did not say the documentation does not specify what instance_exists generally returns,
I said it does not specify what instance_exists(-1) returns.

It says what it returns if you give an object index or an instance id,
but it doesn't say what it returns if you give it the keyword self (or any keyword for that matter).

If you believe I've got that wrong, please do point out where exactly it is stated what it returns.
 

Yal

šŸ§ *penguin noises*
GMC Elder
I always use my own constant NONE (value -12341 since it's easier to spot in the debug output than hexadecimal $deadbeef, another commonly used value) for all cases like this... mainly because I have quite a few cases where small negative numbers are used, and I don't want those to end up being equal to the GM constants yet still want the same undefined value everywhere. (I got this habit before undefined became a thing, and it's much easier to do an if-not-equal check than a if-is_undefined check anyway, since you don't need to get off the home row while typing <>, making casual use easier).

Also, 0 doesn't just stand for the first object in the resource tree. Also the first sprite, sound, background, timeline, path, font, room, ds_grid, ds_list... the first of every resource. It's good to be aware of this, especially when naming resources, because your code won't complain if you, say, pass an object ID into draw_sprite()... it'll just do something other than what you'd want it to.
 
P

ParodyKnaveBob

Guest
I did not say the documentation does not specify what instance_exists generally returns,
I said it does not specify what instance_exists(-1) returns.

It says what it returns if you give an object index or an instance id,
but it doesn't say what it returns if you give it the keyword self (or any keyword for that matter).

If you believe I've got that wrong, please do point out where exactly it is stated what it returns.
The manual said:
instance_exists()
Returns:
Boolean

Description
This function can be used in two ways depending on what you wish to check. You can give it an object_index to check for, in which case this function will return true if any active instances of the specified object exist in the current room, or you can also supply it with an instance id, in which case this function will return true if that specific instance exists and is active in the current room.
+
The manual said:
self
Code:
Keyword   Description                                                  value
self      The instance which is executing the current block of code.   -1
self can be used to identify the calling instance of the current block of code. It always returns the value of -1 which GameMaker: Studio interprets as the unique ID for the instance. In general you should never need to use this keyword, as you can do the same task more efficiently and appropriately using other keywords or functions, and is maintained for legacy support reasons.
==
The manual essentially said:
instance_exists(self)
Returns:
Boolean

Description
This function, given this supplied argument which GameMaker: Studio interprets as the unique ID for the calling instance of the current block of code, will return true if the current block of code is called from any instance (because if an instances calls it, then the instance necessarily both exists and is active in the current room).
Otoh, I don't recall if it returns false or presents some unexpected behavior when using instance_exists(self) in Room Creation Code, and I'm not in the best position to test it right now (but I very well might a little later, along with instance_exists(id) of course), but I hope that answers the immediate concern anyway: what GM returns upon supplying self to instance_exists() according to the manual.

Regards, ~nod~
Bob
 

jo-thijs

Member
I was going to give a counter example, but it seems I mixed 2 things up.
If you try:
Code:
show_message(collision_point((bbox_left + bbox_right + 1) * 0.5, (bbox_top + bbox_bottom + 1) * 0.5, self, false, false));
This will always return false.

The documentation states:
Collision point checks the point specified by the arguments x1,y1 for a collision with any instance of the object specified by the argument "obj".
So, I'm guessing this is actually more of a bug.

Then, if you take a look at the documentation of position_meeting, you will see the document specifically states that it will also work for the keywords all and other,
even though this function seems to work just fine with the keyword self as well.

Anyway, it is not true that an inactive instance cannot execute any code.
If you use instance_deactivate_object(self), then the code below that will still be executed with the executing instance being deactivated.
instance_exists(self) returns true before deactivation and false afterwards.
 

TheouAegis

Member
Well, the deactivation is merely flagging a variable. So the variable is flagged once you call instance_deactivate_object(self). The object still exists and it will not be able to execute code once the current event has ended. Deactivation checks are clearly only handled any time the instance needs to be read, such as at the beginning of an event or when a function needs to read from it.

with instance
{
if deactivated continue;
....
}

It's similar to destroyed objects.
 

TehCupcakes

Member
Presumably you have some variable tracking an instance_id. The default value you have set is -1 to indicate it is "not set", and then you set it to an instance id when the time comes. The short answer is: This is bad practice. Your default value should be "noone", rather than -1 to ensure proper behavior.
 
Top