checking object variable on multiple instances

I have two objects and three instances

Object1
O1Instance1

Object 2
O2Instance1
O2Instance2

O1Instance one is checking a variable on Object 2

if(Object2.Variable==true)
{
Run code

}

On instance 1 the variable is true, on instance 2 the variable is false.

What is supposed to happen here? I assumed the code would run once, but it doesn't seem to be doing that in what i am working on.

Below is the code. The second part of the code is just so it doesn't check for the variable that doesn't exist yet. Effectively once the oBuilderPlacer is created and is clicked somewhere on the map, the variable "Building_placment" turns to false. The code is there so you won't spawn multiple buildings on your cursor when pressing the button to spawn them.


Code:
if(Unit_selected==true&&global.keyboard_b_press==true&&instance_exists(oBuilderPlacer))
{
    if(oBuilderPlacer.Building_Placement==false)
    {
        instance_create_depth(((mouse_x div 32)*32) + 16,((mouse_y div 32)*32) + 16,-1000,oBuilderPlacer);
    }
 
}
if(Unit_selected==true&&global.keyboard_b_press==true&&!instance_exists(oBuilderPlacer))
{
    instance_create_depth(((mouse_x div 32)*32) + 16,((mouse_y div 32)*32) + 16,-1000,oBuilderPlacer);
 
}

Edit: i figured it out, i just have to use with(object), not sure why i didn't do that to begin with.
 
Last edited:

Slyddar

Member
if(Object2.Variable==true)
{
Run code

}
The reason is doesn't run once, like you assume, is you are referring to an object.variable referece, and Gamemaker will only run that against one instance of that object. If you want to run the check against multiple instances you need to use the 'with' function.

EDIT: Oops, sorry, just noticed you have solved it. Maybe change your title to [SOLVED] at the end.
 
Top