diff --git a/Assets/Fungus/Lua/Scripts/LuaStore.cs b/Assets/Fungus/Lua/Scripts/LuaStore.cs index d55fe633..02783c7c 100644 --- a/Assets/Fungus/Lua/Scripts/LuaStore.cs +++ b/Assets/Fungus/Lua/Scripts/LuaStore.cs @@ -82,18 +82,15 @@ namespace Fungus return; } + // If the fungus global table is defined then add the store to it Table fungusTable = globals.Get("fungus").Table; - if (fungusTable == null) + if (fungusTable != null) { - Debug.LogError("fungus table not found"); - return; + fungusTable["store"] = primeTable; } - - fungusTable["store"] = primeTable; - - // If we're using the fungus module in globals mode then add the store to globals as well. - if (globals.Get("luaenvironment") != DynValue.Nil) + else { + // Add the store as a global globals["store"] = primeTable; } } diff --git a/Assets/Fungus/Lua/Scripts/LuaUtils.cs b/Assets/Fungus/Lua/Scripts/LuaUtils.cs index b07a62ca..b9062ee7 100644 --- a/Assets/Fungus/Lua/Scripts/LuaUtils.cs +++ b/Assets/Fungus/Lua/Scripts/LuaUtils.cs @@ -94,7 +94,6 @@ namespace Fungus InitTypes(); InitFungusModule(); InitBindings(); - InitStringTable(); } /// @@ -185,6 +184,33 @@ namespace Fungus fungusTable["test"] = UserData.CreateStatic(testType); } + // Register the stringtable (if one is set) + if (stringTable != null) + { + try + { + DynValue stringTableRes = interpreter.DoString(stringTable.text); + if (stringTableRes.Type == DataType.Table) + { + stringTableCached = stringTableRes.Table; + if (fungusTable != null) + { + fungusTable["stringtable"] = stringTableCached; + } + } + } + catch (ScriptRuntimeException ex) + { + LuaEnvironment.LogException(ex.DecoratedMessage, stringTable.text); + } + catch (InterpreterException ex) + { + LuaEnvironment.LogException(ex.DecoratedMessage, stringTable.text); + } + } + + stringSubstituter = new StringSubstituter(); + if (fungusModule == FungusModuleOptions.UseGlobalVariables) { // Copy all items from the Fungus table to global variables @@ -200,45 +226,12 @@ namespace Fungus } } + interpreter.Globals["fungus"] = DynValue.Nil; + // Note: We can't remove the fungus table itself because of dependencies between functions } } - /// - /// Loads the global string table used for localisation. - /// - protected virtual void InitStringTable() - { - MoonSharp.Interpreter.Script interpreter = luaEnvironment.Interpreter; - - if (stringTable != null) - { - try - { - DynValue stringTableRes = interpreter.DoString(stringTable.text); - if (stringTableRes.Type == DataType.Table) - { - stringTableCached = stringTableRes.Table; - Table fungusTable = interpreter.Globals.Get("fungus").Table; - if (fungusTable != null) - { - fungusTable["stringtable"] = stringTableCached; - } - } - } - catch (ScriptRuntimeException ex) - { - LuaEnvironment.LogException(ex.DecoratedMessage, stringTable.text); - } - catch (InterpreterException ex) - { - LuaEnvironment.LogException(ex.DecoratedMessage, stringTable.text); - } - } - - stringSubstituter = new StringSubstituter(); - } - /// /// Returns a string from the string table for this key. /// The string returned depends on the active language. diff --git a/Assets/Tests/Lua/FungusTests.unity b/Assets/Tests/Lua/FungusTests.unity index 5bbddcdb..72765174 100644 --- a/Assets/Tests/Lua/FungusTests.unity +++ b/Assets/Tests/Lua/FungusTests.unity @@ -1218,7 +1218,7 @@ MonoBehaviour: runwait( b.Execute() ) - check(fungus.gettime() - t >= 1) + check(gettime() - t >= 1) -- Check the new value of StringVar @@ -2748,7 +2748,7 @@ MonoBehaviour: check(v3.value.z == 1) - fungus.pass()' + pass()' runAsCoroutine: 1 --- !u!1 &465829169 GameObject: @@ -5397,11 +5397,11 @@ MonoBehaviour: m_Name: m_EditorClassIdentifier: version: 1 - scrollPos: {x: 32, y: -30} + scrollPos: {x: 163, y: 77} variablesScrollPos: {x: 0, y: 0} variablesExpanded: 0 blockViewHeight: 251 - zoom: 0.9129947 + zoom: 1 scrollViewRect: serializedVersion: 2 x: -343 @@ -5409,8 +5409,7 @@ MonoBehaviour: width: 1114 height: 859 selectedBlock: {fileID: 942227027} - selectedCommands: - - {fileID: 942227053} + selectedCommands: [] variables: - {fileID: 942227029} - {fileID: 942227035} @@ -5574,7 +5573,7 @@ MonoBehaviour: errorMessage: indentLevel: 0 luaEnvironment: {fileID: 0} - luaScript: 'local c = fungus.factory.color(1, 0.5, 1, 1) + luaScript: 'local c = factory.color(1, 0.5, 1, 1) return c' @@ -5742,7 +5741,7 @@ MonoBehaviour: errorMessage: indentLevel: 0 luaEnvironment: {fileID: 0} - luaScript: 'local v = fungus.factory.vector2(1,1) + luaScript: 'local v = factory.vector2(1,1) return v' @@ -5811,7 +5810,7 @@ MonoBehaviour: errorMessage: indentLevel: 0 luaEnvironment: {fileID: 0} - luaScript: 'local v = fungus.factory.vector3(1,1,1) + luaScript: 'local v = factory.vector3(1,1,1) return v' runAsCoroutine: 0 @@ -7621,7 +7620,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 @@ -8816,7 +8815,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 diff --git a/Assets/Tests/Lua/LuaEnvironmentTests.unity b/Assets/Tests/Lua/LuaEnvironmentTests.unity index 8cc40dcc..66537909 100644 --- a/Assets/Tests/Lua/LuaEnvironmentTests.unity +++ b/Assets/Tests/Lua/LuaEnvironmentTests.unity @@ -142,7 +142,7 @@ GameObject: m_Icon: {fileID: 0} m_NavMeshLayer: 0 m_StaticEditorFlags: 0 - m_IsActive: 1 + m_IsActive: 0 --- !u!114 &47556548 MonoBehaviour: m_ObjectHideFlags: 0 @@ -958,7 +958,7 @@ MonoBehaviour: local t = 0 - local timer = fungus.time.time + local timer = time.time -- Wait with timeScale = -1 @@ -1620,19 +1620,19 @@ MonoBehaviour: luaFile: {fileID: 0} luaScript: '-- Test string table localisation system - check(fungus.stringtable != nil) + check(stringtable != nil) -- Check string table loaded ok - check(fungus.stringtable.hello.en == "Hi there") + check(stringtable.hello.en == "Hi there") - check(fungus.stringtable.hello.fr == "Bonjour") + check(stringtable.hello.fr == "Bonjour") -- Check getstring() - local g = fungus.getstring("hello") + local g = getstring("hello") check(g == "Hi there") @@ -1641,14 +1641,14 @@ MonoBehaviour: t = 3 - local s = fungus.sub("should be {$t}") + local s = sub("should be {$t}") check(s == "should be 3") -- Test substituting a string table value (en) - s = fungus.sub("say {$hello}") + s = sub("say {$hello}") check(s == "say Hi there") @@ -1921,7 +1921,7 @@ GameObject: m_Icon: {fileID: 0} m_NavMeshLayer: 0 m_StaticEditorFlags: 0 - m_IsActive: 0 + m_IsActive: 1 --- !u!114 &1900871525 MonoBehaviour: m_ObjectHideFlags: 0 diff --git a/Assets/Tests/StringSubstitution/StringSubstitutionTests.unity b/Assets/Tests/StringSubstitution/StringSubstitutionTests.unity index 3dd694f6..cf200eec 100644 --- a/Assets/Tests/StringSubstitution/StringSubstitutionTests.unity +++ b/Assets/Tests/StringSubstitution/StringSubstitutionTests.unity @@ -345,7 +345,7 @@ GameObject: m_Icon: {fileID: 0} m_NavMeshLayer: 0 m_StaticEditorFlags: 0 - m_IsActive: 1 + m_IsActive: 0 --- !u!114 &545923888 MonoBehaviour: m_ObjectHideFlags: 0 @@ -474,7 +474,7 @@ GameObject: m_Icon: {fileID: 0} m_NavMeshLayer: 0 m_StaticEditorFlags: 0 - m_IsActive: 0 + m_IsActive: 1 --- !u!114 &684307463 MonoBehaviour: m_ObjectHideFlags: 0 @@ -1185,7 +1185,7 @@ MonoBehaviour: check (subbed == "Ok") - fungus.pass()' + pass()' runAsCoroutine: 1 --- !u!1 &1532141878 GameObject: