Skip to content

Commit 0b8dd5d

Browse files
committed
Create PauseFlowCharUntilCondition.cs
1 parent 4b04491 commit 0b8dd5d

File tree

1 file changed

+125
-0
lines changed

1 file changed

+125
-0
lines changed

PauseFlowCharUntilCondition.cs

+125
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
// This code is part of the Fungus library (https://github.com/snozbot/fungus)
2+
// It is released for free under the MIT open source license (https://github.com/snozbot/fungus/blob/master/LICENSE)
3+
4+
using UnityEngine;
5+
using UnityEngine.UI;
6+
using UnityEngine.Serialization;
7+
using System.Collections;
8+
using System.Collections.Generic;
9+
10+
namespace Fungus
11+
{
12+
/// <summary>
13+
/// Waits for period of time before executing the next command in the block.
14+
/// </summary>
15+
[CommandInfo("Flow",
16+
"Pause flowchart Until Condition",
17+
"Waits for period of time before executing the next command in the block. This also mean, you can execute blocks in parallel, while waiting. FYI, this a single instance command")]
18+
[AddComponentMenu("")]
19+
[ExecuteInEditMode]
20+
public class PauseFlowCharUntilCondition : Command
21+
{
22+
private static bool flowIsPaused = false;
23+
[HideInInspector] protected string key = "";
24+
[Tooltip("Variable to store the value in.")]
25+
[VariableProperty(typeof(IntegerVariable))]
26+
[SerializeField] protected Variable variable;
27+
[SerializeField] protected int valueComparer;
28+
[SerializeField] protected string blockName;
29+
30+
protected virtual IEnumerator WhilePause()
31+
{
32+
if (variable != null && valueComparer != 0)
33+
{
34+
if (blockName != "")
35+
{
36+
key = variable.name;
37+
if (key == "" ||
38+
variable == null)
39+
{
40+
Continue();
41+
yield return null;
42+
}
43+
var flowchart = GetFlowchart();
44+
flowIsPaused = true;
45+
46+
// Prepend the current save profile (if any)
47+
string prefsKey = SetSaveProfile.SaveProfile + "_" + flowchart.SubstituteVariables(key);
48+
System.Type variableType = variable.GetType();
49+
IntegerVariable integerVariable = variable as IntegerVariable;
50+
//FloatVariable floatVariable = variable as FloatVariable;
51+
52+
//Call another block based on it's name
53+
Flowchart flowcharty = GetComponent<Flowchart>();
54+
flowcharty.ExecuteBlock(blockName);
55+
56+
//Debug.Log("after block was called");
57+
58+
WaitForSeconds waiting = new WaitForSeconds(0.1f);
59+
60+
//Halts the flow, waits until condition is met
61+
while (flowIsPaused)
62+
{
63+
//You don't have to check every frame, thus this sort of needed for easing cpu
64+
yield return waiting;
65+
66+
if (valueComparer == integerVariable.Value)
67+
{
68+
MoveOn();
69+
//Debug.Log("Condition was met! SUCCESS");
70+
yield break;
71+
}
72+
}
73+
}
74+
}
75+
}
76+
77+
protected void MoveOn()
78+
{
79+
flowIsPaused = false;
80+
StopAllCoroutines();
81+
Continue();
82+
}
83+
84+
#region Public members
85+
86+
public override void OnEnter()
87+
{
88+
if (variable != null && valueComparer != 0)
89+
{
90+
StartCoroutine(WhilePause());
91+
}
92+
else
93+
{
94+
Continue();
95+
}
96+
}
97+
public override string GetSummary()
98+
{
99+
string noVar = "";
100+
string noVal = "";
101+
if (valueComparer == 0)
102+
{
103+
noVal = "Variable cannot be empty or 0!";
104+
}
105+
if (valueComparer == 0)
106+
{
107+
noVar = "No variable selected!";
108+
}
109+
return noVar + " : " + noVal;
110+
}
111+
112+
public override Color GetButtonColor()
113+
{
114+
return new Color32(235, 191, 217, 255);
115+
}
116+
117+
public override bool HasReference(Variable in_variable)
118+
{
119+
return this.variable == in_variable ||
120+
base.HasReference(in_variable);
121+
}
122+
123+
#endregion
124+
}
125+
}

0 commit comments

Comments
 (0)