Reload script at runtime
See original GitHub issueHi
I’m writing a scripting engine for an internal tool. One thing I would like to implement is on the fly reloading when the script file changes on disk. The tool is long running and calls a member function loaded from a Lua script continuously. I need to be able to reload this script while the tool is still running. This is what i have tried:
Please excuse the Pesudo-code
if (scripts_changed) {
try
{
lua = new Lua();
chunk = lua.CompileChunk(script_file, new LuaCompileOptions() { DebugEngine = LuaStackTraceDebugger.Default });
}
catch (LuaParseException e)
{
//... Parse error. just return and try again.
}
try
{
global = lua.CreateEnvironment<LuaGlobal>();
global.DoChunk(chunk);
}
catch (Exception e)
{
// ... just return and try again
}
}
try
{
global.CallMember("Something"); //This fails after fixed runtime error.
}
catch (Exception e)
{
//... oof
}
The script_changed is just set when a *.lua file changes via FileSystemWatcher and when starting up.
- Start up. Script runs OK.
- Introduce change. Script runs OK
- Introduce parse error. Fails with parse exception.
- Fix Parse error. Script runs OK.
- Introduce runtime error. Script Fails with runtime exception.
- Fix runtime error. Script fails with cant find member function exception.
Only way to fix this is to restart the whole program. I’ve tried lua.Clear(),global.Clear(),lua = null, global = null and creating new instances. But it seems the program keeps old script data around and can’t be cleared and still fails on reload even if the error is fixed.
I’m probably missing something fundamental here. And tips on how to do this properly?
I’m Using v1.2.8. But i could find any relevant commits that my have fixed this.
Thanks for you help 😃
Here are the exceptions i’m getting in the runtime error case: ** Runtime Error Introduced ** Script changed… Reloading. Exception: Can not call nil value. Exception: – internal – at [L] main8(LuaTable _G) line main.lua:22:1
Runtime Error: Value cannot be null. Parameter name: ‘table:Scan’ not found. Runtime Error: at [C] Neo.IronLua.LuaTable.CallMemberDirect(String memberName,Object[] args,Boolean ignoreCase,Boolean rawGet,Boolean throwExceptions) line C:\Projects\NeoLua\NeoLua\LuaTable.cs:2824:8 at [C] Neo.IronLua.LuaTable.CallMember(String memberName) line C:\Projects\NeoLua\NeoLua\LuaTable.cs:2771:7 at [C] MyProgram.LuaScript.ScriptTask() line c:\bla\bla.cs
** Runtime Error Fixed ** Script changed… Reloading. Runtime Error: Failed to call ‘Scan’. Runtime Error: at [C] Neo.IronLua.LuaTable.CallMemberDirect(String memberName,Object[] args,Boolean ignoreCase,Boolean rawGet,Boolean throwExceptions) line C:\Projects\NeoLua\NeoLua\LuaTable.cs:2869:6 at [C] Neo.IronLua.LuaTable.CallMember(String memberName) line C:\Projects\NeoLua\NeoLua\LuaTable.cs:2771:7 at [C] MyProgram.LuaScript.ScriptTask() line c:\bla\bla.cs
Issue Analytics
- State:
- Created 7 years ago
- Comments:8 (8 by maintainers)
Top GitHub Comments
Extremely. Thanks alot!
My thoughts are composed in the commit 987f0927b0dafaf5f4380ad639832beb82a7b2be.
Is this helpful?