Check the value of a variable in a specific instance

M

MoonMonky

Guest
Hi there!

After reading a lot of thread about similar problem I still can't figure it out how can I check a value of a specific instance and do an action.

That's my code. It works but it's always goes to Option room and never in Atrio room.

Code:
if mouse_check_button_pressed(mb_left) {            
    if position_meeting(mouse_x, mouse_y, menu_select) {
        if (menu_select.menu_selection == 2)
        {
room_goto(option);
        }

        if (menu_select.menu_selection == 1)
        {
room_goto(atrio);
        }
}}
Do you have any idea where I'm wrong? Thanks!
 
M

MoonMonky

Guest
Where are you setting the variable menu_selection?
Thanks for answer and sorry for non-clear question.
I defined it in "variable definitions" menu of the object. And then I modified the value ("1" and "2") in instances variables.
 

FrostyCat

Member
If you've done any real research on this topic, you should have seen at least one place where I responded demanding that the poster read these articles:
The first article said:
NEVER access a single instance by object ID if multiple instances of the object exist. This includes attempts to reference or set object.variable(which is inconsistent across exports) and using with (object) to apply actions to it (this encompasses all instances of the object instead of just the one you want). Verbally, "Dog's colour" makes sense with one dog, but not with multiple dogs.
...
Collision-checking functions: instance_place() and instance_position() are the instance-ID-oriented analogues of place_meeting() and position_meeting(). Functions that start with collision_ all return instance IDs. Note that these functions return noone upon not finding a collision, so be sure to account for this value.
The second article said:
Meeting vs. instance
When you use a function that has meeting in its name, it just tells whether there is an object and returns either true or false. On the other hand, functions those named instance returns the colliding instance's id, or the special value noone (-4) when there is no collision.
Given the above, it's not difficult to derive this:
Code:
var inst_menu_select = instance_position(mouse_x, mouse_y, menu_select);
if (inst_menu_select != noone) {
  switch (inst_menu_select.menu_selection) {
    case 1:
      room_goto(atrio);
      break;
    case 2:
      room_goto(option);
      break;
  }
}
 

Slyddar

Member
As Frosty has so elegantly stated, you need to let GM know which menu_select object you are referring to. Even though you did a position_meeting, it doesn't know that you are then referring to that one when accessing the menu_selection variable. Only by using instance_place or instance_position can you obtain the id of the collided object, and then, as his example shows, store the id in a local variable, and then refer to it in order to access that particular instance.
 
M

MoonMonky

Guest
If you've done any real research on this topic, you should have seen at least one place where I responded.
Thank you!!

I understand the logic behind that and I'm checking out your guides for newbie, really thank you guys!!
 
Top