• 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!

SOLVED Instance_ID's can't be the same right?

S

septuagint84

Guest
Hi, I created ObjTeamA and used the instance_number with instance_create, to create 5 blue square objects each time I clicked. Once I'm done clicking, I want to be able to click on my blue Squares.

What I want to do before that though is find the ID of each of my squares. However, what ever I code in blue squares object, it happens to all the blue square object.

I put in a code, global.picked=ID or Instance_position , and then draw it out, however, all the ID's of the object when I click on them are the same.

I hope that I have given enough information, that you can guide me on what to do next. How can I find the ID of the bluebox in this scenario? Why does it think I have the same ID blued box. If this is not enough information, I"ll put in the code.
 
S

Sybok

Guest
All instances of an 'object' will have the same object id, but they will have a different 'instance' id.
 
O

OrangeCrowStudio

Guest
Might be easier if you post the code you're using to create the instances. Also the code you're running within your square objs.
 

jucarave

Member
Just the result of create_instance should return the unique id of the object, maybe you're mixing the ids somewhere else in your flow.
 

NightFrost

Member
Post the code you're using when clicking the blue squares, too. With all the squares returning the same instance id, it sounds likely you're grabbing it by refering through object notation, which always returns the variables of first instance GMS discovers.
 
S

septuagint84

Guest
in OBJ_teamA I have this
Code:
if (left_click and numClick2 =2 and instance_number(obj_red)=0)
{
    instance_create_layer(x,y,"Instances", obj_red)
}
if (left_click and numClick2 =3 and instance_number(obj_red)=1)
{
    instance_create_layer(x,y,"Instances", obj_red)
}

and in OBJ_red I have this, sorry, bluesquares was something else, I have a blue square that is suppose to draw out my global.id, it's red circles I want.
Code:
mouse_enter = position_meeting(mouse_x,mouse_y, obj_red)
left_click = mouse_check_button_pressed(mb_left)
redId=instance_position(x,y,obj_red)
if obj_teamA.redcircle = true {
//find ID of each circles



    if mouse_enter and left_click and clicked=false{
        show_message(string(redId))//shows -4 if i do global.picked
    }
}
 
Last edited by a moderator:

NightFrost

Member
The code in obj_teama could be written in different way but current code should work as long as the variables are getting set correctly, and doesn't seem to be the issue.

In obj_red however, your position_meeting is using obj_red meaning hover over any instance will do, not just the current instance. Just use id instead. And since instances know their own id, it is not necessary to try and figure it with that instance_position check, you can just refer to id. That is, remove redId and just do show_message(string(id)). I don't see global.picked getting set anywhere so I can't say what would be wrong with it.
 
S

septuagint84

Guest
Thanks I tried that. So when I do that now, all the ID for each red circle is 10004.
 

NightFrost

Member
Well, instances can't share id values so either the code is always fetching from same instance, or the click is always making certain instance run its code no matter which one is clicked. How your obj_red code looks like now? Incidentally, I don't see the clicked variable getting set anywhere either, but since your debug messaging works, it seems to be always set to false.
 
S

septuagint84

Guest
Code:
mouse_entry = position_meeting(mouse_x,mouse_y, id))
and
Code:
if mouse_entry and left_click and clicked=false{
show_message(string(id))
So my first mistake, was not putting ID in both when I responded.
The second, i'm not sure if this could be a reason, but in both objects I had mouse_enter as the position meeting. When I switched the red object to mouseEntry as a variable. Everything works. The thing is, I don't know why, or I wouldn't be able to explain this. I think I should know why before I move on. So if that makes a difference, then I consider this solved. Otherwise, I got lucky.

Thanks for your help also
 

NightFrost

Member
Variables are instance scoped by default. That is, an instance can only see variables declared in its own code. They cannot see variables in other instances without referencing them through instance ID. This means if even if multiple instances have variables with identical names, they do not conflict.

You probably also are more or less aware of difference between instances and objects, but here's a quick refresh picking an idea from a more exhaustive guide: object is a template. Instances are products of that template. You can think of "human" as template and "Jane" and "John" as instances. You cannot ask "human" how old it is, but you can ask "Jane" or "John" how old they are. Each of them has an "age" variable that affects only that one instace, but which they received from the template with some default value carried in. (Caveat: when you ask GMS to tell how old a "human" is, it picks the first instance of human it can find and reports you his or her age.)
 
S

septuagint84

Guest
Thanks and that's a helpful guide to look at. I wish I saw that first.
 
Top