• 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!

GameMaker [Solved] Grabbing+running a script assigned by an array in a list

Hi,

I've run into a small problem which I thought wouldn't be an issue, but can't seem to get around. I'm simply trying to access and run a script inside an array, which itself is inside a list. The variable name of the array isn't being recognized even though its been declared as an instance variable. Also, please note that I don't actually want to call the list itself. The list is simply being mentioned as a caveat.

The relevant code is as follows (list related elements included):

Create:

Code:
//Create lists
ai_list_stand_mel_source=ds_list_create();
ai_list_move_source=ds_list_create();

//Array+scripts
ar_ml_grab[0] = scr_ai_stand_mel_grab;
ar_ml_grab[1] = script_get_name(ar_ml_grab[0]);
ar_ml_grab[2] = ["some argument value"]

ds_list_add(ai_list_stand_mel_source,
ar_ml_grab,
)

ar_mv_adv_mel[0] = scr_ai_move_adv_mel;
ar_mv_adv_mel[1] = script_get_name(ar_mv_adv_mel[0]);
ar_mv_adv_mel[2] = [noone]; //added in case this arg is needed


ds_list_add(ai_list_move_source,
ar_mv_adv_mel,
)
I'm running the script "scr_ai_move_adv_mel". Inside the script:

Code:
var scr_end = 1000;
var range_x = 48;
var mel_choice = noone;

if ai_mom<scr_end
    {
    if feed_x<=range_x
        {
        mel_choice=choose("mel")//,"gr")
     
        if mel_choice=="mel"
            {
            var sel = ar_ml_grab[@ 0]
            script_execute(sel[0],sel[2]);
            }
        }
    }
This part:

Code:
var sel = ar_ml_grab[@ 0]
            script_execute(sel[0],sel[2]);
Is where I'm having trouble. I'm trying to grab and run the script "scr_ai_stand_mel_grab" which is inside the array "ar_ml_grab[0]" along with its argument variable inside ar_ml_grab[2]. I went through the manual and found that accessors may be needed to access the script+arg inside the array. With the above code, I'm given an error which says that the variable ar_ml_grab is not an array, which is strange because it clearly is, as per the create event. But obviously I'm doing something wrong here with the syntax. The manual's example for array accessors doesn't seem too helpful in my case either.

Thanks in advance for any guidance.
 
Last edited:
The syntax is not wrong. My guess is that somewhere you have put
Code:
ar_ml_grab = something;
in your code. This will destroy the array and ar_ml_grab will revert to a normal variable, which will then throw an error when trying to be accessed as an array. Open Edit > Search and do a search for ar_ml_grab and see what the results are in the Search Results tab in the bottom. You can also check this by putting a breakpoint at:
Code:
var sel = ar_ml_grab[0];
And checking what the value of sel is in the debugger (as a side note, the @ is not needed to access the variable, that accessor is used to make sure you are editing the original array, rather than a copied array (most useful in scripts, but also in other scenarios). Since you're only reading a value, you can access the array like normal (ar_ml_grab[0]).
 
Thank you both.

@Ouroboros I tried your suggestion and it works.

@RefresherTowel I was not able to find anywhere the array being turned into a variable. The above fix seems to prove this. Not sure why it didn't work with the syntax I provided if it indeed was correct. I haven't used the debugger yet, but will look into the issue a bit later. Thanks for the info on the accessors.
 
Hahaha, actually, reading over it again, the syntax was wrong:
Code:
var sel = ar_ml_grab[@ 0]; // Sel is now the first variable stored in your array: scr_ai_stand_mel_grab
script_execute(sel[0],sel[2]); // Now you try to grab scr_ai_stand_mel_grab[0]...Since scr_ai_stand_mel_grab is a script name, not an array, it throws an error saying that it's not an array.
The correct syntax would be:
Code:
var sel = ar_ml_grab[0];
var sel2 = ar_ml_grab[2];
script_execute(sel,sel2);
 
Top