• Create Account

# Pointer and Value interchangeability

Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

2 replies to this topic

### #1chondee  Members   -  Reputation: 135

Like
0Likes
Like

Posted 12 April 2012 - 01:41 PM

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.

### #2ApochPiQ  Moderators   -  Reputation: 10521

Like
2Likes
Like

Posted 12 April 2012 - 02:34 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.

*Integrity = some_value;

Maker of Machinery

### #3chondee  Members   -  Reputation: 135

Like
0Likes
Like

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.

*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!

Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

PARTNERS