• 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 Store script with arguments

Gigicom

Member
Hey,

I'm currently working on queued actions for NPCs.
For this, I'm using a priority queue and then add the action scripts to it. Then I have just one line of code that executes the most prioritized action script with script_execute
The problem is, that sometimes these action scripts need specific arguments. However, I can't just store a script like action(arg0, arg1) since that would just store the return value.

My question: Is it possible to store a script while keeping its arguments for later use? Or does anybody know an asset on the marketplace that can achieve this?
It would technically be possible to first somehow store the script and arguments in a list or in an array, however, that would require me to write multiple lines of code for every single action.

Thank you!
 

curato

Member
well yes you can always store anything you want to store, but it is really up to you to store and know where to get it later for your use. I mean you could always have the script save the arguments it is being passed. Maybe if you explain in a little more detail what you are trying to accomplish by storing the arguments someone may have a suggestion on how to achieve what you are looking for with a bit more of a direct answer.
 

Gigicom

Member
well yes you can always store anything you want to store, but it is really up to you to store and know where to get it later for your use. I mean you could always have the script save the arguments it is being passed. Maybe if you explain in a little more detail what you are trying to accomplish by storing the arguments someone may have a suggestion on how to achieve what you are looking for with a bit more of a direct answer.
Thank you for your reply. I think it's better if I make an example that's easier to understand but has the same problem:

Let's say I have a script called "script1" that requires 1 argument. I want to execute this script with the argument "id".
GML:
task = script1

script_execute(task, id)
This would obviously work, however, what I want to do is this:
GML:
task = script1(id)

script_execute(task)
This obviously wouldn't work since adding the "()" to the script would only give the return value of the script so it would just give an error. However, I think this should show what I am trying to achieve.
So my question is if there is a way of storing a script with its arguments in a variable and then executing the script with the specific variables?
 

YellowAfterlife

ᴏɴʟɪɴᴇ ᴍᴜʟᴛɪᴘʟᴀʏᴇʀ
Forum Staff
Moderator
The standard practice is to pack your arguments of interest into an array,
Code:
task = [script1, id];
and later do
Code:
switch (array_length_1d(task)) {
case 1: script_execute(task[0]); break;
case 2: script_execute(task[0], task[1]); break;
case 3: script_execute(task[0], task[1], task[2]); break;
...
}
 

Nidoking

Member
You've got a couple of good options. One is to write all of the scripts you want to execute in this fashion to take a single array argument (which could be an empty array), and each one knows how to parse that array for the arguments you need. Then you just store pairs of script ID and argument array, and the call always has the same format. That's probably going to be the easiest way. Another way is to parse the argument array to get the appropriate number of arguments as shown above.
 
Top