• Hello [name]! Thanks for joining the GMC. Before making any posts in the Tech Support forum, can we suggest you read the forum rules? These are simple guidelines that we ask you to follow so that you can get the best help possible for your issue.

Question - Code beta channel bug in instance_number() function

Maa2007

Member
im having troubles with this function
instance_number()

i think its returning wrong values,when used with if statment
so it's returning true, when it should be false and vice versa
or somthing like that

any body having problems with it ?
im using beta run time 2.2.2.314
 

FrostyCat

Redemption Seeker
instance_number() doesn't return a Boolean, it returns the number of instances of the given object and its descendents.

Read the Manual and make sure that your assumptions match the documented behaviour. Provide an example of the code you're using. You have to prove yourself faultless before pointing the finger at GMS.
 

Maa2007

Member
instance_number() doesn't return a Boolean, it returns the number of instances of the given object and its descendents.
Read the Manual and make sure that your assumptions match the documented behaviour. Provide an example of the code you're using. You have to prove yourself faultless before pointing the finger at GMS.

no it can return true and false if you use it with an if statment
an example:
if instance_number(enemy) > 2 { do somthing }

the above code will only excute if there is more than two enemy instances
 

FrostyCat

Redemption Seeker
no it can return true and false if you use it with an if statment
an example:
if instance_number(enemy) > 2 { do somthing }

the above code will only excute if there is more than two enemy instances
In your example, instance_number() isn't the one returning the Boolean, the > operator is.

So your complaint is actually about instance_number() returning the wrong number of instances. Now post an example proving that it's wrong.
 

gnysek

Member
OK, so what it returns in fact? Remember, that instance_number also includes children.

To check your code:
Code:
show_debug_message(instance_number());
if instance_number(enemy) > 2 { do somthing }
and then look into "output" window for result, maybe that solve your issue
 

Maa2007

Member
i just created a new project to test instance_number(), and in my tests everything seem to work right

but im 100% sure there is a bug somewhere as when i load my game on latest stable runtime (2.2.1.291) all my enviroment objects work right
and then i load the same exact project on beta channel and my enviroment objects are not showing, so there is something wrong

on beta channel i tried to see where is the bug, i could only make my enviroment objects some how show and work again by fiddling with a code that had instance_number() in it
 

gnysek

Member
if you're unable to reproduce bug, how we can help ? you're even not giving any code/output to prove issue.

Also, as mentioned above, you can compare instance_number with with():
Code:
var inst = 0;
with(object_name) {
    inst++;
}
show_debug_message(inst);
show_debug_message(instance_number(object_name));
if both numbers are same, there's no bug.
 

Maa2007

Member
if you're unable to reproduce bug, how we can help ? you're even not giving any code/output to prove issue.
Also, as mentioned above, you can compare instance_number with with():
Code:
var inst = 0;
with(object_name) {
    inst++;
}
show_debug_message(inst);
show_debug_message(instance_number(object_name));
if both numbers are same, there's no bug.

i used this code
var inst = 0;
with(obj_parent) {
inst++; }
show_message(inst);
show_message(instance_number(obj_parent));

and both massages are giving the same result, so there is no bug i guess,




in my game i have my character move to the right, and so the mountain in background will create another instance of it self to the right when the view nears its end

var right_edge = x+sprite_width-200 //only create another instance when view is about to go beyond current instance
if right_edge < global.cam_x_right_side {
if !position_meeting(x+sprite_width+hspeed+10, y+10, object_index) {//check if there is no instance at the right
if instance_number(object_index) < 2 {//if there is less than two instances, then create an instance
inst_1 = instance_create_layer(x+sprite_width+hspeed, y, layer, object_index)//create another instance to the right
inst_1.image_xscale = image_xscale
inst_1.image_yscale = image_yscale}}
}


and this code will destroy the instence to the left as it goes out of the view, so the game always have only two mountain instances to keep its frame rate stable

if x+sprite_width+hspeed < global.cam_x {
if instance_number(object_index) > 1 {
instance_destroy()}} //destroy useless instance to the left and out of view



as you see the highlighted code above, i fixed the bug by changeing the oprator < to >
like this
if instance_number(object_index) < 2 {
it became
if instance_number(object_index) > 2 {

this is why i think there is a problem in instance_number()
 

gnysek

Member
Always first try do debug and ensure your code is right :) There's a lot of ways to check it, like mentioned above - checking how many objects there already is and displaying in Output window already gave you answer, where code is wrong.

I'm glad you fixed this :)
 

Maa2007

Member
Always first try do debug and ensure your code is right :) There's a lot of ways to check it, like mentioned above - checking how many objects there already is and displaying in Output window already gave you answer, where code is wrong.
I'm glad you fixed this :)

actually just now i fixed it, it does not make sense for me logically, but it works so i kept it
my line was like that:
if instance_number(object_index) < 2 {
i changed it to:
if instance_number(object_index) < 6 {

i cant describe my whole code, it will take too much time and brain power to translate what my codes are doing, but im just putting this out there
that there might be a bug eaither in instance_number() or object_index, which are used in the codes above

my codes worked after i changd number 2 to 6, but logically 2 should have worked as it worked for 8 monthes on all game maker previous realeases
 

TrunX

Member
Sounds like you've just fixed a symptom and not the "bug" itself if there is any. It seems there are 3 additional mountains in the room or childs of it that my look different or be invisible.
You could check instance_number in the room creation code and in addition display the number every time you create a new one. In theory it will count up from 0 to 5.
 

Maa2007

Member
Sounds like you've just fixed a symptom and not the "bug" itself if there is any. It seems there are 3 additional mountains in the room or childs of it that my look different or be invisible.
You could check instance_number in the room creation code and in addition display the number every time you create a new one. In theory it will count up from 0 to 5.
this is really amazing, you seem to know exactly what im doing, without me posting much source codes

well yes i have three mountains, they don't have codes them selves but they all inherit it from one parent, and in that parent (step event) i tell it to always check if the right edge is about to enter the view, if that happen then it will create another instance of it self to the right, this way i have an endless mountains background,

now i was using
if instance_number(object_index) < 2 { create vresion of me to the right of me}
so there could only be two instances of each of the three mountains, this mean there could be maxmim 6 children of the shared parent at any time, (two instance of each of its 3 children)
and i have a code in draw event that print instance_number(parent), and as expected it never goes above 6, so everything is working

until i installed the latest beta runtime,
for some reason the mountains now never create instances of themselves, unless i changed this line:
instance_number(object_index) < 2 { create vresion of me to the right of me}
to
instance_number(object_index) < 6 { create vresion of me to the right of me}

so there is somthing wrong with instance_number() or object_index, or the parenting mechanism
 
Top