Advertisement

Regarding modding: Lua method patching

Started by April 03, 2024 04:36 AM
1 comment, last by LorenzoGatti 7 months, 2 weeks ago

Coming from a modding background and having modded numerous games, I know that C# is practically one of the easiest languages to “monkey patch” thanks to its compilation to CIL, and there are even several libraries made just for this purpose, allowing for injection into and replacement of methods. I was wondering if there are similar tools available for Lua, as it can too compile to intermediary bytecode to execute on VM.

The reason I'm asking is that I want my own game to be as accessible to modding as possible. Particularly I want to allow injection and replacement of arbitrary Lua code, shipping precompiled Lua binaries with my game. If I were to ship raw Lua source code to modders, said injection and replacement would have to be textual and therefore a little awkward (keep in mind that there could be multiple modders wishing to inject their code into the same method).

So, what approach would be the most practical in this situation? Or should I just stick to C#? Thanks!

If your modders are intended to write Lua scripts, it doesn't follow that they should see further “internal” Lua code: you can publish only example mods and optional official libraries.

And when your engine executes mod scripts, there is no compelling reason to do it by monkeypatching your internal Lua scripts, and many reasons not to “allow injection and replacement of arbitrary Lua code”.

You can identify specific extension points (e.g. functions that, once after loading a level, can add, remove or alter enemies according to difficulty settings or functions that, every timestep, decide whether the player wins or loses or neither), collect all functions for each extension point from the active mods, and execute them in a well defined order and in a relatively isolated context every time the extension point is reached. Your own scripts can be much more generic and ad hoc, but they have nothing to do with mods.

Omae Wa Mou Shindeiru

This topic is closed to new replies.

Advertisement