Jump to content

Doubts about how Postmessage works


Recommended Posts

Hello folks,

I'm starting to work the 0ad code, trying to figure out how it works so excuse if my questions seems lame or have a very obvious answer.

think I got the big picture of how Postmessage works, however, I still have some doubts about its functionallity. Reading the comments in the code, they state that the message will be send to the entity (first parameter) and its components, Where could I find which components are associated to an entity? In my case, I'm trying to catch a message sent by ProductionQueue in Player component (as it is done in AIProxy). I cannot find the link between ProductionQueue and AIProxy in order to reproduce it (or not, maybe this is not the proper way) with Player component.

Thank you!

  • Like 1
Link to comment
Share on other sites

That's actually something that is quite not-obious-at-all (and don't be afraid to ask anyway).

So basically calling "PostMessage" will trigger Script_PostMessage in source/simulation/system/componentManager.cpp . This will in turn trigger PostMessage (this time in the c++).

Now the c++ "PostMessage" will iterate over a list of all components of that entity that subscribed to such a type of message (remember that each entity has its own subset of components. Things like the ResourceGatherer Component are actually created for all entities). For each of these Components, it will call the c++ function "HandleMessage". Usually this was automatically defined by a macro on creation, which calls "On--Whatever the message type--()" (like onResourceSupplyChanged() for example). Thus the JS function gets called.

So here's your link between AIProxy and ProductionQueue: one entity has both a AIProxy and a ProductionQeeue Component. The Production queue will "PostMessage", what I just described above will happen, and AiProxy will see "OnWhatever()" called.

(note: I'm pretty sure I'm right about this, but I may have said a few things that are wrong, so take it with a grain of salt).

Link to comment
Share on other sites

Thank you for your explanation wraitii,

So, if I understand you, AIProxy and ProductionQueue components are created for all entities (as, for example, ResourceGatherer). It implies that when a postMessage is sent from within ProductionQueue's method using this.entity as first parameter, the message will be sent to the ProductionQueue "container entity" and, in extension, to entity's "contained components" (AIProxy among them).

That clarifies the mechanic regarding messages, now I wonder, How do I find which components are associated to an entity? I'm not looking for a getComponents method but some of documentation, even if it is only a comment in the code.

Link to comment
Share on other sites

I'm not quite sure I understand what you call the "container entity". Basically all entities have components associated with them (some more than others: some entities have no "UnitAI" component for example). And when you call Engine.QueryInterface, you provide an ID (the ID of the entity) and a component definition(it's actually a number). The component manager will then return the right one.

To get the components associated to an entity, I fear you'll just have to try each of them (check GUIInterface.hs, the "GetEntityState" method).

Link to comment
Share on other sites

Sorry for the confusing terminology. When I said "container entity" of a component, I was refering to the entity that has that component asociated (among other components).

I am currently using GetEntityState to discover the components of an entity, so, I'm afraid that this makes me wonder a new question (sorry for being so inquisitive), what if I want to add a component to an entity? Is that possible or it a desing question completly out of my reach?

There is no problem in that of course, I just want to know which are my tools in order to make the best code possible ;)

Link to comment
Share on other sites

I believe that components are determined by the templates. I've never tried to add one at runtime, it might work, look for functions in ComponentManager.cpp (the ones starting with script_ in particular as they're linking JS and C++).

(I'm not too knowledgeable about that particular part of the code either, I must say :) )

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...