- Home /
Limiting the execution time of a lua script?
Hey, I'm attempting to limit the execution time of Lua scripts in my game so that players can't just do:
while true do end
and end up crashing the game with an infinite loop. I've tried doing it with Coroutines, but that didn't work because the Lua script doesn't yield execution and the game ends up freezing. I've tried doing it with Threads, but that doesn't work because my Lua functions need access to Unity API stuff like transform, and it throws errors like:
get_transform can only be called from the main thread.
Is this simply not possible in Unity, because Unity is not thread-safe? This issue is a project killer for me, any help would be appreciated. Here's my attempt at doing it with threads, if you want to see what I've already tried:
// Lua functionality
public System.Object[] Call(string function, params System.Object[] args) {
if(lua == null) return null;
LuaFunction lf = lua.GetFunction(function);
if(lf == null) return null;
System.Object[] result = new System.Object[0];
CallTimeLimit(lf, args, value => result = value);
return result;
}
public System.Object[] Call(string function) {
return Call(function, null);
}
private void CallTimeLimit(LuaFunction lf, System.Object[] args, Action<System.Object[]> result) {
// Start two threads, timer and lua execution.
// Whichever thread finishes first will abort the other one.
Thread timerThread = new Thread(CallTimer);
Thread execThread = new Thread(CallExecution);
CallTimerParams ctParams = new CallTimerParams(execThread);
CallExecutionParams ceParams = new CallExecutionParams(lf, args, result, timerThread);
timerThread.Start(ctParams);
execThread.Start(ceParams);
}
private struct CallTimerParams {
public Thread execThread;
public CallTimerParams(Thread execThread) {
this.execThread = execThread;
}
}
private void CallTimer(object parameters) {
CallTimerParams timerParams = (CallTimerParams) parameters;
Thread.Sleep(500);
timerParams.execThread.Abort();
Debug.Log("TIMER FINISHED FIRST");
luaValid = false;
}
private struct CallExecutionParams {
public LuaFunction lf;
public System.Object[] args;
public Action<System.Object[]> result;
public Thread timerThread;
public CallExecutionParams(LuaFunction lf, System.Object[] args, Action<System.Object[]> result, Thread timerThread) {
this.lf = lf;
this.args = args;
this.result = result;
this.timerThread = timerThread;
}
}
private void CallExecution(object parameters) {
CallExecutionParams execParams = (CallExecutionParams) parameters;
System.Object[] funcResult = null;
try {
if(execParams.args != null) {
funcResult = execParams.lf.Call(execParams.args);
} else {
funcResult = execParams.lf.Call();
}
} catch(NLua.Exceptions.LuaException e) {
LuaError(e.Message);
}
execParams.timerThread.Abort();
Debug.Log("EXECUTION FINISHED FIRST");
execParams.result(funcResult);
}
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Nested Coroutines Waiting for Each Other 1 Answer
Lerp to target position in X amount of time 2 Answers
How to create a continous transition between intro and loop of a song 2 Answers