Pointer and Value interchangeability
Members - Reputation: 135
Posted 12 April 2012 - 01:41 PM
I am not sure if this is the most fitting title for what I have going on, but couldn't think of any better.
I am working on a 2d scrolling shooter, and the way I have designed my entities/game objects, many times I encounter situations where I wish I could just use some variables/objects without having to know about it if it is a pointer or value. Now, I know this sounds really weird, but let me give an example:
I have a game object, destroyable.
It has a lists of colliders: shields, hull, weakPoints
if a shield is hit the damage is calculated based on the shield's own material properties, and the damage is exerted on the shield itself, not on the main destroyable object.
if a hull collider is hit the damage is calculated based on the destroyable's material properties (so for material they don't have their own, they need a pointer to the destroyable's material) and the calculated damage will be exerted on the destroyable (so they need a pointer for the destroyable's integrity, they don't have their own)
if a weakPoint is hit the damage is calculated based on the weakPoint's own material properties, but the damage exerted will be on the main destroyable, so they need a pointer for the destroyable's integrity.
As you can see the process of collision handling and taking damage is exactly the same in all three cases,but since in some cases a pointer to an object (material or integrity) needs to be used, and other times the actual value, each case needs to be treated separately and for all the combinations a separate function needs to be written.
Is some way I could make this such that when I give an integrity object or material object to a collider it can be treated either as his own, OR as a pointer to his parent's material or integrity?
For example I would just like to make the takeDamage function, in general as:
integrity -= collider.material.takedamage(&damagesource); without knowing that the integrity and the material is the collider's own, or it is a pointer to his parent's property
I would appreciate any assistance on this.
Thank you in advance!
Moderators - Reputation: 10521
Posted 12 April 2012 - 02:34 PM
Then your code simply reads:
*Integrity = some_value;
[Work - ArenaNet] [Epoch Language] [Scribblings] [Journal - peek into my shattered mind]
Members - Reputation: 135
Posted 12 April 2012 - 02:45 PM
Sure, just make the publicly visible Integrity always a pointer, and set it to point to a private "ActualIntegrity" variable or some such when you want to use your "own", and a pointer to the parent's "ActualIntegrity" when you want to use a parent (or some other object) instead.
Then your code simply reads:*Integrity = some_value;
Ah, so I can just treat everything as pointers and it might point to his own value, or his parent's, awesome, thanks very much!