1. Hey! Guest! The 35th GMC Jam will take place between November 28th, 12:00 UTC - December 2nd, 12:00 UTC. Why not join in! Click here to find out more!
    Dismiss Notice

checking object variable on multiple instances

Discussion in 'Programming' started by squirrelah, Aug 9, 2019.

  1. squirrelah

    squirrelah Member

    Joined:
    Mar 30, 2018
    Posts:
    30
    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: Aug 9, 2019
  2. TheSly

    TheSly Member

    Joined:
    Jan 16, 2017
    Posts:
    997
    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.
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice