GameMaker Need help with layer_get_all_elements() [SOLVED]

L

Lenchantin

Guest
Hey all.

We're trying to make something where, when an event is activated and a script is called, the script searches out all of the objects on a layer, they look for a specific variable inside of the variable, and if it's there, it changes the variable. The idea here is that the script disables specific code on ANY object with the variable. Here's how I have it set up:

Any object that I want to disable has this code:

Code:
// Create Event
obj_enabled = true;
Code:
// Code I want to be able to enable / disable. In this case, it's in a Left Down event.

if (obj_enabled) {
    // whatever code happens here only if obj_enabled is true
}
Now, here's the event that calls the code. In this case, if you press a button, it moves a camera, and does several other things. If I press this one button object, any object with the above code disables or enables. The important lines here are the ones with 'layerDisable'.

Code:
// if you click on a menu button...
if menuCameraDart.x == 1460 {
    menuCameraDart.x -=500;
    global.c_menuOpen = true;
    audio_play_sound(clickOn_a,0,0);
    menuTriggerButton.sprite_index = menuButtonPressed_s;
    menuTriggerButton.image_speed = 1;
    menuScreenDim.y = 0;
    layerDisable("gameObjectsTop", false);
}
else {
    menuCameraDart.x +=500;
    global.c_menuOpen = false;
    audio_play_sound(clickOff_a,0,0);
    menuTriggerButton.sprite_index = menuButtonClosePressed_s;
    menuTriggerButton.image_speed = 1;
    layerDisable("gameObjectsTop", true);
}
And here's the script that's supposed to seek out the objects on any layer specified, and is supposed to change the value to either true or false, to make the code in the first two blocks above work or not work.

Code:
// Script which does... nothing
/// layerDisable("layer name", true or false)
layerName = argument0;
tOrF = argument1;

var a = layer_get_all_elements(layerName);
for (var i = 0; i< array_length_1d(a); i++;) {
    if variable_instance_exists(a[i], "obj_enabled") {
        obj_enabled = tOrF;
    }
}
We've tried writing the code several different ways, but to no avail. The code in the script seems to pass null values. No crashes, though. At best, we get an array of several numbers, for instance 24, 15, 16, 17, when we debug. We can't separate the items in a layer with the obj_enabled value and the objects that don't.

What we need it to do:
  • Seek out any object in a specific layer that has a variable called obj_enabled
  • If it has the variable, change the variable to either true or false
  • If it doesn't have the variable -- then move along. Nothing to see here.
Anyone have any suggestions? Sorry if this reads like an epileptic spider dipped in ink walking across a piece of paper. I've been working on this for 5 hours and my brain is mush.
 
E

Ephemeral

Guest
You would need an additional check for element type to do it that way, but I don't think you actually need to be using layer_get_all_elements() for this.
What we need it to do:
  • Seek out any object in a specific layer that has a variable called obj_enabled
  • If it has the variable, change the variable to either true or false
  • If it doesn't have the variable -- then move along. Nothing to see here.
Is there a reason this wouldn't work?
Code:
/// @description Change a variable for all instances on a layer
/// @param layer The layer name or layer ID
/// @param variable_name The name of the variable as a string.
/// @param value The value to set the variable to.

var layer_id = argument0;
var v_name = argument1;
var value = argument2;

if (!layer_exists(layer_id)) return false;
if (!is_string(v_name)) return false;
if (is_string(layer_id)) layer_id = layer_get_id(argument0);

with (all)
{
    if (layer == layer_id)
    {
        if (variable_instance_exists(id, v_name))
        {
            variable_instance_set(id, v_name, value);
        }
    }
}
return true;
 
L

Lenchantin

Guest
@Ephemeral - took a little trial and error, but we got it to work with your code. We just had to be sure to add quotation marks to the first two arguments or it would crash when the event triggered. (Much to the chagrin of our other programmer, who usually works in python and C#. He's trying to learn GML to help with this project, and he's finding 'quirks' -- lol. Actually, he convinced me to move from Fusion to GMS2, and so far, it's just learning GML and some of the strangeties of the language, but he's fine with it since it's a language, and there's source control.)

Anyway, works like a charm. You are a true gent! Thank you.
 
Top