Skip to content

Commit 957ab1f

Browse files
committed
Random chance now supports seeds
1 parent e3f8144 commit 957ab1f

File tree

2 files changed

+25
-5
lines changed

2 files changed

+25
-5
lines changed

Assets/FluidBehaviorTree/Scripts/BehaviorTree/BehaviorTreeBuilderBase.cs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -102,18 +102,19 @@ public T Condition (Func<bool> action) {
102102
return Condition("condition", action);
103103
}
104104

105-
public T RandomChance (string name, int chance, int outOf) {
105+
public T RandomChance (string name, int chance, int outOf, int seed = 0) {
106106
_tree.AddNode(Pointer, new RandomChance {
107107
Name = name,
108108
chance = chance,
109-
outOf = outOf
109+
outOf = outOf,
110+
seed = seed
110111
});
111112

112113
return (T)this;
113114
}
114115

115-
public T RandomChance (int chance, int outOf) {
116-
return RandomChance("random chance", chance, outOf);
116+
public T RandomChance (int chance, int outOf, int seed = 0) {
117+
return RandomChance("random chance", chance, outOf, seed);
117118
}
118119

119120
public T Wait (string name, int turns = 1) {

Assets/FluidBehaviorTree/Scripts/Tasks/Conditions/RandomChance.cs

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,32 @@
22

33
namespace Adnc.FluidBT.Tasks {
44
public class RandomChance : ConditionBase {
5+
private Random.State _state;
6+
57
public float chance = 1;
68
public float outOf = 1;
7-
9+
public int seed;
10+
11+
protected override void OnInit () {
12+
var oldState = Random.state;
13+
14+
if (seed != 0) {
15+
Random.InitState(seed);
16+
}
17+
18+
_state = Random.state;
19+
Random.state = oldState;
20+
}
21+
822
protected override bool OnUpdate () {
23+
var oldState = Random.state;
24+
Random.state = _state;
25+
926
var percentage = chance / outOf;
1027
var rng = Random.value;
1128

29+
Random.state = oldState;
30+
1231
return rng <= percentage;
1332
}
1433
}

0 commit comments

Comments
 (0)