• Hey Guest! Ever feel like entering a Game Jam, but the time limit is always too much pressure? We get it... You lead a hectic life and dedicating 3 whole days to make a game just doesn't work for you! So, why not enter the GMC SLOW JAM? Take your time! Kick back and make your game over 4 months! Interested? Then just click here!

Windows If statement spawning multiple objects

M

matthulm

Guest
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 by a moderator:

TsukaYuriko

☄️
Forum Staff
Moderator
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. ;)
 
M

matthulm

Guest
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. ;)
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();
 
Top