Jump to content


Photo

Dynamic Code in Studio


  • Please log in to reply
2 replies to this topic

#1 redspark

redspark

    GMC Member

  • GMC Member
  • 149 posts
  • Version:GM:Studio

Posted 02 May 2012 - 03:08 AM

I'm in the middle of writing a level editor for a small RPG. Just like RPG Maker, I want to store events with the map tile. For instance, I was just going to have certain events tied to each tile such as OnEnter and code GML script right in the editor. Then use execute_string() to run it when the event was triggered. However, Studio has removed those functions and I know the reasons why and they make sense to me. My question is: how are developers getting by without dynamic scripting support. What options are available?


For me, in other languages, I would use an array of function pointers and store in the map file the function index and the value of its arguments in an array. Then every command would just be an array reference and passing the array of parameters. But I don't think GM allows for function pointers. Does it? I suppose I could do an array of objects with a User defined event in each that is called every time to perform the event's action. However, I would think that is overkill especially if you want to have hundreds of event functions. I don't know GM well enough to know if that would be a load on the engine.

So my final thought is to use a switch style statement to find the appropriate event function. This seems to be a bit clunky and lead to a lot of unnecessary comparisons. So back to my question: How do you plan to deal with dynamic scripting in your game using Studio?

Thanks.
  • 1

#2 xot

xot

    GMC Dismember

  • Retired Staff
  • 4772 posts
  • Version:GM:Studio

Posted 02 May 2012 - 06:48 AM

You can refer to scripts using their resource ids, which is something like a function pointer. The resource id of a script is returned by using just its name without parenthesis. They can be invoked using the script_execute() function.

// Equivalent
g = gauss(0,1.66667);
g = script_execute(gauss,0,1.66667);

You still can't construct code a run-time, but script_execute() can solve most of these sorts of challenges.
  • 0

#3 redspark

redspark

    GMC Member

  • GMC Member
  • 149 posts
  • Version:GM:Studio

Posted 02 May 2012 - 11:30 AM

Oh, I thought that was HTML5 only by the description in the help file. Interesting. Is there a way to translate a string to a script id -- like a script id lookup? Or store a script id in a variable?

I think that would go a long way to allow script calls to be data driven which is what is needed for external level editors. GM's room editor is good for basic stuff but production slows when you are trying to do something that is specific to your game. An external editor can help cut down on the workload by doing some of the heavy lifting for you. But there needs to be a way to make GM's function calls, data driven for that to work nicely.


EDIT: I did some playing around and you can assign a script to a variable and then execute the variable using the script_execute() statement. For instance:

global.testfunctionid = testscript

script_execute(global.testfunctionid)

where testscript is a script with a single call to room_goto_next() acted as a wrapper for that function. Therefore I may be able to do my first idea and assign each script to an index of an array and use the index as the script identifier in the map file.

Thanks for your help.

Edited by redspark, 02 May 2012 - 05:02 PM.

  • 0




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users