A recent change to how Variant maps are indexed in Lua required me to go through my entire game and redo how events are sent. A common idiom of mine was to populate a variant map with data, then fire off an event.
local vm=VariantMap()
vm:SetBool("flag", true)
vm:SetVector3("pos", Vector3(1,1,1))
self.node:SendEvent("Event", vm)
The change to Lua variant map indexing broke this idiom, so this morning I went through and made the changes:
local vm=VariantMap()
vm["flag"]=true
vm["pos"]=Vector3(1,1,1)
self.node:SendEvent("Event", vm)
However, something has broken and I am not sure exactly what it is.
I have a component, written in C++, that performs a task of broadcasting an “AreYouReady” type of event, and any objects signed up to receive the event will send back a reply by populating a variant map with their node ID and sending off another event. The scheduler component listens for this event and inserts ready objects into a vector for later processing. Before the change to the variant map indexing, this was functioning as intended, but now it no longer is. The C++ component is essentially structured as so:
// Turn scheduler
TurnScheduler::TurnScheduler(Context *context) : LogicComponent(context)
{
SetUpdateEventMask(USE_UPDATE);
SubscribeToEvent(StringHash("RegisterReadyObject"), HANDLER(TurnScheduler, HandleRegisterReadyObject));
}
void TurnScheduler::Update(float dt)
{
SendEvent(CombatRequestStatus, vm);
}
void TurnScheduler::HandleRegisterReadyObject(StringHash eventType, VariantMap& eventData)
{
static StringHash ReadyID("ReadyID");
unsigned int id=eventData[ReadyID].GetUInt();
std::cout << "Ready object: " << id << std::endl;
// Add object to Ready vector
}
Objects then have a LuaScriptObject class that will subscribe to the CombatRequestStatus event, and fire off a RegisterReadyObject event if they are ready:
CombatCommandQueue=ScriptObject()
function CombatCommandQueue:Start()
self:SubscribeToEvent("CombatRequestStatus", "CombatCommandQueue:HandleRequestStatus")
self.ready=true
end
unction CombatCommandQueue:HandleRequestStatus(eventType, eventData)
if self.ready then
print("Sending ready for "..self.node:GetID())
self.vm["ReadyID"]=self.node:GetID()
self.node:SendEvent("RegisterReadyObject", self.vm)
end
end
Logging indicates that the HandleRequestStatus event is being received. However, when the RegisterReadyObject event is fired off, it is not being received by the turn scheduler anymore. I’ve experimented with populating the variant map with string hashes rather than strings:
self.vm[StringHash("ReadyID")=self.node:GetID()
and with sending a string hash of the RegisterReadyObject event instead of a regular string:
self.node:SendEvent(StringHash("RegisterReadyObject"), self.vm)
but to no avail. The events simply are not being received by the C++ component anymore. Anybody have any ideas on what’s going wrong?