Skip to content

Commit 73d5600

Browse files
committed
Provide both old and new value in variables changed event.
1 parent d511f99 commit 73d5600

File tree

4 files changed

+45
-19
lines changed

4 files changed

+45
-19
lines changed

Editor/Descriptors/VariableChangedEventUnitDescriptor.cs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,10 @@ protected override void DefinedPort(IUnitPort port, UnitPortDescription desc)
3939
base.DefinedPort(port, desc);
4040

4141
switch (port.key) {
42-
case nameof(PlayerVariableChangedEventUnit.Value):
42+
case nameof(PlayerVariableChangedEventUnit.OldValue):
43+
desc.summary = "The previous value of the player variable.";
44+
break;
45+
case nameof(PlayerVariableChangedEventUnit.NewValue):
4346
desc.summary = "The new value of the player variable.";
4447
break;
4548
}
@@ -66,7 +69,10 @@ protected override void DefinedPort(IUnitPort port, UnitPortDescription desc)
6669
base.DefinedPort(port, desc);
6770

6871
switch (port.key) {
69-
case nameof(TableVariableChangedEventUnit.Value):
72+
case nameof(TableVariableChangedEventUnit.OldValue):
73+
desc.summary = "The previous value of the table variable.";
74+
break;
75+
case nameof(TableVariableChangedEventUnit.NewValue):
7076
desc.summary = "The new value of the table variable.";
7177
break;
7278
}

Runtime/Gamelogic/State.cs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ public void Set<T>(string variableId, T value) where T : class
100100
if (currentValue != value) {
101101
EventBus.Trigger(
102102
VariableChangedEventName,
103-
new VariableChangedArgs(variableId)
103+
new VariableChangedArgs(variableId, currentValue, value)
104104
);
105105
}
106106
}
@@ -157,9 +157,14 @@ public readonly struct VariableChangedArgs
157157
{
158158
public readonly string VariableId;
159159

160-
public VariableChangedArgs(string variableId)
160+
public readonly object OldValue;
161+
public readonly object NewValue;
162+
163+
public VariableChangedArgs(string variableId, object oldValue, object newValue)
161164
{
162165
VariableId = variableId;
166+
OldValue = oldValue;
167+
NewValue = newValue;
163168
}
164169
}
165170
}

Runtime/Gamelogic/VisualScriptingGamelogicEngine.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -104,15 +104,15 @@ public void SetCurrentPlayer(int value, bool forceNotify = false)
104104

105105
// also trigger updates for each variable
106106
foreach (var varDef in PlayerVariableDefinitions) {
107+
var before = PlayerStates[previousPlayer].GetVariable(varDef.Id);
108+
var now = PlayerStates[_currentPlayer].GetVariable(varDef.Id);
107109
if (PlayerStates.ContainsKey(previousPlayer)) {
108-
var before = PlayerStates[previousPlayer].GetVariable(varDef.Id);
109-
var now = PlayerStates[_currentPlayer].GetVariable(varDef.Id);
110110
if (forceNotify || before != now) {
111-
EventBus.Trigger(VisualScriptingEventNames.PlayerVariableChanged, new VariableChangedArgs(varDef.Id));
111+
EventBus.Trigger(VisualScriptingEventNames.PlayerVariableChanged, new VariableChangedArgs(varDef.Id, before.Get<object>(), now.Get<object>()));
112112
}
113113

114114
} else {
115-
EventBus.Trigger(VisualScriptingEventNames.PlayerVariableChanged, new VariableChangedArgs(varDef.Id));
115+
EventBus.Trigger(VisualScriptingEventNames.PlayerVariableChanged, new VariableChangedArgs(varDef.Id, before.Get<object>(), now.Get<object>()));
116116
}
117117
}
118118
}

Runtime/Nodes/PlayerState/VariableChangedEventUnit.cs

Lines changed: 26 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,11 @@ public class TableVariableChangedEventUnit : VariableChangedEventUnit
4747

4848
public abstract class VariableChangedEventUnit : GleEventUnit<VariableChangedArgs>
4949
{
50-
[DoNotSerialize, PortLabel("Value"), Inspectable]
51-
public ValueOutput Value { get; private set; }
50+
[DoNotSerialize, PortLabel("Old Value"), Inspectable]
51+
public ValueOutput OldValue { get; private set; }
52+
53+
[DoNotSerialize, PortLabel("New Value"), Inspectable]
54+
public ValueOutput NewValue { get; private set; }
5255

5356
public override EventHook GetHook(GraphReference reference) => new(EventHookName);
5457
protected override bool register => true;
@@ -65,11 +68,19 @@ protected override void Definition()
6568
return;
6669
}
6770

68-
Value = VariableDefinition.Type switch {
69-
VariableType.String => ValueOutput<string>(nameof(Value)),
70-
VariableType.Integer => ValueOutput<int>(nameof(Value)),
71-
VariableType.Float => ValueOutput<float>(nameof(Value)),
72-
VariableType.Boolean => ValueOutput<bool>(nameof(Value)),
71+
OldValue = VariableDefinition.Type switch {
72+
VariableType.String => ValueOutput<string>(nameof(OldValue)),
73+
VariableType.Integer => ValueOutput<int>(nameof(OldValue)),
74+
VariableType.Float => ValueOutput<float>(nameof(OldValue)),
75+
VariableType.Boolean => ValueOutput<bool>(nameof(OldValue)),
76+
_ => throw new ArgumentOutOfRangeException()
77+
};
78+
79+
NewValue = VariableDefinition.Type switch {
80+
VariableType.String => ValueOutput<string>(nameof(NewValue)),
81+
VariableType.Integer => ValueOutput<int>(nameof(NewValue)),
82+
VariableType.Float => ValueOutput<float>(nameof(NewValue)),
83+
VariableType.Boolean => ValueOutput<bool>(nameof(NewValue)),
7384
_ => throw new ArgumentOutOfRangeException()
7485
};
7586
}
@@ -87,16 +98,20 @@ protected override void AssignArguments(Flow flow, VariableChangedArgs args)
8798

8899
switch (VariableDefinition.Type) {
89100
case VariableType.String:
90-
flow.SetValue(Value, State.Get<string>(VariableDefinition.Id));
101+
flow.SetValue(OldValue, (string)args.OldValue);
102+
flow.SetValue(NewValue, (string)args.NewValue);
91103
break;
92104
case VariableType.Integer:
93-
flow.SetValue(Value, (int)State.Get<Integer>(VariableDefinition.Id));
105+
flow.SetValue(OldValue, (int)(Integer)args.OldValue);
106+
flow.SetValue(NewValue, (int)(Integer)args.NewValue);
94107
break;
95108
case VariableType.Float:
96-
flow.SetValue(Value, (float)State.Get<Float>(VariableDefinition.Id));
109+
flow.SetValue(OldValue, (float)(Float)args.OldValue);
110+
flow.SetValue(NewValue, (float)(Float)args.NewValue);
97111
break;
98112
case VariableType.Boolean:
99-
flow.SetValue(Value, (bool)State.Get<Bool>(VariableDefinition.Id));
113+
flow.SetValue(OldValue, (bool)(Bool)args.OldValue);
114+
flow.SetValue(NewValue, (bool)(Bool)args.NewValue);
100115
break;
101116
default:
102117
throw new ArgumentOutOfRangeException();

0 commit comments

Comments
 (0)