Skip to content

Commit 44a8974

Browse files
committed
Use sim time
1 parent 10b6e1b commit 44a8974

File tree

8 files changed

+52
-20
lines changed

8 files changed

+52
-20
lines changed

Doc/QuickStart.md

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,18 @@
55

66
Be welcome! Here is a hands-on introduction to Active Logic. Let's get started.
77

8-
This introduction is engine agnostic; if you are using Unity, read the [Unity Quick Start Guide](QuickStart-Unity.md).
8+
This introduction is engine agnostic. Unity users may prefer the [Unity Quick Start Guide](QuickStart-Unity.md).
9+
10+
## Setting the time
11+
12+
Before evaluating a behaviour tree, set the time:
13+
14+
```
15+
// Set at game frame start or before invoking your ticker
16+
SimTime.time = System.DateTime.Now.TotalMilliseconds/1000f;
17+
```
18+
19+
Active Logic uses [simulated time](Reference/SimTime.md) to evaluate certain decorators, such as intervals and cooldowns.
920

1021
## Status expressions and the update loop
1122

Doc/Reference/SimTime.md

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
*Sources: SimTime.cs - Last Updated: 2021.07.18*
2+
3+
# SimTime Reference
4+
5+
In order for time-reliant decorators (cooldown, delay, interval, timeout, wait and perhaps others) to work as expected, the time should be correctly set.
6+
7+
- In a video game, usually you want game time; this (ordinarily) is different from real time.
8+
- When evaluating a simulation, set the time at the beginning of each iteration.
9+
- For some applications, real time may be just what you need.
10+
11+
## Setting the time
12+
13+
Depending on the intended use case, there's a couple of approaches to setting the time:
14+
15+
1- If all the agents/tickers in your game use the same time, consider setting `SimTime.time` at the beginning of each iteration (or game/physics frame)
16+
17+
2- In less common cases, different agents may each use their own time. For example, in a turn based game, 'tactical time' (round count) may be used to evaluate some agents while other parts of your game may use real time for animation.
18+
19+
**Sim time is not thread safe** - you may use sim time in a multi-threaded environment, provided all agents use the same time, and all threads complete work within the current iteration.
20+
21+
Note: in *Unity*, time defaults to `UnityEngine.Time.time`; you may change this on a stepper basis (see [Steppers](Unity/Steppers.md))
22+
23+
## Fields & Properties
24+
25+
`time` - get/set the current time.

Src/SimTime.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
// Doc/Reference/SimTime.md
2+
namespace Active.Core{
3+
public static class SimTime{
4+
5+
public static float time;
6+
7+
}}

Src/Stateful/AbstractDecorator.cs

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,7 @@ namespace Active.Core{
99
public abstract partial class AbstractDecorator : IDecorator,
1010
Resettable{
1111

12-
internal virtual float time{ get{
13-
#if UNITY_2018_1_OR_NEWER
14-
return UnityEngine.Time.time;
15-
#else
16-
return System.DateTime.Now.Millisecond/1000f;
17-
#endif
18-
}}
12+
internal float time => SimTime.time;
1913

2014
protected static int ID(int id) => id == 0 ? ++MaxId : id;
2115

Tests/Decorators/AbstractDecoratorTest.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,10 @@
66
public class AbstractDecoratorTest
77
: DecoratorTest<AbstractDecoratorTest.C>{
88

9-
[Test] public void Time() => o( x.GetTime() != 0 );
9+
[Test] public void Time(){
10+
SimTime.time = 10f;
11+
o( x.GetTime(), 10f );
12+
}
1013

1114
public class C : AbstractDecorator{
1215
public C(){}

Tests/Decorators/TestCooldown.cs

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -92,15 +92,10 @@ [Test] public void Format(){
9292
#endif
9393

9494
public class Cooldown : Active.Core.Cooldown{
95-
public int time_;
9695
public Cooldown() : base(){}
9796
public Cooldown(float duration) : base(duration){}
98-
override internal float time => time_;
9997
}
10098

101-
int t{
102-
set => x.time_ = value;
103-
get => x.time_;
104-
}
99+
int t{ set => SimTime.time = value; }
105100

106101
}

Tests/Decorators/TestInterval.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ public class TestInterval : DecoratorTest<TestInterval.Interval> {
1010

1111
bool _log;
1212

13-
static float t; // Time.time value for testing
13+
static float t{ set => SimTime.time = value; }
1414

1515
[SetUp] override public void Setup(){
1616
t = 0;
@@ -132,7 +132,6 @@ public class Interval : Active.Core.Interval{
132132
public Interval() : base(){}
133133
public Interval(float period, float offset=0f)
134134
: base(period, offset){}
135-
override internal float time => TestInterval.t;
136135
}
137136

138137
}

Tests/Decorators/TestTimeout.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
/* Timeout limits the duration of the target task */
66
public class TestTimeout : DecoratorTest<TestTimeout.Timeout> {
77

8-
static float t; // Replaces Time.time (see derived class at end)
8+
static float t{ set => SimTime.time = value; }
99

1010
[SetUp] override public void Setup(){ t = 0; base.Setup(); }
1111

@@ -66,10 +66,8 @@ [Test] public void DisarmedOnFail(){
6666
}
6767

6868
public class Timeout : Active.Core.Timeout{
69-
public float time_ => TestTimeout.t;
7069
public Timeout() : base(){}
7170
public Timeout(float duration) : base(duration){}
72-
override internal float time => time_;
7371
}
7472

7573
}

0 commit comments

Comments
 (0)