Browse Source

Improve efficiency of executing next command in a Sequence #76

Switched to use command indices instead of scanning the command list to
find the current command.
master
chrisgregan 10 years ago
parent
commit
4ecee9c332
  1. 3
      Assets/Fungus/FungusScript/Scripts/Command.cs
  2. 33
      Assets/Fungus/FungusScript/Scripts/Commands/If.cs
  3. 46
      Assets/Fungus/FungusScript/Scripts/Sequence.cs

3
Assets/Fungus/FungusScript/Scripts/Command.cs

@ -37,6 +37,9 @@ namespace Fungus
[HideInInspector] [HideInInspector]
public int indentLevel; public int indentLevel;
[NonSerialized]
public int commandIndex;
/** /**
* Reference to the Sequence object that this command belongs to. * Reference to the Sequence object that this command belongs to.
* This reference is only set at runtime (null in editor). * This reference is only set at runtime (null in editor).

33
Assets/Fungus/FungusScript/Scripts/Commands/If.cs

@ -152,26 +152,35 @@ namespace Fungus
else else
{ {
// Find the next Else or EndIf command at the same indent level as this If command // Find the next Else or EndIf command at the same indent level as this If command
bool foundThisCommand = false; for (int i = commandIndex + 1; i < parentSequence.commandList.Count; ++i)
int indent = indentLevel;
foreach(Command command in parentSequence.commandList)
{ {
if (foundThisCommand && Command nextCommand = parentSequence.commandList[i];
command.indentLevel == indent)
// Find next command at same indent level as this If command
// Skip disabled commands & comments
if (!nextCommand.enabled ||
nextCommand.GetType() == typeof(Comment) ||
nextCommand.indentLevel != indentLevel)
{ {
System.Type type = command.GetType(); continue;
}
System.Type type = nextCommand.GetType();
if (type == typeof(Else) || if (type == typeof(Else) ||
type == typeof(EndIf) || // Legacy support for old EndIf command type == typeof(EndIf) || // Legacy support for old EndIf command
type == typeof(End)) type == typeof(End))
{ {
// Execute command immediately after the Else or EndIf command if (i >= parentSequence.commandList.Count - 1)
Continue(command); {
return; // Last command in Sequence, so stop
} Stop();
} }
else if (command == this) else
{ {
foundThisCommand = true; // Execute command immediately after the Else or End command
Continue(nextCommand);
return;
}
} }
} }

46
Assets/Fungus/FungusScript/Scripts/Sequence.cs

@ -37,12 +37,28 @@ namespace Fungus
protected virtual void Awake() protected virtual void Awake()
{ {
// Give each child command a reference back to its parent sequence // Give each child command a reference back to its parent sequence
int index = 0;
foreach (Command command in commandList) foreach (Command command in commandList)
{ {
command.parentSequence = this; command.parentSequence = this;
command.commandIndex = index++;
} }
} }
#if UNITY_EDITOR
// The user can modify the command list order while playing in the editor,
// so we keep the command indices updated every frame. There's no need to
// do this in player builds so we compile this bit out for those builds.
void Update()
{
int index = 0;
foreach (Command command in commandList)
{
command.commandIndex = index++;
}
}
#endif
public virtual FungusScript GetFungusScript() public virtual FungusScript GetFungusScript()
{ {
FungusScript fungusScript = GetComponent<FungusScript>(); FungusScript fungusScript = GetComponent<FungusScript>();
@ -97,27 +113,27 @@ namespace Fungus
FungusScript fungusScript = GetFungusScript(); FungusScript fungusScript = GetFungusScript();
activeCommand = null; int commandIndex = 0;
Command nextCommand = null; if (currentCommand != null)
executingIconTimer = 0.5f;
bool executeNext = (currentCommand == null);
foreach (Command command in commandList)
{ {
if (command == currentCommand) commandIndex = currentCommand.commandIndex + 1;
{
executeNext = true;
} }
else if (executeNext)
{ while (commandIndex < commandList.Count &&
if (command.enabled && command.GetType() != typeof(Comment)) (!commandList[commandIndex].enabled || commandList[commandIndex].GetType() == typeof(Comment)))
{ {
nextCommand = command; commandIndex = commandList[commandIndex].commandIndex + 1;
break;
}
} }
Command nextCommand = null;
if (commandIndex < commandList.Count)
{
nextCommand = commandList[commandIndex];
} }
activeCommand = null;
executingIconTimer = 0.5f;
if (nextCommand == null) if (nextCommand == null)
{ {
Stop(); Stop();

Loading…
Cancel
Save