0

I'm using LuaInterface for scripting in a game I'm making.

The scripting works quite nice, but somehow I'm getting a lot of random exceptions.

I've narrowed the problem down to where I call lua functions from C# code, which are invoked from another thread, handling networking with the gameserver.

I think what is happening, is that the lua code is being run, and lua functions are called from C# from another thread, which causes the random errors.

How I'm calling the functions from C#:

function.Call(message);

function is of type LuaFunction and message is a custom object.

Errors I'm getting:

  • A first chance exception of type 'LuaInterface.LuaScriptException' occurred in LuaInterface.dll
  • Error running lua: function
  • A first chance exception of type 'LuaInterface.LuaScriptException' occurred in LuaInterface.dll
  • Unable to open script: mainmenu/console

Could it have to do with running lua functions from threads other than the thread the LuaVM was created on?

Cpt. eMco
  • 565
  • 4
  • 13
  • "Unforeseen exception", ok. "Unknown exception", ok. "Random exception" . . . . . . – Tipx Aug 30 '11 at 13:30

2 Answers2

1

Lua is not thread safe. You either need to have a Lua state per thread and call the thread specific state, or place locks around any Lua calls.

BMitch
  • 231,797
  • 42
  • 475
  • 450
0

The problem was indeed threading, thanks to BMitch for pointing that out.

I've fixed it by letting the message thread hand functions with parameters to the main thread, which then calls them in the update method.

Cpt. eMco
  • 565
  • 4
  • 13