diff --git a/Assets/Example/Scenes/Example.unity b/Assets/Example/Scenes/Example.unity index 5b48b999..e2bf1d8b 100644 Binary files a/Assets/Example/Scenes/Example.unity and b/Assets/Example/Scenes/Example.unity differ diff --git a/Assets/Fungus/FungusScript/Commands/Else.cs b/Assets/Fungus/FungusScript/Commands/Else.cs index aaafc604..29ab29ec 100644 --- a/Assets/Fungus/FungusScript/Commands/Else.cs +++ b/Assets/Fungus/FungusScript/Commands/Else.cs @@ -11,10 +11,16 @@ namespace Fungus { public override void OnEnter() { + Sequence sequence = GetSequence(); + if (sequence == null) + { + return; + } + // Find the next EndIf command at the same indent level as this Else command bool foundThisCommand = false; int indent = indentLevel; - foreach(Command command in parentSequence.commandList) + foreach(Command command in sequence.commandList) { if (foundThisCommand && command.indentLevel == indent) diff --git a/Assets/Fungus/FungusScript/Commands/If.cs b/Assets/Fungus/FungusScript/Commands/If.cs index ba1151dd..3d7b86b6 100644 --- a/Assets/Fungus/FungusScript/Commands/If.cs +++ b/Assets/Fungus/FungusScript/Commands/If.cs @@ -33,6 +33,12 @@ namespace Fungus public override void OnEnter() { + Sequence sequence = GetSequence(); + if (sequence == null) + { + return; + } + bool condition = false; if (variable == null) @@ -137,7 +143,7 @@ namespace Fungus // Find the next Else or EndIf command at the same indent level as this If command bool foundThisCommand = false; int indent = indentLevel; - foreach(Command command in parentSequence.commandList) + foreach(Command command in sequence.commandList) { if (foundThisCommand && command.indentLevel == indent) diff --git a/Assets/Fungus/FungusScript/Editor/CommandEditor.cs b/Assets/Fungus/FungusScript/Editor/CommandEditor.cs index 0da5a504..a8a53c78 100644 --- a/Assets/Fungus/FungusScript/Editor/CommandEditor.cs +++ b/Assets/Fungus/FungusScript/Editor/CommandEditor.cs @@ -125,6 +125,11 @@ namespace Fungus static public Command PasteCommand(Command copyCommand, Sequence sequence) { + if (sequence == null) + { + return null; + } + System.Type type = copyCommand.GetType(); Component copy = Undo.AddComponent(sequence.gameObject, type); System.Reflection.FieldInfo[] fields = type.GetFields(); @@ -133,9 +138,7 @@ namespace Fungus field.SetValue(copy, field.GetValue(copyCommand)); } - FungusScript fungusScript = sequence.GetFungusScript(); - - Undo.RecordObject(fungusScript, "Paste Command"); + Undo.RecordObject(sequence, "Paste Command"); Command newCommand = copy as Command; sequence.commandList.Add(newCommand); diff --git a/Assets/Fungus/FungusScript/Editor/FungusScriptWindow.cs b/Assets/Fungus/FungusScript/Editor/FungusScriptWindow.cs index 21211d3e..3c7b13c8 100755 --- a/Assets/Fungus/FungusScript/Editor/FungusScriptWindow.cs +++ b/Assets/Fungus/FungusScript/Editor/FungusScriptWindow.cs @@ -286,11 +286,13 @@ namespace Fungus if (s != null) { FungusScript fungusScript = s.GetFungusScript(); - fungusScript.selectedSequence = s; - fungusScript.selectedCommand = null; - - Selection.activeGameObject = fungusScript.gameObject; - GUIUtility.keyboardControl = 0; // Fix for textarea not refeshing (change focus) + if (fungusScript != null) + { + fungusScript.selectedSequence = s; + fungusScript.selectedCommand = null; + Selection.activeGameObject = fungusScript.gameObject; + GUIUtility.keyboardControl = 0; // Fix for textarea not refeshing (change focus) + } } } } @@ -320,8 +322,7 @@ namespace Fungus bool sequenceIsSelected = (fungusScript.selectedSequence == sequence); - Command[] commands = sequence.GetComponentsInChildren(); - foreach (Command command in commands) + foreach (Command command in sequence.commandList) { bool commandIsSelected = (fungusScript.selectedCommand == command); diff --git a/Assets/Fungus/FungusScript/Scripts/Command.cs b/Assets/Fungus/FungusScript/Scripts/Command.cs index 70210d96..2986244b 100644 --- a/Assets/Fungus/FungusScript/Scripts/Command.cs +++ b/Assets/Fungus/FungusScript/Scripts/Command.cs @@ -24,51 +24,37 @@ namespace Fungus } [RequireComponent(typeof(Sequence))] - public class Command : MonoBehaviour + public class Command : MonoBehaviour { - [HideInInspector] public string errorMessage = ""; - [HideInInspector] - public FungusScript parentFungusScript; - - [HideInInspector] - public Sequence parentSequence; - - [HideInInspector] public int indentLevel; - public virtual void Start() + public Sequence GetSequence() { - parentSequence = GetComponent(); - parentFungusScript = GetFungusScript(); + return gameObject.GetComponent(); } public FungusScript GetFungusScript() { - FungusScript sc = null; - - Transform parent = transform.parent; - while (parent != null) + Sequence s = GetSequence(); + if (s == null) { - sc = parent.gameObject.GetComponent(); - if (sc != null) - { - break; - } - parent = parent.transform.parent; + return null; } - return sc; + + return s.GetFungusScript(); } public bool IsExecuting() { - if (parentSequence == null) + Sequence sequence = GetSequence(); + if (sequence == null) { return false; } - return (parentSequence.activeCommand == this); + return (sequence.activeCommand == this); } public virtual void Execute() @@ -84,20 +70,36 @@ namespace Fungus public virtual void Continue(Command currentCommand) { OnExit(); - parentSequence.ExecuteNextCommand(currentCommand); + Sequence sequence = GetSequence(); + if (sequence != null) + { + sequence.ExecuteNextCommand(currentCommand); + } } public virtual void Stop() { OnExit(); - parentSequence.Stop(); + Sequence sequence = GetSequence(); + if (sequence != null) + { + sequence.Stop(); + } } public virtual void ExecuteSequence(Sequence s) { OnExit(); - parentSequence.Stop(); - parentFungusScript.ExecuteSequence(s); + Sequence sequence = GetSequence(); + if (sequence != null) + { + sequence.Stop(); + FungusScript fungusScript = sequence.GetFungusScript(); + if (fungusScript != null) + { + fungusScript.ExecuteSequence(s); + } + } } public virtual void OnEnter() diff --git a/Assets/Fungus/FungusScript/Scripts/Sequence.cs b/Assets/Fungus/FungusScript/Scripts/Sequence.cs index e4ee5700..e4c1b66e 100644 --- a/Assets/Fungus/FungusScript/Scripts/Sequence.cs +++ b/Assets/Fungus/FungusScript/Scripts/Sequence.cs @@ -12,18 +12,12 @@ namespace Fungus [ExecuteInEditMode] public class Sequence : MonoBehaviour { - [HideInInspector] public Rect nodeRect = new Rect(10, 10, 100, 40); - [HideInInspector] public string sequenceName = "Sequence"; - [HideInInspector] public string description = ""; - [System.NonSerialized] - public FungusScript fungusScript; - [System.NonSerialized] public Command activeCommand; @@ -31,25 +25,9 @@ namespace Fungus int executionCount; - public virtual void Start() - { - fungusScript = GetFungusScript(); - } - public FungusScript GetFungusScript() { - FungusScript sc = null; - Transform parent = transform.parent; - while (parent != null) - { - sc = parent.gameObject.GetComponent(); - - if (sc != null) - { - break; - } - } - return sc; + return GetComponentInParent(); } public bool HasError() @@ -67,6 +45,8 @@ namespace Fungus public bool IsRunning() { + FungusScript fungusScript = GetFungusScript(); + if (fungusScript == null || fungusScript.executingSequence == null) { @@ -114,14 +94,16 @@ namespace Fungus } else { - if (GetFungusScript().stepTime == 0f) + FungusScript fungusScript = GetFungusScript(); + + if (fungusScript.stepTime == 0f) { activeCommand = nextCommand; nextCommand.Execute(); } else { - StartCoroutine(ExecuteAfterDelay(nextCommand, GetFungusScript().stepTime)); + StartCoroutine(ExecuteAfterDelay(nextCommand, fungusScript.stepTime)); } } @@ -136,6 +118,12 @@ namespace Fungus public void Stop() { + FungusScript fungusScript = GetFungusScript(); + if (fungusScript == null) + { + return; + } + activeCommand = null; fungusScript.executingSequence = null; fungusScript.selectedSequence = null;