File tree Expand file tree Collapse file tree
StateGraph/Extensions/ScriptableCommandExtension Expand file tree Collapse file tree Original file line number Diff line number Diff line change 11# Change Log
22
3+ ## [ 0.8.2-beta] - Sept 09, 2024
4+ - Added TryGetData and HasData methods to the SharedData class
5+
6+ ## [ 0.8.1-beta] - Aug 25, 2024
7+ - Refactored the CommandState (Available when using the ScriptableCommands package) to better handle exceptions and clean up of cancellation tokens.
8+
39## [ 0.8.0-beta] - Aug 25, 2024
410- Added support for shared data between states. The SharedData object is a generic data store.
511 - States can access shared data via ` this.SharedData `
Original file line number Diff line number Diff line change @@ -10,6 +10,8 @@ public interface ISharedData
1010 event Action OnDataCleared ;
1111
1212 T GetData < T > ( string key ) ;
13+ bool TryGetData < T > ( string key , out T value ) ;
14+ bool HasData ( string key ) ;
1315 void SetData < T > ( string key , T value ) ;
1416 void ClearData ( ) ;
1517 }
@@ -30,9 +32,9 @@ public T GetData<T>(string key)
3032 {
3133 if ( ! _data . TryGetValue ( key , out var value ) ) return default ;
3234
33- if ( value is T tValue )
35+ if ( value is T typedValue )
3436 {
35- return tValue ;
37+ return typedValue ;
3638 }
3739
3840 throw new InvalidOperationException ( $ "Attempted to retrieve type { typeof ( T ) } but data is of type { value . GetType ( ) } ") ;
@@ -42,6 +44,41 @@ public T GetData<T>(string key)
4244 _lock . ExitReadLock ( ) ;
4345 }
4446 }
47+
48+ public bool TryGetData < T > ( string key , out T value )
49+ {
50+ _lock . EnterReadLock ( ) ;
51+
52+ try
53+ {
54+ if ( _data . TryGetValue ( key , out var objValue ) && objValue is T typedValue )
55+ {
56+ value = typedValue ;
57+ return true ;
58+ }
59+
60+ value = default ;
61+ return false ;
62+ }
63+ finally
64+ {
65+ _lock . ExitReadLock ( ) ;
66+ }
67+ }
68+
69+ public bool HasData ( string key )
70+ {
71+ _lock . EnterReadLock ( ) ;
72+
73+ try
74+ {
75+ return _data . ContainsKey ( key ) ;
76+ }
77+ finally
78+ {
79+ _lock . ExitReadLock ( ) ;
80+ }
81+ }
4582
4683 public void SetData < T > ( string key , T value )
4784 {
Original file line number Diff line number Diff 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}
Original file line number Diff line number Diff line change 11{
22 "name" : " com.nonatomic.visualstatemachinev2" ,
3- "version" : " 0.8.0 -beta" ,
3+ "version" : " 0.8.2 -beta" ,
44 "displayName" : " Visual State Machine V2" ,
55 "description" : " Visual State Machine V2" ,
66 "unity" : " 2022.3" ,
You can’t perform that action at this time.
0 commit comments