From 4401060c619b0c0a2e1684fd4329975ff09961ff Mon Sep 17 00:00:00 2001 From: chrisgregan Date: Fri, 26 Sep 2014 22:45:17 +0100 Subject: [PATCH] Fix #46 - commands now store reference to their parent sequence MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The reference to the parent sequence is only populate at runtime, so don’t try to use it in custom editors, etc. --- Assets/Fungus/FungusScript/Commands/Else.cs | 5 +-- Assets/Fungus/FungusScript/Commands/If.cs | 5 +-- Assets/Fungus/FungusScript/Scripts/Command.cs | 42 +++++++++---------- .../FungusScript/Scripts/CommandCopyBuffer.cs | 2 +- .../FungusScript/Scripts/FungusScript.cs | 2 +- .../Fungus/FungusScript/Scripts/Sequence.cs | 9 ++++ 6 files changed, 35 insertions(+), 30 deletions(-) diff --git a/Assets/Fungus/FungusScript/Commands/Else.cs b/Assets/Fungus/FungusScript/Commands/Else.cs index 29ab29ec..57422316 100644 --- a/Assets/Fungus/FungusScript/Commands/Else.cs +++ b/Assets/Fungus/FungusScript/Commands/Else.cs @@ -11,8 +11,7 @@ namespace Fungus { public override void OnEnter() { - Sequence sequence = GetSequence(); - if (sequence == null) + if (parentSequence == null) { return; } @@ -20,7 +19,7 @@ namespace Fungus // Find the next EndIf command at the same indent level as this Else command bool foundThisCommand = false; int indent = indentLevel; - foreach(Command command in sequence.commandList) + foreach(Command command in parentSequence.commandList) { if (foundThisCommand && command.indentLevel == indent) diff --git a/Assets/Fungus/FungusScript/Commands/If.cs b/Assets/Fungus/FungusScript/Commands/If.cs index 3d7b86b6..be8aaf2e 100644 --- a/Assets/Fungus/FungusScript/Commands/If.cs +++ b/Assets/Fungus/FungusScript/Commands/If.cs @@ -33,8 +33,7 @@ namespace Fungus public override void OnEnter() { - Sequence sequence = GetSequence(); - if (sequence == null) + if (parentSequence == null) { return; } @@ -143,7 +142,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 sequence.commandList) + foreach(Command command in parentSequence.commandList) { if (foundThisCommand && command.indentLevel == indent) diff --git a/Assets/Fungus/FungusScript/Scripts/Command.cs b/Assets/Fungus/FungusScript/Scripts/Command.cs index fd81f9eb..a4065832 100644 --- a/Assets/Fungus/FungusScript/Scripts/Command.cs +++ b/Assets/Fungus/FungusScript/Scripts/Command.cs @@ -31,31 +31,32 @@ namespace Fungus [HideInInspector] public bool selected; - public virtual Sequence GetSequence() - { - return gameObject.GetComponent(); - } + /** + * Reference to the Sequence object that this command belongs to. + * This reference is only set at runtime (null in editor). + */ + [NonSerialized] + public Sequence parentSequence; public virtual FungusScript GetFungusScript() { - Sequence s = GetSequence(); - if (s == null) + FungusScript fungusScript = GetComponent(); + if (fungusScript == null && + transform.parent != null) { - return null; + fungusScript = transform.parent.GetComponent(); } - - return s.GetFungusScript(); + return fungusScript; } public virtual bool IsExecuting() { - Sequence sequence = GetSequence(); - if (sequence == null) + if (parentSequence == null) { return false; } - return (sequence.activeCommand == this); + return (parentSequence.activeCommand == this); } public virtual void Execute() @@ -71,31 +72,28 @@ namespace Fungus public virtual void Continue(Command currentCommand) { OnExit(); - Sequence sequence = GetSequence(); - if (sequence != null) + if (parentSequence != null) { - sequence.ExecuteNextCommand(currentCommand); + parentSequence.ExecuteNextCommand(currentCommand); } } public virtual void Stop() { OnExit(); - Sequence sequence = GetSequence(); - if (sequence != null) + if (parentSequence != null) { - sequence.Stop(); + parentSequence.Stop(); } } public virtual void ExecuteSequence(Sequence s) { OnExit(); - Sequence sequence = GetSequence(); - if (sequence != null) + if (parentSequence != null) { - sequence.Stop(); - FungusScript fungusScript = sequence.GetFungusScript(); + parentSequence.Stop(); + FungusScript fungusScript = parentSequence.GetFungusScript(); if (fungusScript != null) { fungusScript.ExecuteSequence(s); diff --git a/Assets/Fungus/FungusScript/Scripts/CommandCopyBuffer.cs b/Assets/Fungus/FungusScript/Scripts/CommandCopyBuffer.cs index 7ae0442f..857a4af0 100644 --- a/Assets/Fungus/FungusScript/Scripts/CommandCopyBuffer.cs +++ b/Assets/Fungus/FungusScript/Scripts/CommandCopyBuffer.cs @@ -34,7 +34,7 @@ namespace Fungus return instance; } - protected virtual void Start() + protected override void Start() { if (Application.isPlaying) { diff --git a/Assets/Fungus/FungusScript/Scripts/FungusScript.cs b/Assets/Fungus/FungusScript/Scripts/FungusScript.cs index eb0723ed..98a169fd 100644 --- a/Assets/Fungus/FungusScript/Scripts/FungusScript.cs +++ b/Assets/Fungus/FungusScript/Scripts/FungusScript.cs @@ -148,7 +148,7 @@ namespace Fungus { // Sequence must be a child of the parent Fungus Script if (sequence == null || - sequence.transform.parent != transform) + sequence.transform.parent != transform && sequence.transform != transform) { return; } diff --git a/Assets/Fungus/FungusScript/Scripts/Sequence.cs b/Assets/Fungus/FungusScript/Scripts/Sequence.cs index 4676c434..ca1668f7 100644 --- a/Assets/Fungus/FungusScript/Scripts/Sequence.cs +++ b/Assets/Fungus/FungusScript/Scripts/Sequence.cs @@ -20,6 +20,15 @@ namespace Fungus protected int executionCount; + protected virtual void Start() + { + // Give each child command a reference back to its parent sequence + foreach (Command command in commandList) + { + command.parentSequence = this; + } + } + public virtual FungusScript GetFungusScript() { FungusScript fungusScript = GetComponent();