Hello, I am looking for a better method for passing variables (or components maybe) between objects in c++.
I already have a working messaging system completed, and I am using a component based architecture.
Surprisingly, I haven't found very much information on how to actually pass information from one object to another in the form of variables.
for example: Lets say I'm making a game, and mario needs to know how much time is left in a level. Lets say there is a Mario object and a Time object. The time object has a float time;, and mario needs to know about it in his update function. How would I go about retrieving that time value (or a pointer to it for that matter)?
I was thinking of just grabbing a pointer to the component that has the variable (with an Interface Component pointer), and then just type cast it to the right type of component to get the variable...
but one major downside to this method is the lack of flexibility. I would have to #include the data type of the component for every type of component I want to access... which would be daunting. Sure, I could just put them all inside a MainHeader.h and include it in every Component.h, but that would stifle my compile times, and every time I add a new #include, I would have to recompile the whole program @~@!
Another alternative I was considering, Is to just subscribe a pointer to the variable I want to a map of maps, kinda like a list of subscribers. An example would be map<string, map<string, void*>>; Where the first string would be the instance name of the object, and the second string would be the variable name, and the void* would be a pointer to the variable I want. I could subscribe the variable name to the list of map objects when the object is created, so that I can access it later. I could send a message to the message system requesting the variable in question, and retrieve it easily.
However... the downside to this method is the overhead... I would have to subscribe hundreds of variables every frame if I was creating a whole lot of objects, and I would be doing a lot of string comparisons... etc. However I cannot think of a more flexible method.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
What i'm really asking is... is there a better method to allow objects to flexibly access other objects variables?
Any code examples would really help me out.
Any help would be appreciated :3.