@@ -80,17 +80,29 @@ public PlayerState CurrentPlayerState {
8080 }
8181 }
8282
83- public int CurrentPlayer {
84- get => _currentPlayer ;
85- set {
86- if ( ! PlayerStates . ContainsKey ( value ) ) {
87- Debug . LogError ( $ "Cannot change to non-existing player { value } .") ;
88- return ;
89- }
90- var previousPlayer = _currentPlayer ;
91- _currentPlayer = value ;
92- if ( previousPlayer != _currentPlayer ) {
93- EventBus . Trigger ( VisualScriptingEventNames . CurrentPlayerChanged , EventArgs . Empty ) ;
83+ public void SetCurrentPlayer ( int value , bool forceNotify = false )
84+ {
85+ if ( ! PlayerStates . ContainsKey ( value ) ) {
86+ Debug . LogError ( $ "Cannot change to non-existing player { value } .") ;
87+ return ;
88+ }
89+ var previousPlayer = _currentPlayer ;
90+ _currentPlayer = value ;
91+ if ( forceNotify || previousPlayer != _currentPlayer ) {
92+ EventBus . Trigger ( VisualScriptingEventNames . CurrentPlayerChanged , EventArgs . Empty ) ;
93+ }
94+
95+ // also trigger updates for each variable
96+ foreach ( var varDef in PlayerVariableDefinitions ) {
97+ if ( PlayerStates . ContainsKey ( previousPlayer ) ) {
98+ var before = PlayerStates [ previousPlayer ] . GetVariable ( varDef . Id ) ;
99+ var now = PlayerStates [ _currentPlayer ] . GetVariable ( varDef . Id ) ;
100+ if ( forceNotify || before != now ) {
101+ EventBus . Trigger ( VisualScriptingEventNames . PlayerVariableChanged , new VariableChangedArgs ( varDef . Id ) ) ;
102+ }
103+
104+ } else {
105+ EventBus . Trigger ( VisualScriptingEventNames . PlayerVariableChanged , new VariableChangedArgs ( varDef . Id ) ) ;
94106 }
95107 }
96108 }
@@ -109,10 +121,16 @@ public void CreatePlayerState(int playerId)
109121
110122 // switch to this state if current state is invalid
111123 if ( ! PlayerStates . ContainsKey ( _currentPlayer ) ) {
112- CurrentPlayer = playerId ;
124+ SetCurrentPlayer ( playerId , true ) ;
113125 }
114126 }
115127
128+ public void DestroyPlayerStates ( )
129+ {
130+ PlayerStates . Clear ( ) ;
131+ _currentPlayer = 0 ;
132+ }
133+
116134 public void OnInit ( Player player , TableApi tableApi , BallManager ballManager )
117135 {
118136 _player = player ;
@@ -191,3 +209,4 @@ public void OnAfterDeserialize()
191209 }
192210 }
193211}
212+
0 commit comments