GMS 2 (SOLVED) Script problems with ds_list_find_value/index

I'm trying to redo my AI system, but have run into an issue.

I'm using timelines which contain a set of actions that the AI object will perform. Once the timelines resources are added to the list, I can use an alarm or a switch variable along with :

Code:
timeline_index=ds_list_find_value(ai_tml_list_move,0)
            timeline_position=0;
            timeline_running=1;
to run the set of actions.

This works, but unfortunately, timeline resources are not compatible with @YellowAfterlife 's GMLive extension (which is a must).

I then tried using scripts instead but these don't appear to be working correctly with the ds_list functions. For example, if I do:

create:

Code:
ds_list_add(ai_list_off_stand_spc,
scr_debug_msg("A"),
scr_debug_msg("B")
)
Then on a key press event:

Code:
script_execute(ds_list_find_value(ai_list_off_stand_spc,0))
The initial scripts will run (though this can be bypassed) and upon key press, the game simply exits.

Am I doing something wrong here or will script resources not work with ds_list functions (both index and value have the same problem)?

Thanks!
 
Last edited:
script_execute() requires the script index as the first parameter. The script index can be accessed by just using the script name without the parentheses ().

In your code above, what is the return value of your scr_debug_msg() script?

Whatever value it is returning is being added to the ds_list. If it's not returning anything explicitly using the [return] expression, I think it just returns 0.

So when you use script_execute, its running whatever script has an index of 0 (which at the moment will be the first script in your resource tree, as they are numbered sequentially from 0..N)

If you want to add a meaningful value to the ds_list which then can be used via script_execute, you just need to use the script name without the parentheses, which gives you the script index (an integer from 0..N, N being your total number of scripts - 1.)

At the moment, your code:
Code:
ds_list_add(ai_list_off_stand_spc,
scr_debug_msg("A"),
scr_debug_msg("B")
)
is actually running the script scr_debug_msg("A") immediately, and adding the return value of that script to the ds_list, its not adding the script index (which is required by script_execute()) itself to your ds_list.

If you already were aware of this, please ignore, but that's what it looks like is happening to me.
 
Appreciate the input!

The return value for the scr_debug_msg script shows up as -4 initially (scr_debug_msg is essentially just [show_message(argument0); return;]. Once the message dialogue shows "A" and "B" and I press enter to remove them, the return value shows 0.

Also, your explanation about script order in the res tree is exactly whats being demonstrated as the very first script in my res tree is a game end one, lol. Thought GMS was malfunctioning or something.

Is there any way to make this work? Thanks.
 
Appreciate the input!

The return value for the scr_debug_msg script shows up as -4 initially (scr_debug_msg is essentially just [show_message(argument0); return;]. Once the message dialogue shows "A" and "B" and I press enter to remove them, the return value shows 0.

Also, your explanation about script order in the res tree is exactly whats being demonstrated as the very first script in my res tree is a game end one, lol. Thought GMS was malfunctioning or something.

Is there any way to make this work? Thanks.
Instead of this:
Code:
ds_list_add( ai_list_off_stand_spc, scr_debug_msg("A"), scr_debug_msg("B") )
Do this:
Code:
ds_list_add( ai_list_off_stand_spc, scr_debug_msg, scr_debug_msg )
And then when you do this:
Code:
script_execute(ds_list_find_value(ai_list_off_stand_spc,0))
script_execute() will run the script as you are now giving it the script index value correctly.

However, you will then have a problem - how to pass parameters to the script if it requires them (e.g. "A" and "B")

Off the top of my head, when you add a script to the ds_list, you could instead add multiple values.

- The script id
- The number of parameters
- The parameters, if any.

e.g.
Code:
ds_list_add( ai_list_off_stand_spc, scr_debug_msg, 1, "A") )
Then, when you process a script, you need to read the script index, the number of parameters, then grab the parameters out of the list if there are any.

Code:
var _script_index = ds_list_find_value(ai_list_off_stand_spc,0);
ds_list_delete(ai_list_off_stand_spc, 0);
var _num_params = ds_list_find_value(ai_list_off_stand_spc,0);
ds_list_delete(ai_list_off_stand_spc, 0);


switch(_num_params)
{
    case 0:
         script_execute(_script_index);
    break;

    case 1:
        script_execute(_script_index, ai_list_off_stand_spc[| 0]);
    break;
 
    case 2:
        script_execute(_script_index, ai_list_off_stand_spc[| 0], ai_list_off_stand_spc[| 1]);
    break;
  
   case 3:
        script_execute(_script_index, ai_list_off_stand_spc[| 0], ai_list_off_stand_spc[| 1], ai_list_off_stand_spc[| 2]);
   break;

   default:
        show_debug_message("You sure have a lot of parameters - unable to process till add more cases");
   break;
}

// Remove parameters from ds_list
for ( var i = 0; i < _num_params; ++i )
{
    ds_list_delete(ai_list_off_stand_spc, 0);
}
This is a bit ugly and quick, just to give you some ideas.
 

YellowAfterlife

ᴏɴʟɪɴᴇ ᴍᴜʟᴛɪᴘʟᴀʏᴇʀ
Forum Staff
Moderator
@IndianaBones

The fix you've posted is essentially what I needed. A way to access a set of actions which can be added to individual "AI types" via ds_lists. I don't think I'll necessarily require setting arguments, but all the same, it is definitely something important to keep in mind so I've bookmarked your suggestion.

@YellowAfterlife

Custom timelines are an interesting idea. I use something similar, but I'll probably use your system instead. Thanks for sharing this!
 
Top