I am experiencing an issue with enums and I can't seem to pinpoint the cause.
My data flow is this
-> PlayerManager confirms a move (BattlePlan) for a unit
-> UnitController sends this BattlePlan to the MatchManager on the host
-> MatchManager sets an order index (and performs a few local calculations) and sends the final BattlePlan data back to the client
-> UnitController updates the BattlePlan with the new values
public enum AbilityLocation : int
{
Default = 0,
Hand = 1
}
PlayerManager:
Unit.tno.Send("HostRfcSetBattlePlan", Target.Host, location, index, x, z);
UnitController:
[RFC]
public void RfcSetBattlePlan(int orderIndex, AbilityLocation location, int index, int targetX, int targetZ)
{
Debug.Log($"Client battle plan from {tno.uid}: {location} {index} {targetX} {targetZ} (Order Index: {orderIndex})");
var battlePlan
= new BattlePlan
(tno
.uid, location, index, targetX, targetZ
) { OrderIndex
= orderIndex
} ;
Main.Match.UpdateBattlePlan(battlePlan);
}
[RFC]
public void HostRfcSetBattlePlan(AbilityLocation location, int index, int targetX, int targetZ)
{
var battlePlan
= new BattlePlan
(tno
.uid, location, index, targetX, targetZ
); Debug.Log($"New battle plan from {tno.uid}: {battlePlan.Location} {battlePlan.Index} {battlePlan.TargetX} {battlePlan.TargetZ} (Order Index: {battlePlan.OrderIndex})");
Main.Match.HostUpdateBattlePlan(battlePlan);
}
MatchManager:
public void HostUpdateBattlePlan(BattlePlan battlePlan)
{
UpdateBattlePlan(battlePlan);
Debug.Log($"Host battle plan from {battlePlan.Creator.tno.uid}: {battlePlan.Location} {battlePlan.Index} {battlePlan.TargetX} {battlePlan.TargetZ} (Order Index: {battlePlan.OrderIndex})");
battlePlan.Creator.tno.Send("RfcSetBattlePlan", Target.AllSaved, battlePlan.OrderIndex, battlePlan.Location, battlePlan.Index, battlePlan.TargetX, battlePlan.TargetZ);
}
The debug output:
New battle plan
from 16777214: Hand
0 1 1 (Order Index
: -1) Host battle plan from 16777214: Hand 0 1 1 (Order Index: 0) // (order index set by the host)
Client battle plan from 16777214: Default 0 1 1 (Order Index: 0)
So for some reason the enum value "Hand" reaches the server just fine, but turns into "Default" when it gets send back to the clients. If I, however, cast my enum to an integer when sending it to the clients and back to an enum on the client, it does work and the output becomes this:
New battle plan
from 16777214: Hand
0 1 1 (Order Index
: -1) Host battle plan from 16777214: Hand 0 1 1 (Order Index: 0)
Client battle plan from 16777214: Hand 0 1 1 (Order Index: 0)
I don't understand why it would work in one direction and not the other. The fact that manual casts to int seem to fix the issue makes me think it might not be my code.
I'm happy for any input on the matter.