Browse Source

Merge pull request #445 from FungusGames/luautils-global-mode

Luautils global mode
master
Chris Gregan 9 years ago
parent
commit
9e7a62bbf2
  1. 34
      Assets/Fungus/Lua/Resources/Lua/fungus.txt
  2. 13
      Assets/Fungus/Lua/Scripts/LuaStore.cs
  3. 49
      Assets/Fungus/Lua/Scripts/LuaUtils.cs
  4. 21
      Assets/Tests/Lua/FungusTests.unity
  5. 18
      Assets/Tests/Lua/LuaEnvironmentTests.unity
  6. 6
      Assets/Tests/StringSubstitution/StringSubstitutionTests.unity

34
Assets/Fungus/Lua/Resources/Lua/fungus.txt

@ -18,17 +18,17 @@ end
-- Returns the absolute time -- Returns the absolute time
-- Use this timing function to work correctly with the Lua Environment's timeScale property -- Use this timing function to work correctly with the Lua Environment's timeScale property
function M.gettime() function M.gettime()
return fungus.luautils.getTime() return M.luautils.getTime()
end end
-- Returns the delta time this frame -- Returns the delta time this frame
-- Use this timing function to work correctly with the Lua Environment's timeScale property -- Use this timing function to work correctly with the Lua Environment's timeScale property
function M.getdeltatime() function M.getdeltatime()
return fungus.luautils.getDeltaTime() return M.luautils.getDeltaTime()
end end
function M.settimescale(s) function M.settimescale(s)
fungus.luautils.timeScale = s M.luautils.timeScale = s
end end
------------- -------------
@ -61,7 +61,7 @@ end
function M.run(enumerator) function M.run(enumerator)
-- If the parameter isn't an enumerator then CreateTask will fail -- If the parameter isn't an enumerator then CreateTask will fail
local status, err = pcall( function() local status, err = pcall( function()
local task = fungus.luaenvironment.RunUnityCoroutine(enumerator) local task = M.luaenvironment.RunUnityCoroutine(enumerator)
end) end)
if (not status) then if (not status) then
@ -74,7 +74,7 @@ end
function M.runwait(enumerator) function M.runwait(enumerator)
-- If the parameter isn't an enumerator then CreateTask will fail -- If the parameter isn't an enumerator then CreateTask will fail
local status, err = pcall( function() local status, err = pcall( function()
local task = fungus.luaenvironment.RunUnityCoroutine(enumerator) local task = M.luaenvironment.RunUnityCoroutine(enumerator)
while (task != nil and task.Running) do while (task != nil and task.Running) do
coroutine.yield(); coroutine.yield();
end end
@ -92,18 +92,18 @@ end
-- Set active language for string table -- Set active language for string table
function M.setlanguage(languagecode) function M.setlanguage(languagecode)
fungus.luautils.activeLanguage = languagecode M.luautils.activeLanguage = languagecode
end end
-- Get a named string from the string table -- Get a named string from the string table
function M.getstring(key) function M.getstring(key)
return fungus.luautils.GetString(key) return M.luautils.GetString(key)
end end
-- Substitutes variables and localisation strings into a piece of text -- Substitutes variables and localisation strings into a piece of text
-- e.g. v = 10, "Subbed value is [$v]" => "Subbed value is 10" -- e.g. v = 10, "Subbed value is [$v]" => "Subbed value is 10"
function M.sub(text) function M.sub(text)
return fungus.luautils.substitute(text) return M.luautils.substitute(text)
end end
-------------------- --------------------
@ -151,7 +151,7 @@ end
-- Gets the active say dialog, or creates one if none exists yet -- Gets the active say dialog, or creates one if none exists yet
function M.getsaydialog() function M.getsaydialog()
if (M.sayoptions.saydialog == nil) then if (M.sayoptions.saydialog == nil) then
local sd = fungus.luautils.spawn("SayDialog").GetComponent("SayDialog") local sd = M.luautils.spawn("SayDialog").GetComponent("SayDialog")
M.setsaydialog(sd) M.setsaydialog(sd)
end end
return M.sayoptions.saydialog return M.sayoptions.saydialog
@ -166,7 +166,7 @@ function M.setcharacter(character, portrait)
sd.SetCharacter(character) sd.SetCharacter(character)
-- Do substitution on character name -- Do substitution on character name
local subbed = fungus.sub(character.nameText) local subbed = M.sub(character.nameText)
M.sayoptions.saydialog.SetCharacterName(subbed, character.nameColor) M.sayoptions.saydialog.SetCharacterName(subbed, character.nameColor)
-- Try to set the portrait sprite -- Try to set the portrait sprite
@ -191,10 +191,10 @@ function M.say(text, voiceclip)
local sd = M.getsaydialog() local sd = M.getsaydialog()
-- Do variable substitution before displaying text -- Do variable substitution before displaying text
local subbed = fungus.sub(text) local subbed = M.sub(text)
local e = sd.SayInternal(subbed, M.sayoptions.clearprevious, M.sayoptions.waitforinput, M.sayoptions.fadewhendone, M.sayoptions.stopvoiceover, voiceclip) local e = sd.SayInternal(subbed, M.sayoptions.clearprevious, M.sayoptions.waitforinput, M.sayoptions.fadewhendone, M.sayoptions.stopvoiceover, voiceclip)
fungus.runwait(e) M.runwait(e)
end end
-------------- --------------
@ -212,7 +212,7 @@ end
-- Gets the active menu dialog, or creates one if none exists yet -- Gets the active menu dialog, or creates one if none exists yet
function M.getmenudialog() function M.getmenudialog()
if (M.menuoptions.menudialog == nil) then if (M.menuoptions.menudialog == nil) then
local md = fungus.luautils.spawn("MenuDialog").GetComponent("MenuDialog") local md = M.luautils.spawn("MenuDialog").GetComponent("MenuDialog")
M.setmenudialog(md) M.setmenudialog(md)
end end
return M.menuoptions.menudialog return M.menuoptions.menudialog
@ -226,8 +226,8 @@ function M.menu(text, callback, interactive)
local md = M.getmenudialog() local md = M.getmenudialog()
-- Do variable substitution before displaying text -- Do variable substitution before displaying text
local subbed = fungus.sub(text) local subbed = M.sub(text)
md.AddOption(subbed, interactive or true, fungus.luaenvironment, callback) md.AddOption(subbed, interactive or true, M.luaenvironment, callback)
end end
-- Display a timer during which the player has to choose an option. -- Display a timer during which the player has to choose an option.
@ -236,8 +236,8 @@ end
function M.menutimer(duration, callback) function M.menutimer(duration, callback)
local md = M.getmenudialog() local md = M.getmenudialog()
local e = md.ShowTimer(duration, fungus.luaenvironment, callback) local e = md.ShowTimer(duration, M.luaenvironment, callback)
fungus.runwait(e) M.runwait(e)
end end
-- Clear all currently displayed menu options -- Clear all currently displayed menu options

13
Assets/Fungus/Lua/Scripts/LuaStore.cs

@ -82,18 +82,15 @@ namespace Fungus
return; return;
} }
// If the fungus global table is defined then add the store to it
Table fungusTable = globals.Get("fungus").Table; 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. else
if (globals.Get("luaenvironment") != DynValue.Nil)
{ {
// Add the store as a global
globals["store"] = primeTable; globals["store"] = primeTable;
} }
} }

