Skip to main content
gamedev.net
🔒 Locked 🎮 Unity

Pointer and Value interchangeability

Started by chondee Apr 12, 2012 at 7:41 PM 1 replies 950+ views
Original Post
chondee
chondee
Hi,

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!
ApochPiQ
ApochPiQ
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;
chondee
chondee

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!

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.