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

            if (luaEnvironment == null)
            {
                // Create a Lua Environment if none exists yet
                luaEnvironment = LuaEnvironment.GetLua();
            }

            if (luaEnvironment == null)
            {
                Debug.LogError("No Lua Environment found");
                return;
            }

            // Ensure the LuaEnvironment is initialized before trying to execute code
            luaEnvironment.InitEnvironment();

            // Cache a descriptive name to use in Lua error messages
            friendlyName = GetPath(transform) + ".LuaScript";

            string s = GetLuaString();

            luaFunction = luaEnvironment.LoadLuaFunction(s, friendlyName);

            initialised = true;
        }
示例#2
0
        public override void AddBindings(LuaEnvironment luaEnv)
        {
            if (!Init())
            {
                return;
            }

            MoonSharp.Interpreter.Script interpreter = luaEnv.Interpreter;
            Table globals = interpreter.Globals;

            if (globals == null)
            {
                Debug.LogError("Lua globals table is null");
                return;
            }

            // If the fungus global table is defined then add the store to it
            Table fungusTable = globals.Get("fungus").Table;

            if (fungusTable != null)
            {
                fungusTable["store"] = primeTable;
            }
            else
            {
                // Add the store as a global
                globals["store"] = primeTable;
            }
        }
示例#3
0
        protected IEnumerator CallLuaClosure(LuaEnvironment luaEnv, Closure callback)
        {
            yield return(new WaitForEndOfFrame());

            if (callback != null)
            {
                luaEnv.RunLuaFunction(callback, true);
            }
        }
示例#4
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 + "." + this.GetType().ToString() + " #" + CommandIndex.ToString();

            Flowchart flowchart = GetFlowchart();

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

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

            string s = GetLuaString();

            luaFunction = luaEnvironment.LoadLuaFunction(s, friendlyName);

            // Add a binding to the parent flowchart
            if (flowchart.LuaBindingName != "")
            {
                Table globals = luaEnvironment.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;
            }
        }
示例#5
0
        /// <summary>
        /// Adds the option to the list of displayed options, calls a Lua function when selected.
        /// Will cause the Menu dialog to become visible if it is not already visible.
        /// </summary>
        /// <returns><c>true</c>, if the option was added successfully.</returns>
        public virtual bool AddOption(string text, bool interactable, LuaEnvironment luaEnv, Closure callBack)
        {
            if (!gameObject.activeSelf)
            {
                gameObject.SetActive(true);
            }

            // Copy to local variables
            LuaEnvironment env  = luaEnv;
            Closure        call = callBack;

            UnityEngine.Events.UnityAction action = delegate
            {
                StopAllCoroutines();
                // Stop timeout
                Clear();
                HideSayDialog();
                // Use a coroutine to call the callback on the next frame
                StartCoroutine(CallLuaClosure(env, call));
            };

            return(AddOption(text, interactable, false, action));
        }
示例#6
0
        /// <summary>
        /// Show a timer during which the player can select an option. Calls a Lua function when the timer expires.
        /// </summary>
        public virtual IEnumerator ShowTimer(float duration, LuaEnvironment luaEnv, Closure callBack)
        {
            if (CachedSlider == null ||
                duration <= 0f)
            {
                yield break;
            }

            CachedSlider.gameObject.SetActive(true);
            StopAllCoroutines();

            float  elapsedTime   = 0;
            Slider timeoutSlider = CachedSlider;

            while (elapsedTime < duration)
            {
                if (timeoutSlider != null)
                {
                    float t = 1f - elapsedTime / duration;
                    timeoutSlider.value = t;
                }

                elapsedTime += Time.deltaTime;

                yield return(null);
            }

            Clear();
            gameObject.SetActive(false);
            HideSayDialog();

            if (callBack != null)
            {
                luaEnv.RunLuaFunction(callBack, true);
            }
        }
示例#7
0
        /// <summary>
        /// Add all declared bindings to the globals table.
        /// </summary>
        public override void AddBindings(LuaEnvironment luaEnv)
        {
            if (!allEnvironments &&
                (luaEnvironment != null && !luaEnvironment.Equals(luaEnv)))
            {
                // Don't add bindings to this environment
                return;
            }

            MoonSharp.Interpreter.Script interpreter = luaEnv.Interpreter;
            Table globals = interpreter.Globals;

            Table bindingsTable = null;

            if (tableName == "")
            {
                // Add directly to globals table
                bindingsTable = globals;
            }
            else
            {
                DynValue res = globals.Get(tableName);
                if (res.Type == DataType.Table)
                {
                    // Add to an existing table
                    bindingsTable = res.Table;
                }
                else
                {
                    // Create a new table
                    bindingsTable      = new Table(globals.OwnerScript);
                    globals[tableName] = bindingsTable;
                }
            }

            if (bindingsTable == null)
            {
                Debug.LogError("Bindings table must not be null");
            }

            // Register types of bound objects with MoonSharp
            if (registerTypes)
            {
                foreach (string typeName in boundTypes)
                {
                    LuaEnvironment.RegisterType(typeName);
                }
            }

            for (int i = 0; i < boundObjects.Count; ++i)
            {
                // Ignore empty keys
                string key = boundObjects[i].key;
                if (key == null ||
                    key == "")
                {
                    continue;
                }

                // Check for keys used multiple times
                if (bindingsTable.Get(key).Type != DataType.Nil)
                {
                    Debug.LogWarning("An item already exists with the same key as binding '" + key + "'. This binding will be ignored.");
                    continue;
                }

                // Ignore bindings with no object set
                GameObject go = boundObjects[i].obj as GameObject;
                if (go != null)
                {
                    // Register as gameobject / components
                    Component component = boundObjects[i].component;
                    if (component == null)
                    {
                        // Bind the key to the gameobject
                        bindingsTable[key] = go;
                    }
                    else
                    {
                        // Bind the key to the component
                        bindingsTable[key] = component;
                    }
                }
                else
                {
                    // Register as other UnityEngine.Object type
                    bindingsTable[key] = boundObjects[i].obj;
                }
            }
        }
示例#8
0
 /// <summary>
 /// Adds the required bindings to the Lua environment.
 /// </summary>
 public abstract void AddBindings(LuaEnvironment luaEnv);
示例#9
0
        /// <summary>
        /// Registers all listed c# types for interop with Lua.
        /// You can also register types directly in the Awake method of any
        /// monobehavior in your scene using UserData.RegisterType().
        /// </summary>
        protected virtual void InitTypes()
        {
            // Always register these FungusLua utilities
            LuaEnvironment.RegisterType("Fungus.PODTypeFactory");
            LuaEnvironment.RegisterType("Fungus.FungusPrefs");

            foreach (TextAsset textFile in registerTypes)
            {
                if (textFile == null)
                {
                    continue;
                }

                // Parse JSON file
                JSONObject jsonObject = new JSONObject(textFile.text);
                if (jsonObject == null ||
                    jsonObject.type != JSONObject.Type.OBJECT)
                {
                    UnityEngine.Debug.LogError("Error parsing JSON file " + textFile.name);
                    continue;
                }

                // Register types with MoonSharp
                JSONObject registerTypesArray = jsonObject.GetField("registerTypes");
                if (registerTypesArray != null &&
                    registerTypesArray.type == JSONObject.Type.ARRAY)
                {
                    foreach (JSONObject entry in registerTypesArray.list)
                    {
                        if (entry != null &&
                            entry.type == JSONObject.Type.STRING)
                        {
                            string typeName = entry.str.Trim();

                            if (Type.GetType(typeName) == null)
                            {
                                continue;
                            }

                            LuaEnvironment.RegisterType(typeName);
                        }
                    }
                }

                // Register extension types with MoonSharp
                JSONObject extensionTypesArray = jsonObject.GetField("extensionTypes");
                if (extensionTypesArray != null &&
                    extensionTypesArray.type == JSONObject.Type.ARRAY)
                {
                    foreach (JSONObject entry in extensionTypesArray.list)
                    {
                        if (entry != null &&
                            entry.type == JSONObject.Type.STRING)
                        {
                            string typeName = entry.str.Trim();

                            if (Type.GetType(typeName) == null)
                            {
                                continue;
                            }

                            LuaEnvironment.RegisterType(typeName, true);
                        }
                    }
                }
            }
        }