49
Assets/Fungus/Lua/Scripts/LuaUtils.cs

@ -94,7 +94,6 @@ namespace Fungus
InitTypes(); InitTypes();
InitFungusModule(); InitFungusModule();
InitBindings(); InitBindings();
InitStringTable();
} }
/// <summary> /// <summary>
@ -185,32 +184,7 @@ namespace Fungus
fungusTable["test"] = UserData.CreateStatic(testType); fungusTable["test"] = UserData.CreateStatic(testType);
} }
if (fungusModule == FungusModuleOptions.UseGlobalVariables) // Register the stringtable (if one is set)
{
// Copy all items from the Fungus table to global variables
foreach (TablePair p in fungusTable.Pairs)
{
if (interpreter.Globals.Keys.Contains(p.Key))
{
UnityEngine.Debug.LogError("Lua globals already contains a variable " + p.Key);
}
else
{
interpreter.Globals[p.Key] = p.Value;
}
}
// Note: We can't remove the fungus table itself because of dependencies between functions
}
}
/// <summary>
/// Loads the global string table used for localisation.
/// </summary>
protected virtual void InitStringTable()
{
MoonSharp.Interpreter.Script interpreter = luaEnvironment.Interpreter;
if (stringTable != null) if (stringTable != null)
{ {
try try
@ -219,7 +193,6 @@ namespace Fungus
if (stringTableRes.Type == DataType.Table) if (stringTableRes.Type == DataType.Table)
{ {
stringTableCached = stringTableRes.Table; stringTableCached = stringTableRes.Table;
Table fungusTable = interpreter.Globals.Get("fungus").Table;
if (fungusTable != null) if (fungusTable != null)
{ {
fungusTable["stringtable"] = stringTableCached; fungusTable["stringtable"] = stringTableCached;
@ -237,6 +210,26 @@ namespace Fungus
} }
stringSubstituter = new StringSubstituter(); stringSubstituter = new StringSubstituter();
if (fungusModule == FungusModuleOptions.UseGlobalVariables)
{
// Copy all items from the Fungus table to global variables
foreach (TablePair p in fungusTable.Pairs)
{
if (interpreter.Globals.Keys.Contains(p.Key))
{
UnityEngine.Debug.LogError("Lua globals already contains a variable " + p.Key);
}
else
{
interpreter.Globals[p.Key] = p.Value;
}
}
interpreter.Globals["fungus"] = DynValue.Nil;
// Note: We can't remove the fungus table itself because of dependencies between functions
}
} }
/// <summary> /// <summary>

21
Assets/Tests/Lua/FungusTests.unity

@ -1218,7 +1218,7 @@ MonoBehaviour:
runwait( b.Execute() ) runwait( b.Execute() )
check(fungus.gettime() - t >= 1) check(gettime() - t >= 1)
-- Check the new value of StringVar -- Check the new value of StringVar
@ -2748,7 +2748,7 @@ MonoBehaviour:
check(v3.value.z == 1) check(v3.value.z == 1)
fungus.pass()' pass()'
runAsCoroutine: 1 runAsCoroutine: 1
--- !u!1 &465829169 --- !u!1 &465829169
GameObject: GameObject:
@ -5397,11 +5397,11 @@ MonoBehaviour:
m_Name: m_Name:
m_EditorClassIdentifier: m_EditorClassIdentifier:
version: 1 version: 1
scrollPos: {x: 32, y: -30} scrollPos: {x: 163, y: 77}
variablesScrollPos: {x: 0, y: 0} variablesScrollPos: {x: 0, y: 0}
variablesExpanded: 0 variablesExpanded: 0
blockViewHeight: 251 blockViewHeight: 251
zoom: 0.9129947 zoom: 1
scrollViewRect: scrollViewRect:
serializedVersion: 2 serializedVersion: 2
x: -343 x: -343
@ -5409,8 +5409,7 @@ MonoBehaviour:
width: 1114 width: 1114
height: 859 height: 859
selectedBlock: {fileID: 942227027} selectedBlock: {fileID: 942227027}
selectedCommands: selectedCommands: []
- {fileID: 942227053}
variables: variables:
- {fileID: 942227029} - {fileID: 942227029}
- {fileID: 942227035} - {fileID: 942227035}
@ -5574,7 +5573,7 @@ MonoBehaviour:
errorMessage: errorMessage:
indentLevel: 0 indentLevel: 0
luaEnvironment: {fileID: 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' return c'
@ -5742,7 +5741,7 @@ MonoBehaviour:
errorMessage: errorMessage:
indentLevel: 0 indentLevel: 0
luaEnvironment: {fileID: 0} luaEnvironment: {fileID: 0}
luaScript: 'local v = fungus.factory.vector2(1,1) luaScript: 'local v = factory.vector2(1,1)
return v' return v'
@ -5811,7 +5810,7 @@ MonoBehaviour:
errorMessage: errorMessage:
indentLevel: 0 indentLevel: 0
luaEnvironment: {fileID: 0} luaEnvironment: {fileID: 0}
luaScript: 'local v = fungus.factory.vector3(1,1,1) luaScript: 'local v = factory.vector3(1,1,1)
return v' return v'
runAsCoroutine: 0 runAsCoroutine: 0
@ -7621,7 +7620,7 @@ GameObject:
m_Icon: {fileID: 0} m_Icon: {fileID: 0}
m_NavMeshLayer: 0 m_NavMeshLayer: 0
m_StaticEditorFlags: 0 m_StaticEditorFlags: 0
m_IsActive: 0 m_IsActive: 1
--- !u!114 &1641285457 --- !u!114 &1641285457
MonoBehaviour: MonoBehaviour:
m_ObjectHideFlags: 0 m_ObjectHideFlags: 0
@ -8816,7 +8815,7 @@ MonoBehaviour:
m_Script: {fileID: 11500000, guid: 61dddfdc5e0e44ca298d8f46f7f5a915, type: 3} m_Script: {fileID: 11500000, guid: 61dddfdc5e0e44ca298d8f46f7f5a915, type: 3}
m_Name: m_Name:
m_EditorClassIdentifier: m_EditorClassIdentifier:
selectedFlowchart: {fileID: 56584893} selectedFlowchart: {fileID: 942227028}
--- !u!4 &1791050794 --- !u!4 &1791050794
Transform: Transform:
m_ObjectHideFlags: 1 m_ObjectHideFlags: 1

18
Assets/Tests/Lua/LuaEnvironmentTests.unity

@ -142,7 +142,7 @@ GameObject:
m_Icon: {fileID: 0} m_Icon: {fileID: 0}
m_NavMeshLayer: 0 m_NavMeshLayer: 0
m_StaticEditorFlags: 0 m_StaticEditorFlags: 0
m_IsActive: 1 m_IsActive: 0
--- !u!114 &47556548 --- !u!114 &47556548
MonoBehaviour: MonoBehaviour:
m_ObjectHideFlags: 0 m_ObjectHideFlags: 0
@ -958,7 +958,7 @@ MonoBehaviour:
local t = 0 local t = 0
local timer = fungus.time.time local timer = time.time
-- Wait with timeScale = -1 -- Wait with timeScale = -1
@ -1620,19 +1620,19 @@ MonoBehaviour:
luaFile: {fileID: 0} luaFile: {fileID: 0}
luaScript: '-- Test string table localisation system luaScript: '-- Test string table localisation system
check(fungus.stringtable != nil) check(stringtable != nil)
-- Check string table loaded ok -- 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() -- Check getstring()
local g = fungus.getstring("hello") local g = getstring("hello")
check(g == "Hi there") check(g == "Hi there")
@ -1641,14 +1641,14 @@ MonoBehaviour:
t = 3 t = 3
local s = fungus.sub("should be {$t}") local s = sub("should be {$t}")
check(s == "should be 3") check(s == "should be 3")
-- Test substituting a string table value (en) -- Test substituting a string table value (en)
s = fungus.sub("say {$hello}") s = sub("say {$hello}")
check(s == "say Hi there") check(s == "say Hi there")
@ -1921,7 +1921,7 @@ GameObject:
m_Icon: {fileID: 0} m_Icon: {fileID: 0}
m_NavMeshLayer: 0 m_NavMeshLayer: 0
m_StaticEditorFlags: 0 m_StaticEditorFlags: 0
m_IsActive: 0 m_IsActive: 1
--- !u!114 &1900871525 --- !u!114 &1900871525
MonoBehaviour: MonoBehaviour:
m_ObjectHideFlags: 0 m_ObjectHideFlags: 0

6
Assets/Tests/StringSubstitution/StringSubstitutionTests.unity

@ -345,7 +345,7 @@ GameObject:
m_Icon: {fileID: 0} m_Icon: {fileID: 0}
m_NavMeshLayer: 0 m_NavMeshLayer: 0
m_StaticEditorFlags: 0 m_StaticEditorFlags: 0
m_IsActive: 1 m_IsActive: 0
--- !u!114 &545923888 --- !u!114 &545923888
MonoBehaviour: MonoBehaviour:
m_ObjectHideFlags: 0 m_ObjectHideFlags: 0
@ -474,7 +474,7 @@ GameObject:
m_Icon: {fileID: 0} m_Icon: {fileID: 0}
m_NavMeshLayer: 0 m_NavMeshLayer: 0
m_StaticEditorFlags: 0 m_StaticEditorFlags: 0
m_IsActive: 0 m_IsActive: 1
--- !u!114 &684307463 --- !u!114 &684307463
MonoBehaviour: MonoBehaviour:
m_ObjectHideFlags: 0 m_ObjectHideFlags: 0
@ -1185,7 +1185,7 @@ MonoBehaviour:
check (subbed == "Ok") check (subbed == "Ok")
fungus.pass()' pass()'
runAsCoroutine: 1 runAsCoroutine: 1
--- !u!1 &1532141878 --- !u!1 &1532141878
GameObject: GameObject:

Loading…
Cancel
Save