Jump to content
  • Advertisement
Sign in to follow this  
chondee

Pointer and Value interchangeability

This topic is 2318 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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!

Share this post


Link to post
Share on other sites
Advertisement
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;

Share this post


Link to post
Share on other sites

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!

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!