- Home /
Getting GameState property values after state is done updating
I have a GameState of properties. When I change a value, the property fires its own event. The event is being listened to by an observer script.
The observer script can assert when one value has changed. But when I make two consecutive changes to values, an event is fired before the GameState is done updating, and the observer can only see the value change for one property, not both.
How do I get the property values only after both value changes are complete?
GameState.cs
private int _x;
public event Action XChanged = () => { };
public int X
{
get => _x;
set
{
var x = _x;
_x = value;
if (value != x)
{
XChanged();
}
}
}
private int _y;
public event Action YChanged = () => { };
public int Y
{
get => _y;
set
{
var y = _y;
_y = value;
if (value != y)
{
YChanged();
}
}
}
Observer.cs
void StateValidator()
{
Assert.That(gameState.X, Is.EqualTo(1));
Assert.That(gameState.Y, Is.EqualTo(1));
}
gameStateService.State.XChanged += StateValidator;
gameStateService.State.YChanged += StateValidator;
shop.BuyXandY(1, 1); //buy 1x and 1y
Transaction.cs
public void BuyXandY(int xAmount, int yAmount)
{
GameStateService.Get().State.X += xAmount;
GameStateService.Get().State.Y += yAmount;
}
Your answer
Follow this Question
Related Questions
NullReferenceException when call an event 1 Answer
How do I modify an existing default component? 0 Answers
How do I create public read-only properties? 3 Answers
iPhoneKeyboard Set Text after validation 1 Answer
How do I expose public properties in the same way the inspector does via a gui in game? 1 Answer