1. Hello Guest! It's with a heavy heart that we must announce the removal of the Legacy GMC Archive. If you wish to save anything from it, now's the time! Please see this topic for more information.
    Dismiss Notice

Windows If statement spawning multiple objects

Discussion in 'Programming' started by matthulm, Dec 4, 2019.

  1. matthulm

    matthulm Member

    Joined:
    Dec 3, 2019
    Posts:
    7
    Hello,

    I can't quite get my head around how this 'if' statement

    I'm currently setting up a settings menu that will create different button objects, then once I close the settings menu, it will destroy those buttons. However, the 'if' statement keeps creating the buttons every step.
    Code:
    /// @description Insert description here
    // You can write your code in this editor
    if settings = 1
    {
        if room = rmmenu
        {
            instance_deactivate_object(oplay);
            instance_deactivate_object(oleader);
            //instance_deactivate_object(osettingsbut);
            if !instance_exists(osoundbut)
            {
                instance_create_layer(room_width/2+300,room_height/2-60,"INSTANCES",osoundbut);
            }
            if !instance_exists(omusicbut)
            {
                instance_create_layer(room_width/2+300,room_height/2+60,"INSTANCES",omusicbut);
            }
        }
     
        if room = rmlevel
            {
            instance_deactivate_layer("Instances");
            if !instance_exists(osoundbut)
            {
                if instance_create_layer(room_width/2+300,room_height/2-60,"INSTANCES",osoundbut)
                {
                    show_debug_message("Sound Button Created")
                }
            }
            if !instance_exists(omusicbut)
            {
                instance_create_layer(room_width/2+300,room_height/2+60,"INSTANCES",omusicbut);
            }
        }
    
    The 'if' statement is located in a "Step" event, and I've set it so the buttons will only spawn if the button does not already exist. I've used this technique before and it has worked fine but for some reason it's having issues now

    The settings button works fine on the menu, but not the level room

    Any help would be much appreciated

    EDIT 1: I've set an alarm to delay the instance_create_layer(); and that has not worked

    EDIT 2: Turns out I was accidentally destroying my controller object which handled the code to destroy the objects
     
    Last edited: Dec 4, 2019
  2. TsukaYuriko

    TsukaYuriko Q&A Spawn Camper Forum Staff Moderator

    Joined:
    Apr 21, 2016
    Posts:
    1,766
    The only way for instance_exists to fail catching that an instance exists is when its target is deactivated (as deactivated instances will not count as existing or be taken into account by instance_number).

    You're deactivating the entire layer. ;)
     
  3. matthulm

    matthulm Member

    Joined:
    Dec 3, 2019
    Posts:
    7
    Yup you're exactly right, I discovered that I hadn't moved the controller object when I reverted from instance_deactivate_region(); to instance_deactivate_layer();
     

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