question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Reload script at runtime

See original GitHub issue

Hi

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.

  1. Start up. Script runs OK.
  2. Introduce change. Script runs OK
  3. Introduce parse error. Fails with parse exception.
  4. Fix Parse error. Script runs OK.
  5. Introduce runtime error. Script Fails with runtime exception.
  6. 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:closed
  • Created 7 years ago
  • Comments:8 (8 by maintainers)

github_iconTop GitHub Comments

1reaction
PatrickKursawecommented, Jun 15, 2020

Extremely. Thanks alot!

0reactions
neolithoscommented, Jun 11, 2020

My thoughts are composed in the commit 987f0927b0dafaf5f4380ad639832beb82a7b2be.

Is this helpful?

Read more comments on GitHub >

github_iconTop Results From Across the Web

debugging - reload the currently running python script
First, the "one-liner" that you need to insert in any script you want to refresh. with open(os.path.dirname(__file__) + os.sep + 'refresh.py', ...
Read more >
Reloading Script Assemblies takes a long time, and keeps ...
On 2020.2.21f on Windows, and Reloading Script Assemblies happens every time I compile, and is now taking 40 seconds each time. With script....
Read more >
runtime.reload() - Mozilla - MDN Web Docs
runtime.reload(). Reloads the extension. If there are any pending updates for the extension, that it has deferred by listening to runtime.
Read more >
Fast Script Reload - 1. Play 2. Make change 3. See results
Fast/Live Script Reload. Tool will allow you to iterate quicker on your code. You simply go into play mode, make a change to...
Read more >
Fast Runtime Iteration on Scripts with no recompile ...
I'm looking at improving my development workflow within Unity. The compile-time and domain reload gets me annoyed every time.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found