Load lua code dynamically, safely

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
User avatar
kexisse
Citizen
Posts: 56
Joined: Wed Jun 13, 2012 2:52 pm

Load lua code dynamically, safely

Post by kexisse »

Is it possible to load a Lua file, and execute it in a safe environment where an error would not cause the main Löve instance to crash?

For example you have a file behavior.lua, you run your main Löve program, edit behavior.lua, hit a button to reload the file, and see changes in the game.

Is this possible?
User avatar
dreadkillz
Party member
Posts: 223
Joined: Sun Mar 04, 2012 2:04 pm
Location: USA

Re: Load lua code dynamically, safely

Post by dreadkillz »

You can run coroutines like that. Any error in a coroutine will return it to the resume call.

http://www.lua.org/pil/9.1.html

Code: Select all

t = coroutine.create(function () return nil + 1 end)
print(coroutine.resume(t)) --> false, "error message"
EDIT: I forgot about the debug library. Try using that as I believe there's support for protected calls.
EDIT2: Silly me, see pcall in the PiL: http://www.lua.org/pil/8.4.html
User avatar
bartbes
Sex machine
Posts: 4946
Joined: Fri Aug 29, 2008 10:35 am
Location: The Netherlands
Contact:

Re: Load lua code dynamically, safely

Post by bartbes »

You want pcall, possibly in combination with setfenv.
Post Reply

Who is online

Users browsing this forum: No registered users and 55 guests