[SOLVED] Handling error exceptions for assert(loadstring(str))() ?
Posted: Tue Aug 14, 2018 8:54 pm
I am trying to add scriptable lua to a console as part of a game, for example to change variables quickly or run custom lua code to edit levels etc.
I have figured out i can use something like this:
Where the console uses text input to supply the console.command variable. Eg; User types some lua code into the console, when they press return, the above code line will execute.
This allows me to enter commands directly into the game whilst it is running, such as:
Where world.gravity is a variable within the actual love2d project.
And also to help modify levels very fast, being able to type actual lua code, for example;
Essentially this is being run as;
This works fine, however, if there is a syntax error... the entire love2d application crashes to the blue error screen. How can i stop this from happening so i can instead print "error" to the console instead of crashing the entire program?
For context; This is what i am trying to achieve; https://love2d.org/imgmirrur/mEfBQYn.jpg The lines which start with DEBUG are the commands that were entered into the in-game console. Here's the project/file which i've added this to: https://github.com/Jigoku/boxclip/blob/ ... e.lua#L115
The problem is that i cannot find a way to stop the game crashing if invalid syntax is entered. Does anyone know if there is a way to do this?
I have figured out i can use something like this:
Code: Select all
local out = assert(loadstring(console.command))()
This allows me to enter commands directly into the game whilst it is running, such as:
Code: Select all
world.gravity = 2300
And also to help modify levels very fast, being able to type actual lua code, for example;
Code: Select all
for _, e in (world.enemies) do editor:remove(e) end
Code: Select all
assert(loadstring("world.gravity = 2300"))()
For context; This is what i am trying to achieve; https://love2d.org/imgmirrur/mEfBQYn.jpg The lines which start with DEBUG are the commands that were entered into the in-game console. Here's the project/file which i've added this to: https://github.com/Jigoku/boxclip/blob/ ... e.lua#L115
The problem is that i cannot find a way to stop the game crashing if invalid syntax is entered. Does anyone know if there is a way to do this?