calling script_execute with a variable

JOATMON picture JOATMON · Nov 24, 2012 · Viewed 16.7k times · Source

I'm using GameMaker:Studio Pro and trying to execute a script stored in a variable as below:

script = close_dialog;
script_execute(script);

It doesn't work. It's obviously looking for a script named "script". Anyone know how I can accomplish this?

Answer

Matt picture Matt · Aug 11, 2014

This question's quite old now, but in case anyone else ends up here via google (as I did), here's something I found that worked quite well and avoids the need for any extra data structures as reference:

scriptToCall = asset_get_index(scr_scriptName);
script_execute(scriptToCall);

The first line here creates the variable scriptToCall and then assigns to it Game Maker's internal ID number for the script you want to call. This allows script_execute to correctly find the script from the ID, which doesn't work if you try to pass it a string containing the script name.

I'm using this to define which scripts should be called in a particular situation from an included txt file, hence the need to convert a string into an addressable script ID!