Skip to content

Commit 620f964

Browse files
committed
Refactor of CommandState
1 parent 384fc09 commit 620f964

2 files changed

Lines changed: 20 additions & 10 deletions

File tree

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
# Change Log
22

3+
## [0.8.1-beta] - Aug 25, 2024
4+
- Refactored the CommandState (Available when using the ScriptableCommands package) to better handle exceptions and clean up of cancellation tokens.
5+
36
## [0.8.0-beta] - Aug 25, 2024
47
- Added support for shared data between states. The SharedData object is a generic data store.
58
- States can access shared data via `this.SharedData`

Runtime/StateGraph/Extensions/ScriptableCommandExtension/CommandState.cs

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -21,24 +21,35 @@ public class CommandState : State
2121

2222
public override void OnEnterState()
2323
{
24-
ExecuteCommands();
24+
_cts = new CancellationTokenSource();
25+
_ = ExecuteCommands();
2526
}
2627

2728
public override void OnExitState()
2829
{
29-
//...
30+
if (_cts == null) return;
31+
32+
_cts.Cancel();
33+
_cts.Dispose();
34+
_cts = null;
3035
}
3136

32-
private async void ExecuteCommands()
37+
private async Task ExecuteCommands()
3338
{
34-
await ExecuteCommandsAsync();
39+
try
40+
{
41+
await ExecuteCommandsAsync();
42+
}
43+
catch (Exception ex)
44+
{
45+
Debug.LogError($"Error executing commands: {ex}");
46+
}
47+
3548
OnComplete?.Invoke();
3649
}
3750

3851
private async Task ExecuteCommandsAsync()
3952
{
40-
_cts = new CancellationTokenSource();
41-
4253
try
4354
{
4455
foreach (var command in _commands)
@@ -50,10 +61,6 @@ private async Task ExecuteCommandsAsync()
5061
{
5162
Debug.Log("Command execution was cancelled.");
5263
}
53-
finally
54-
{
55-
_cts?.Dispose();
56-
}
5764
}
5865
}
5966
}

0 commit comments

Comments
 (0)