Jump to content

Question on Events, Game States, and EventData Reference vs Copy


abeclancy

Recommended Posts

So while working on an event for SoldierTacticalToStrategy, I came across an "interesting" behavior that I'd like to get a clarification on. Basically, why does the EventData for an event not come "up-to-date" when the event is triggered? Is it intentional that the game state "snapshots" the object, and uses that as the EventData for events?

 

Here's a very simple event handler:

class TestClassForEvent extends Object;

function EventListenerReturn OnSoldierTacticalToStrategy(Object EventData, Object EventSource, XComGameState GameState, Name EventID) {
    local XComGameState_Unit UnitState;
    UnitState = XComGameState_Unit(EventData);
    `log("EVENTDATA NUM MISSIONS:" @ UnitState.iNumMissions);
    UnitState = XComGameState_Unit(GameState.GetGameStateForObjectID(UnitState.ObjectID));
    `log("GAMESTATE UNIT NUM MISSIONS:" @ UnitState.iNumMissions);
    return ELR_NoInterrupt;
}

The basic premise is simple: Convert EventData to an XComGameState_Unit type, print iNumMissions, then do the same for the instance of this ObjectID that is within the game state already.

 

Here's the code around where the event is triggered, just to showcase some extra logging statements:

NewGameState.AddStateObject(UnitState);
UnitState.iNumMissions++; // was 7, now 8
...

testc = new class'TestClassForEvent';
testco = testc;
EventManager.RegisterForEvent(testco, 'SoldierTacticalToStrategy', testc.OnSoldierTacticalToStrategy, ELD_Immediate, , , true);

`log("NUM MISSIONS BEFORE SET:" @ UnitState.iNumMissions);
UnitState.iNumMissions = 9;
`log("NUM MISSIONS AFTER SET:" @ UnitState.iNumMissions);
EventManager.TriggerEvent('SoldierTacticalToStrategy', UnitState, , NewGameState);
`log("NUM MISSIONS AFTER CALLBACK:" @ UnitState.iNumMissions);

So basically, set up the event register, change the value of iNumMissions, and trigger the event.

 

Very simple stuff, but here's the actual log for this exchange:

NUM MISSIONS BEFORE SET: 8
NUM MISSIONS AFTER SET: 9
EVENTDATA NUM MISSIONS: 7
GAMESTATE UNIT NUM MISSIONS: 9
NUM MISSIONS AFTER CALLBACK: 9

= The value is 8. Starting value, so whatever.

= The value is 9. I manually set it to 9.

= The value is 7. This is actually from earlier, before it was incremented. So apparently a "snapshot" of UnitState was taken at that time, and even though the theoretically "current" version was used as the second parameter of the TriggerEvent() function, the snapshot version is sent to the registered handler for the event?

= The value is 9. So I can access the edited, "current" version of the UnitState manually?

= The value is 9. As it should be, since this is the "current" version of UnitState.

 

So what's happening here? Is this normal that a snapshot of certain objects (eg XComGameState objects) is taken when the object is added to the game state, and is used in the event handler, but that you CAN look for and access the most recent data if you want to? Can I assume that these snapshots don't apply if the EventData is not a game state object?

 

Link to comment
Share on other sites

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...