How would one make a game where players can run custom Lua/GDScript/etc scripts at runtime?

Godot Version

v4.4 (but I can switch to a newer version if needed)

Question

I’d like to include Lua modding in a game I’m making, such that players can actually write Lua scripts in-game and run them immediately. I know that Lua has been integrated into a variety of other games and platforms (Roblox, Garry’s Mod, WoW) and that there’s an extension for writing Godot editor scripts in Lua, but I don’t know if there’s any established way for supporting something like Lua modding in Godot. Does something like this exist already?

I’m open to other scripting languages as well, if something else would work better. I need to be able to provide a custom API so that player scripts can interact with game features, and also restrict the language from being able to do things like arbitrarily create new files on the user’s computer. I know that this is commonly done with Lua, but if there’s a way to run custom GDScript at runtime, or some other language like Python, I’m happy to work with that instead.

You can run dynamically constructed GDScript. Preventing malicious usage will be hard though. Depending on your needs you might consider using expressions instead.

func _ready():
	var script = GDScript.new()
	script.source_code = """
func function():
	print("CALLED THIS")
"""
	script.reload()
	var object = script.new()
	object.function()

AWESOME, I had no idea that was an option. Off the top of my head, I can probably limit a lot of malicious use by checking code before it’s run for certain banned API calls, or even making a modified version of the language which I translate into normal GDScript. Actually I’ll probably have to do the latter to make it able to interact with my game in a user-friendly manner. I guess that is going to be hard either way, but it’s something I can do so that’s a great start.

I’ll wait to see what other ideas/suggestions people have before making the thread as solved, but this is very helpful, thank you.

Similar to @normalized 's post on using GDScript to parse expressions and run scripts themselves, the (probably) mentioned lua gdextension can run lua scripts in-game just as well


There is also a RISC-V sandbox that should, in theory, support nearly any compiled language as a modding platform. Maybe not in-game scripting as you’d need an in-game compiler.

Damn, okay that’s easier than I expected. I guess I have options.

I guess my last question on this for now is regarding performance. Would runtime GDScript perform faster than runtime Lua because it’s native to Godot?