Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by shoffing · Mar 29, 2015 at 09:35 PM · c#coroutinesecuritythreadssandbox

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);
 }
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

20 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges