diff --git a/Assets/Fungus/Lua/Resources/Lua/fungus.txt b/Assets/Fungus/Lua/Resources/Lua/fungus.txt index c0568c74..3a54d438 100644 --- a/Assets/Fungus/Lua/Resources/Lua/fungus.txt +++ b/Assets/Fungus/Lua/Resources/Lua/fungus.txt @@ -3,6 +3,15 @@ local inspect = require('inspect') -- Utility functions for working with Lua in Fungus local M = {} +-- Merge everything in the temp _fungus table into this module +local function mergefungustable() + for k,v in pairs(_fungus) do + M[k] = v + end + _fungus = nil +end +mergefungustable() + ------------ -- Debugging ------------ @@ -18,17 +27,17 @@ end -- Returns the absolute time -- Use this timing function to work correctly with the Lua Environment's timeScale property function M.gettime() - return unity.luautils.getTime() + return fungus.luautils.getTime() end -- Returns the delta time this frame -- Use this timing function to work correctly with the Lua Environment's timeScale property function M.getdeltatime() - return unity.luautils.getDeltaTime() + return fungus.luautils.getDeltaTime() end function M.settimescale(s) - unity.luautils.timeScale = s + fungus.luautils.timeScale = s end ------------- @@ -61,7 +70,7 @@ end function M.run(enumerator) -- If the parameter isn't an enumerator then CreateTask will fail local status, err = pcall( function() - local task = unity.luaenvironment.RunUnityCoroutine(enumerator) + local task = fungus.luaenvironment.RunUnityCoroutine(enumerator) end) if (not status) then @@ -74,7 +83,7 @@ end function M.runwait(enumerator) -- If the parameter isn't an enumerator then CreateTask will fail local status, err = pcall( function() - local task = unity.luaenvironment.RunUnityCoroutine(enumerator) + local task = fungus.luaenvironment.RunUnityCoroutine(enumerator) while (task != nil and task.Running) do coroutine.yield(); end @@ -92,18 +101,18 @@ end -- Set active language for string table function M.setlanguage(languagecode) - unity.luautils.activeLanguage = languagecode + fungus.luautils.activeLanguage = languagecode end -- Get a named string from the string table function M.getstring(key) - return unity.luautils.GetString(key) + return fungus.luautils.GetString(key) end -- Substitutes variables and localisation strings into a piece of text -- e.g. v = 10, "Subbed value is [$v]" => "Subbed value is 10" function M.sub(text) - return unity.luautils.substitute(text) + return fungus.luautils.substitute(text) end -------------------- @@ -116,34 +125,20 @@ function M.assert(c, reason) -- Output a traceback to help track down source error( debug.traceback("Assert failed", 2) ) end - unity.test.assert(c, reason) + fungus.test.assert(c, reason) end -- Pass an integration test (requires Unity Test Tools) function M.pass() - unity.test.pass() + fungus.test.pass() end -- Fail an integration test (requires Unity Test Tools) function M.fail(reason) error( debug.traceback("Test failed", 2) ) - unity.test.fail(reason) + fungus.test.fail(reason) end ---------------- --- Fungus Prefs ---------------- - --- Handy alias to the real prefs object -M.prefs = unity.prefs - ---------------- --- Lua Store ---------------- - --- Handy alias to the real store object -M.store = unity.store - ------------- -- Say Dialog ------------- @@ -164,7 +159,7 @@ end -- Gets the active say dialog, or creates one if none exists yet function M.getsaydialog() if (M.sayoptions.saydialog == nil) then - local sd = unity.luautils.spawn("SayDialog").GetComponent("SayDialog") + local sd = fungus.luautils.spawn("SayDialog").GetComponent("SayDialog") M.setsaydialog(sd) end return M.sayoptions.saydialog @@ -225,7 +220,7 @@ end -- Gets the active menu dialog, or creates one if none exists yet function M.getmenudialog() if (M.menuoptions.menudialog == nil) then - local md = unity.luautils.spawn("MenuDialog").GetComponent("MenuDialog") + local md = fungus.luautils.spawn("MenuDialog").GetComponent("MenuDialog") M.setmenudialog(md) end return M.menuoptions.menudialog @@ -240,7 +235,7 @@ function M.menu(text, callback, interactive) -- Do variable substitution before displaying text local subbed = fungus.sub(text) - md.AddOption(subbed, interactive or true, unity.luaenvironment, callback) + md.AddOption(subbed, interactive or true, fungus.luaenvironment, callback) end -- Display a timer during which the player has to choose an option. @@ -249,7 +244,7 @@ end function M.menutimer(duration, callback) local md = M.getmenudialog() - local e = md.ShowTimer(duration, unity.luaenvironment, callback) + local e = md.ShowTimer(duration, fungus.luaenvironment, callback) fungus.runwait(e) end diff --git a/Assets/Fungus/Lua/Scripts/LuaStore.cs b/Assets/Fungus/Lua/Scripts/LuaStore.cs index b69043a1..b0572a48 100644 --- a/Assets/Fungus/Lua/Scripts/LuaStore.cs +++ b/Assets/Fungus/Lua/Scripts/LuaStore.cs @@ -82,14 +82,14 @@ namespace Fungus return; } - Table unityTable = globals.Get("unity").Table; - if (unityTable == null) + Table fungusTable = globals.Get("_fungus").Table; + if (fungusTable == null) { - Debug.LogError("Unity table not found"); + Debug.LogError("_fungus table not found"); return; } - unityTable["store"] = primeTable; + fungusTable["store"] = primeTable; } } diff --git a/Assets/Fungus/Lua/Scripts/LuaUtils.cs b/Assets/Fungus/Lua/Scripts/LuaUtils.cs index 2f957d3d..35b9eaef 100644 --- a/Assets/Fungus/Lua/Scripts/LuaUtils.cs +++ b/Assets/Fungus/Lua/Scripts/LuaUtils.cs @@ -132,27 +132,29 @@ namespace Fungus { MoonSharp.Interpreter.Script interpreter = luaEnvironment.Interpreter; - // Add the CLR class objects to a global unity table - Table unityTable = new Table(interpreter); - interpreter.Globals["unity"] = unityTable; + // Add the CLR class objects to a temp unity table called _fungus. + // When the fungus module is required, all the entries from _fungus are copied over. + + Table fungusTable = new Table(interpreter); + interpreter.Globals["_fungus"] = fungusTable; // Static classes - unityTable["time"] = UserData.CreateStatic(typeof(Time)); - unityTable["prefs"] = UserData.CreateStatic(typeof(FungusPrefs)); + fungusTable["time"] = UserData.CreateStatic(typeof(Time)); + fungusTable["prefs"] = UserData.CreateStatic(typeof(FungusPrefs)); UserData.RegisterType(typeof(PODTypeFactory)); - unityTable["factory"] = UserData.CreateStatic(typeof(PODTypeFactory)); + fungusTable["factory"] = UserData.CreateStatic(typeof(PODTypeFactory)); // Lua Environment and Lua Utils components - unityTable["luaenvironment"] = luaEnvironment; - unityTable["luautils"] = this; + fungusTable["luaenvironment"] = luaEnvironment; + fungusTable["luautils"] = this; // Provide access to the Unity Test Tools (if available). Type testType = Type.GetType("IntegrationTest"); if (testType != null) { UserData.RegisterType(testType); - unityTable["test"] = UserData.CreateStatic(testType); + fungusTable["test"] = UserData.CreateStatic(testType); } // Example of how to register an enum @@ -175,10 +177,10 @@ namespace Fungus if (stringTableRes.Type == DataType.Table) { stringTableCached = stringTableRes.Table; - Table unityTable = interpreter.Globals.Get("unity").Table; - if (unityTable != null) + Table fungusTable = interpreter.Globals.Get("_fungus").Table; + if (fungusTable != null) { - unityTable["stringtable"] = stringTableCached; + fungusTable["stringtable"] = stringTableCached; } } } diff --git a/Assets/Tests/Lua/FungusTests.unity b/Assets/Tests/Lua/FungusTests.unity index 8c085390..a2fe6d10 100644 --- a/Assets/Tests/Lua/FungusTests.unity +++ b/Assets/Tests/Lua/FungusTests.unity @@ -4403,10 +4403,10 @@ MonoBehaviour: luaEnvironment: {fileID: 0} luaFile: {fileID: 0} luaScript: "-- Test Say creating a SayDialog when none already exists\n\nfungus.sayoptions.waitforinput - = false\nfungus.say(\"test\")\n\ngo = unity.luautils.find(\"SayDialog\")\nfungus.assert(go + = false\nfungus.say(\"test\")\n\ngo = fungus.luautils.find(\"SayDialog\")\nfungus.assert(go != nil)\n\n-- Test Menu creating a MenuDialog when none already exists\nfunction choose_a()\n fungus.pass()\nend\n\nfungus.menu(\"Option A\", choose_a)\n\nlocal - go = unity.luautils.find(\"OptionButton0\")\nfungus.assert(go != nil)\n\nlocal + go = fungus.luautils.find(\"OptionButton0\")\nfungus.assert(go != nil)\n\nlocal button = go.GetComponent(\"Button\")\nfungus.assert(button != nil)\n\nbutton.onClick.Invoke()" runAsCoroutine: 1 useFungusModule: 1 @@ -5408,7 +5408,7 @@ MonoBehaviour: height: 859 selectedBlock: {fileID: 942227027} selectedCommands: - - {fileID: 942227025} + - {fileID: 942227053} variables: - {fileID: 942227029} - {fileID: 942227035} @@ -5575,7 +5575,7 @@ MonoBehaviour: errorMessage: indentLevel: 0 luaEnvironment: {fileID: 0} - luaScript: 'local c = unity.factory.color(1, 0.5, 1, 1) + luaScript: 'local c = fungus.factory.color(1, 0.5, 1, 1) return c' @@ -5748,7 +5748,7 @@ MonoBehaviour: errorMessage: indentLevel: 0 luaEnvironment: {fileID: 0} - luaScript: 'local v = unity.factory.vector2(1,1) + luaScript: 'local v = fungus.factory.vector2(1,1) return v' @@ -5819,7 +5819,7 @@ MonoBehaviour: errorMessage: indentLevel: 0 luaEnvironment: {fileID: 0} - luaScript: 'local v = unity.factory.vector3(1,1,1) + luaScript: 'local v = fungus.factory.vector3(1,1,1) return v' useFungusModule: 1 @@ -7630,7 +7630,7 @@ GameObject: m_Icon: {fileID: 0} m_NavMeshLayer: 0 m_StaticEditorFlags: 0 - m_IsActive: 0 + m_IsActive: 1 --- !u!114 &1641285457 MonoBehaviour: m_ObjectHideFlags: 0 @@ -8827,7 +8827,7 @@ MonoBehaviour: m_Script: {fileID: 11500000, guid: 61dddfdc5e0e44ca298d8f46f7f5a915, type: 3} m_Name: m_EditorClassIdentifier: - selectedFlowchart: {fileID: 56584893} + selectedFlowchart: {fileID: 942227028} --- !u!4 &1791050794 Transform: m_ObjectHideFlags: 1 @@ -9329,7 +9329,7 @@ MonoBehaviour: variablesScrollPos: {x: 0, y: 0} variablesExpanded: 1 blockViewHeight: 400 - zoom: 0.9729984 + zoom: 1 scrollViewRect: serializedVersion: 2 x: -343 @@ -9337,8 +9337,7 @@ MonoBehaviour: width: 1114 height: 859 selectedBlock: {fileID: 1983492494} - selectedCommands: - - {fileID: 1983492498} + selectedCommands: [] variables: - {fileID: 1983492492} - {fileID: 1983492497} diff --git a/Assets/Tests/Lua/LuaEnvironmentTests.unity b/Assets/Tests/Lua/LuaEnvironmentTests.unity index 818ad403..91e8d493 100644 --- a/Assets/Tests/Lua/LuaEnvironmentTests.unity +++ b/Assets/Tests/Lua/LuaEnvironmentTests.unity @@ -961,7 +961,7 @@ MonoBehaviour: local t = 0 - local timer = unity.time.time + local timer = fungus.time.time -- Wait with timeScale = -1 @@ -994,7 +994,7 @@ MonoBehaviour: -- The real elapsed time should be 3 seconds - elapsed = (unity.time.time - timer) + elapsed = (fungus.time.time - timer) assert(elapsed >= 3) @@ -1622,14 +1622,16 @@ MonoBehaviour: luaFile: {fileID: 0} luaScript: '-- Test string table localisation system - fungus.assert(unity.stringtable != nil) + print (fungus.stringtable) + + fungus.assert(fungus.stringtable != nil) -- Check string table loaded ok - fungus.assert(unity.stringtable.hello.en == "Hi there") + fungus.assert(fungus.stringtable.hello.en == "Hi there") - fungus.assert(unity.stringtable.hello.fr == "Bonjour") + fungus.assert(fungus.stringtable.hello.fr == "Bonjour") -- Check getstring() @@ -1718,9 +1720,9 @@ MonoBehaviour: luaFile: {fileID: 0} luaScript: '-- Test if object binding works - unity.test.assert (dummy != nil) + fungus.assert (dummy != nil) - unity.test.pass() + fungus.pass() ' runAsCoroutine: 1 @@ -1769,7 +1771,8 @@ MonoBehaviour: width: 1114 height: 859 selectedBlock: {fileID: 1828947834} - selectedCommands: [] + selectedCommands: + - {fileID: 1828947833} variables: [] description: "Check if the LuaStore prime table is created \nand registered as fungus.store, and if we can write and read values to it." @@ -2014,12 +2017,12 @@ MonoBehaviour: luaScript: '-- Test GameObject methods like find, instantiate, spawn, etc. - testgo = unity.luautils.find("TestGameObject") + testgo = fungus.luautils.find("TestGameObject") fungus.assert(testgo != null) - go = unity.luautils.instantiate(testgo) + go = fungus.luautils.instantiate(testgo) fungus.assert(go != null) @@ -2029,17 +2032,17 @@ MonoBehaviour: fungus.assert(go.name == "fred") - unity.luautils.destroy(go) + fungus.luautils.destroy(go) -- Spawn an object from a prefab in a Resources folder - saydialog = unity.luautils.spawn("SayDialog") + saydialog = fungus.luautils.spawn("SayDialog") fungus.assert(saydialog != nil) - unity.luautils.destroy(saydialog) + fungus.luautils.destroy(saydialog) fungus.pass()' @@ -2105,6 +2108,10 @@ Prefab: m_Modification: m_TransformParent: {fileID: 1960220032} m_Modifications: + - target: {fileID: 11414792, guid: e0c2b90c058ff43f4a56a266d4fa721b, type: 2} + propertyPath: boundTypes.Array.size + value: 10 + objectReference: {fileID: 0} - target: {fileID: 11414792, guid: e0c2b90c058ff43f4a56a266d4fa721b, type: 2} propertyPath: boundObjects.Array.size value: 1 @@ -2149,6 +2156,51 @@ Prefab: propertyPath: boundObjects.Array.data[0].key value: dummy objectReference: {fileID: 0} + - target: {fileID: 11414792, guid: e0c2b90c058ff43f4a56a266d4fa721b, type: 2} + propertyPath: boundTypes.Array.data[0] + value: UnityEngine.GameObject, UnityEngine, Version=0.0.0.0, Culture=neutral, + PublicKeyToken=null + objectReference: {fileID: 0} + - target: {fileID: 11414792, guid: e0c2b90c058ff43f4a56a266d4fa721b, type: 2} + propertyPath: boundTypes.Array.data[1] + value: UnityEngine.PrimitiveType, UnityEngine, Version=0.0.0.0, Culture=neutral, + PublicKeyToken=null + objectReference: {fileID: 0} + - target: {fileID: 11414792, guid: e0c2b90c058ff43f4a56a266d4fa721b, type: 2} + propertyPath: boundTypes.Array.data[2] + value: UnityEngine.Component, UnityEngine, Version=0.0.0.0, Culture=neutral, + PublicKeyToken=null + objectReference: {fileID: 0} + - target: {fileID: 11414792, guid: e0c2b90c058ff43f4a56a266d4fa721b, type: 2} + propertyPath: boundTypes.Array.data[3] + value: System.Type, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + objectReference: {fileID: 0} + - target: {fileID: 11414792, guid: e0c2b90c058ff43f4a56a266d4fa721b, type: 2} + propertyPath: boundTypes.Array.data[4] + value: System.Boolean, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + objectReference: {fileID: 0} + - target: {fileID: 11414792, guid: e0c2b90c058ff43f4a56a266d4fa721b, type: 2} + propertyPath: boundTypes.Array.data[5] + value: System.Int32, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + objectReference: {fileID: 0} + - target: {fileID: 11414792, guid: e0c2b90c058ff43f4a56a266d4fa721b, type: 2} + propertyPath: boundTypes.Array.data[6] + value: UnityEngine.SendMessageOptions, UnityEngine, Version=0.0.0.0, Culture=neutral, + PublicKeyToken=null + objectReference: {fileID: 0} + - target: {fileID: 11414792, guid: e0c2b90c058ff43f4a56a266d4fa721b, type: 2} + propertyPath: boundTypes.Array.data[7] + value: UnityEngine.SceneManagement.Scene, UnityEngine, Version=0.0.0.0, Culture=neutral, + PublicKeyToken=null + objectReference: {fileID: 0} + - target: {fileID: 11414792, guid: e0c2b90c058ff43f4a56a266d4fa721b, type: 2} + propertyPath: boundTypes.Array.data[8] + value: UnityEngine.Object, UnityEngine, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null + objectReference: {fileID: 0} + - target: {fileID: 11414792, guid: e0c2b90c058ff43f4a56a266d4fa721b, type: 2} + propertyPath: boundTypes.Array.data[9] + value: System.Single, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + objectReference: {fileID: 0} m_RemovedComponents: [] m_ParentPrefab: {fileID: 100100000, guid: e0c2b90c058ff43f4a56a266d4fa721b, type: 2} m_IsPrefabParent: 0