Search code examples
function-pointersgame-maker

Create a function pointer in Game Maker Studio


I would like to dynamically change the script that is executed when a index in an array is called.

The following is valid syntax:

actions[0] = script_do_something
...
actions[n] = script_do_something_else

How do I execute the scripts stored in the array? If I cant, then how else could I replicate a function pointer in GMS?

Unfortunately this doesn't work:

actions[0]()

I would like to avoid if statements as I feel they will get too long and messy.


Solution

  • you should use script_execute function.

    script_execute(actions[0]);
    

    also you may pass arguments:

    script_execute(actions[0], arg1, arg2);