Esempio n. 1
0
        /// <summary>
        /// Initialises the Lua environment and compiles the Lua string for execution later on.
        /// </summary>
        protected virtual void InitExecuteLua()
        {
            if (initialised)
            {
                return;
            }

            // Cache a descriptive name to use in Lua error messages
            friendlyName = gameObject.name + "." + ParentBlock.BlockName + "." + "ExecuteLua #" + CommandIndex.ToString();

            var flowchart = GetFlowchart();

            // See if a Lua Environment has been assigned to this Flowchart
            if (luaEnvironment == null)
            {
                LuaEnv = flowchart.LuaEnv;
            }

            // No Lua Environment specified so just use any available or create one.
            if (LuaEnv == null)
            {
                LuaEnv = LuaEnvironment.GetLua();
            }

            string s = GetLuaString();

            luaFunction = LuaEnv.LoadLuaFunction(s, friendlyName);

            // Add a binding to the parent flowchart
            if (flowchart.LuaBindingName != "")
            {
                Table globals = LuaEnv.Interpreter.Globals;
                if (globals != null)
                {
                    globals[flowchart.LuaBindingName] = flowchart;
                }
            }

            // Always initialise when playing in the editor.
            // Allows the user to edit the Lua script while the game is playing.
            if (!(Application.isPlaying && Application.isEditor))
            {
                initialised = true;
            }
        }