Skip to main content

Indie game storeFree gamesFun gamesHorror games
Game developmentAssetsComics
SalesBundles
Jobs
Tags

Call functions stored within tables?

A topic by Zargy created Mar 13, 2021 Views: 364 Replies: 3
Viewing posts 1 to 3

I can see that there is no translation for Lua functions back to GML, only an API to call global functions.  I'm used to working with Moonsharp in Unity where you can just call a function inside a table. I'd just like to know why this limitation exists. Is it something that would be possible, or is it too difficult to implement currently?

Developer

The reason is that GML and Lua each have their own respective garbage collector, which complicates the process of sharing garbage-collected values between the two. This is much easier to do when everything is in the same runtime, which is the case with MoonSharp.

I was able to implement GML➜Lua referencing in the last update since Lua has a pretty convenient (although not well documented) mechanism with “cleanup” functions in custom objects (__gc), but doing it the other way around requires using GMS2.3 weak references, which I have not deeply investigated yet, and which I’ve not seen actively used by community (meaning that it’s possible that there are bugs).

A workaround would be to make a Lua-side system that would assign indexes for functions that are to be returned to GML and store them in a table for later invocation.

Fair enough. Just wondering then, what would the performance implications of creating many separate Lua states be? The main thing I plan to use this for is creating scripted cutscenes/dialogue sequences (I.E. something that I really needed coroutines for). I'm used to working with MoonSharp, so I usually encapsulate loaded scripts into their own tables so I can define variables local to the script outside the main function that executes as a coroutine. (That's just the way I'm used to doing it for the huge JRPG I'm making though, this game is a lot simpler. I'd still like control)

For this case, my next option would be to try just spinning a separate state up for each loaded script. I don't have a lot of global state that I'd need to pass between scripts. I'd just like to know how "heavy" a lua state is in this instance, and how much performance one of them would take up.

Developer (1 edit)

There are no performance implications as no logic runs unless triggered, but states do take up some memory.

However, if this is primarily for dialogues and not any of the specialized things that Lua can do, you could look into TXR? It’s 100% GML and can yield at any point by design. People have modified it to have custom syntax for dialogue trees before, and some of the recent additions to base source code (like select statements) aid with that.