diff --git a/Assets/Fungus/Lua/Resources/Lua/fungus.txt b/Assets/Fungus/Lua/Resources/Lua/fungus.txt index 8c222a7e..c0568c74 100644 --- a/Assets/Fungus/Lua/Resources/Lua/fungus.txt +++ b/Assets/Fungus/Lua/Resources/Lua/fungus.txt @@ -161,13 +161,22 @@ function M.setsaydialog(saydialog) M.sayoptions.saydialog = saydialog 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") + M.setsaydialog(sd) + end + return M.sayoptions.saydialog +end + -- Set the active character on the Say Dialog -- character: A Fungus.Character component -- portrait: The name of a sprite in the character's portrait list function M.setcharacter(character, portrait) assert(character, "character must not be nil") - assert(M.sayoptions.saydialog, "saydialog must not be nil") - M.sayoptions.saydialog.SetCharacter(character) + local sd = M.getsaydialog() + sd.SetCharacter(character) -- Do substitution on character name local subbed = fungus.sub(character.nameText) @@ -192,11 +201,11 @@ end -- text: A string to write to the say dialog -- voice: A voiceover audioclip to play function M.say(text, voiceclip) - assert(M.sayoptions.saydialog, "saydialog must not be nil") + local sd = M.getsaydialog() -- Do variable substitution before displaying text local subbed = fungus.sub(text) - local e = M.sayoptions.saydialog.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) end @@ -213,24 +222,34 @@ function M.setmenudialog(menudialog) M.menuoptions.menudialog = menudialog 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") + M.setmenudialog(md) + end + return M.menuoptions.menudialog +end + -- Display a menu button -- text: text to display on the button -- callback: function to call when this option is selected -- interactive (optional): if false, displays the option as disabled function M.menu(text, callback, interactive) - assert(M.menuoptions.menudialog, "menudialog must not be nil") + local md = M.getmenudialog() -- Do variable substitution before displaying text local subbed = fungus.sub(text) - M.menuoptions.menudialog.AddOption(subbed, interactive or true, unity.luaenvironment, callback) + md.AddOption(subbed, interactive or true, unity.luaenvironment, callback) end -- Display a timer during which the player has to choose an option. -- duration: The length of time to display the timer. -- callback: Function to call if the timer expires before an option is selected. function M.menutimer(duration, callback) - assert(M.menuoptions.menudialog, "menudialog must not be nil") - local e = M.menuoptions.menudialog.ShowTimer(duration, unity.luaenvironment, callback) + local md = M.getmenudialog() + + local e = md.ShowTimer(duration, unity.luaenvironment, callback) fungus.runwait(e) end diff --git a/Assets/Tests/Lua/FungusTests.unity b/Assets/Tests/Lua/FungusTests.unity index f35ba23d..7b5d5252 100644 --- a/Assets/Tests/Lua/FungusTests.unity +++ b/Assets/Tests/Lua/FungusTests.unity @@ -880,7 +880,7 @@ MonoBehaviour: variablesScrollPos: {x: 0, y: 0} variablesExpanded: 1 blockViewHeight: 400 - zoom: 0.90499437 + zoom: 0.8889934 scrollViewRect: serializedVersion: 2 x: -343 @@ -941,6 +941,57 @@ Transform: m_Children: [] m_Father: {fileID: 1641285458} m_RootOrder: 3 +--- !u!1 &110417825 +GameObject: + m_ObjectHideFlags: 0 + m_PrefabParentObject: {fileID: 0} + m_PrefabInternal: {fileID: 0} + serializedVersion: 4 + m_Component: + - 4: {fileID: 110417827} + - 114: {fileID: 110417826} + m_Layer: 0 + m_Name: AutoDialogTest + m_TagString: Untagged + m_Icon: {fileID: 0} + m_NavMeshLayer: 0 + m_StaticEditorFlags: 0 + m_IsActive: 1 +--- !u!114 &110417826 +MonoBehaviour: + m_ObjectHideFlags: 0 + m_PrefabParentObject: {fileID: 0} + m_PrefabInternal: {fileID: 0} + m_GameObject: {fileID: 110417825} + m_Enabled: 1 + m_EditorHideFlags: 0 + m_Script: {fileID: 11500000, guid: b1dba0b27b0864740a8720e920aa88c0, type: 3} + m_Name: + m_EditorClassIdentifier: + timeout: 5 + ignored: 0 + succeedAfterAllAssertionsAreExecuted: 0 + expectException: 0 + expectedExceptionList: + succeedWhenExceptionIsThrown: 0 + includedPlatforms: -1 + platformsToIgnore: [] + dynamic: 0 + dynamicTypeName: +--- !u!4 &110417827 +Transform: + m_ObjectHideFlags: 0 + m_PrefabParentObject: {fileID: 0} + m_PrefabInternal: {fileID: 0} + m_GameObject: {fileID: 110417825} + m_LocalRotation: {x: 0, y: 0, z: 0, w: 1} + m_LocalPosition: {x: 0, y: 0, z: 0} + m_LocalScale: {x: 1, y: 1, z: 1} + m_Children: + - {fileID: 828158146} + - {fileID: 375366500} + m_Father: {fileID: 0} + m_RootOrder: 13 --- !u!1001 &110499668 Prefab: m_ObjectHideFlags: 0 @@ -2183,6 +2234,65 @@ CanvasRenderer: type: 2} m_PrefabInternal: {fileID: 0} m_GameObject: {fileID: 330161927} +--- !u!1 &375366499 +GameObject: + m_ObjectHideFlags: 0 + m_PrefabParentObject: {fileID: 100640, guid: 49031c561e16d4fcf91c12153f8e0b25, type: 2} + m_PrefabInternal: {fileID: 0} + serializedVersion: 4 + m_Component: + - 4: {fileID: 375366500} + - 114: {fileID: 375366502} + - 114: {fileID: 375366501} + m_Layer: 0 + m_Name: LuaEnvironment + m_TagString: Untagged + m_Icon: {fileID: 0} + m_NavMeshLayer: 0 + m_StaticEditorFlags: 0 + m_IsActive: 1 +--- !u!4 &375366500 +Transform: + m_ObjectHideFlags: 0 + m_PrefabParentObject: {fileID: 495584, guid: 49031c561e16d4fcf91c12153f8e0b25, type: 2} + m_PrefabInternal: {fileID: 0} + m_GameObject: {fileID: 375366499} + m_LocalRotation: {x: 0, y: 0, z: 0, w: 1} + m_LocalPosition: {x: 0, y: 0, z: 0} + m_LocalScale: {x: 1, y: 1, z: 1} + m_Children: [] + m_Father: {fileID: 110417827} + m_RootOrder: 1 +--- !u!114 &375366501 +MonoBehaviour: + m_ObjectHideFlags: 0 + m_PrefabParentObject: {fileID: 11486636, guid: 49031c561e16d4fcf91c12153f8e0b25, + type: 2} + m_PrefabInternal: {fileID: 0} + m_GameObject: {fileID: 375366499} + m_Enabled: 1 + m_EditorHideFlags: 0 + m_Script: {fileID: 11500000, guid: c10f0b861365b42b0928858f7b086ff3, type: 3} + m_Name: + m_EditorClassIdentifier: + stringTable: {fileID: 0} + activeLanguage: en + timeScale: -1 + registerTypes: + - {fileID: 4900000, guid: 9c3ab7a98d51241bbb499643399fa761, type: 3} +--- !u!114 &375366502 +MonoBehaviour: + m_ObjectHideFlags: 0 + m_PrefabParentObject: {fileID: 11493126, guid: 49031c561e16d4fcf91c12153f8e0b25, + type: 2} + m_PrefabInternal: {fileID: 0} + m_GameObject: {fileID: 375366499} + m_Enabled: 1 + m_EditorHideFlags: 0 + m_Script: {fileID: 11500000, guid: ba19c26c1ba7243d2b57ebc4329cc7c6, type: 3} + m_Name: + m_EditorClassIdentifier: + remoteDebugger: 0 --- !u!1 &398057887 GameObject: m_ObjectHideFlags: 0 @@ -4242,6 +4352,64 @@ Transform: - {fileID: 511430245} m_Father: {fileID: 0} m_RootOrder: 4 +--- !u!1 &828158145 +GameObject: + m_ObjectHideFlags: 0 + m_PrefabParentObject: {fileID: 139298, guid: c356764ac08ce4af2806a601a4f1e6e9, type: 2} + m_PrefabInternal: {fileID: 0} + serializedVersion: 4 + m_Component: + - 4: {fileID: 828158146} + - 114: {fileID: 828158147} + m_Layer: 0 + m_Name: LuaScript + m_TagString: Untagged + m_Icon: {fileID: 0} + m_NavMeshLayer: 0 + m_StaticEditorFlags: 0 + m_IsActive: 1 +--- !u!4 &828158146 +Transform: + m_ObjectHideFlags: 0 + m_PrefabParentObject: {fileID: 449874, guid: c356764ac08ce4af2806a601a4f1e6e9, type: 2} + m_PrefabInternal: {fileID: 0} + m_GameObject: {fileID: 828158145} + m_LocalRotation: {x: 0, y: 0, z: 0, w: 1} + m_LocalPosition: {x: 0, y: 0, z: 0} + m_LocalScale: {x: 1, y: 1, z: 1} + m_Children: [] + m_Father: {fileID: 110417827} + m_RootOrder: 0 +--- !u!114 &828158147 +MonoBehaviour: + m_ObjectHideFlags: 0 + m_PrefabParentObject: {fileID: 11472914, guid: c356764ac08ce4af2806a601a4f1e6e9, + type: 2} + m_PrefabInternal: {fileID: 0} + m_GameObject: {fileID: 828158145} + m_Enabled: 1 + m_EditorHideFlags: 0 + m_Script: {fileID: 11500000, guid: 446caeace65234baaacd52095d24f101, type: 3} + m_Name: + m_EditorClassIdentifier: + executeAfterTime: 1 + repeatExecuteTime: 1 + repeatEveryTime: 1 + executeAfterFrames: 1 + repeatExecuteFrame: 1 + repeatEveryFrame: 1 + hasFailed: 0 + executeMethods: 2 + 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 + != 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 + button = go.GetComponent(\"Button\")\nfungus.assert(button != nil)\n\nbutton.onClick.Invoke()" + runAsCoroutine: 1 + useFungusModule: 1 --- !u!1 &851680338 GameObject: m_ObjectHideFlags: 0