I make mistakes related to instances and objects

H

HUMMAN

Guest
Often i make mistakes related to, triggering one specific instance for object, or triggering all of them etc. Are there any good rule of thumbs or i will learn it by experience?

: For example below code, hovering works for any instance which is intented and IT WORKS. white shader gets applied to any instance i hover my mouse. However, when i click IT works ONLY for one specific instance. I dont understand it.
GML:
if(place_meeting(x,y,oCursor))
{      
   
        hover=true;
       
}

else hover=false;

with(oGUI)
{        if (mouse_check_button_pressed(mb_left))
{
        if(other.hover)
    {
        information_ui=true;  
    }
    else
    {
        information_ui=false;
    }
       
}
}

Here is a video clip of the situtation, i did not share other codes since i dont think it is related but if you think so please let me know.
In the video all hovers work, but left click works only for teal instance of same object.
 

TsukaYuriko

☄️
Forum Staff
Moderator
The first portion of your code checks if there is any instance of an object below the cursor.

The second part then loops through all instances individually, and makes use of the check for any instance from before.

Is the first instance a part of any instance of the object? If so, the second code will always trigger for the first instance, as you're not checking specifically for the instance currently being handled, but any instance of that object... so a click on the second one will still register when the first one checks if there was a click on any instance ;)
 
H

HUMMAN

Guest
Thanks for the clear explanation. But what i dont get is, since second part loops for all instances, why does it only trigger for the first instance? "and makes use of the check for any instance from before" I guess this is where i miss your point .

I assumed i can get used to it in the future and emerged the code as below:
GML:
if(place_meeting(x,y,oCursor))
{       
    
        hover=true;
        if (mouse_check_button_pressed(mb_left))
        {
        oGUI.information_ui=true;   
    }

}

else {
hover=false;
if (mouse_check_button_pressed(mb_left))
{
oGUI.information_ui=false;
}
}
Now they are under the same roof, they are in same scope, all/any instances decription you've made. If it worked in first block, second block should work for any instance in first block; i cant see any difference. But there is still same difference happening during execution.
 
H

HUMMAN

Guest
To anyone interested, this is my solution and works as intented. But i usually make similar mistakes and end up with these kind of ugly solutions. Would be glad if you guys have general advices, sources to look.

GML:
"oDistrict"
if(place_meeting(x,y,oCursor))
{     
  
     hover=true;
      
}

else hover=false;

"oCursor"
var district=instance_place(x,y,oDistrict);

if(mouse_check_button_pressed(mb_left))

{
    if(instance_exists(district)) {
        with(oGUI){
            information_ui=true;
        }
    }
    else
    {
    with(oGUI) information_ui=false;   
    }
    
}
 

Nidoking

Member
That solution looks perfectly reasonable to me. You're using instance_place to get the instance ID so you can check only the oDistrict instance you want, then tell all oGUI (presumably, there is only one) to display the information_ui. You don't really need to use with for that, but it's not wrong to do so.

You could clean things up a bit by setting variables directly rather than using an if and then setting the booleans inside. For example, the oDistrict event could just be
GML:
hover = place_meeting(x, y, oCursor);
The brackets are handy if you ever need to do anything else, though.
 
Top