Jump to content
  • Advertisement
Sign in to follow this  
DarrenHorton

The Inheritance of Properties in C#

This topic is 2522 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

I am making a Space Game in C#.

I wish to allocate a maximum of 4 resources properties (Metals/Building Materials/Gases/Luxuries), to a planet/moon/asteroid as the class constructor is called.

The planet/moon/asteroid inherits the 50 resources properties from the class it derives from.

When I create the planet etc I only initialize a maximum of 4 resources properties to between 20-90% resource density. Through a method call.

I wish for these resources properties to be what can be mined/harvested and traded by the planet etc.

However when I checked the planets list every planet had 50 inherited resource properties attached, even though I only wish to use the 4 I initialized.

After a lot of web searching I realized that all of these resource properties will be inherited in the derived class.

As I have not initialized the 50 resource properties they are all auto initialized at 0.

As every planet I create will have all of these resource properties attached (even though I only use 4), I fear that as I am creating over 500 planets this

will lead to more memory being allocated to accomodate the unused resource properties.

Is there any way of removing unwanted property inheritance for each time I create a planet?

Could I remove all resource properties that equal 0 before adding the planet to the List<Planet>..?

Or is there a better way of allocating a potential planetary resource than directly through giving the planet a (iron) property?

Share this post


Link to post
Share on other sites
Advertisement

Is there any way of removing unwanted property inheritance for each time I create a planet?


By not using inheritance when it's not appropriate.

Share this post


Link to post
Share on other sites
You could also use nullable types and just default initialize them to null, then use the null/non-null flag when defining the resources presented from a particular planet?

But that's more of a band-aid, I'd take a second look at your design. 50 is a LOT of properties. Why can't you define a "resource" object: a POD that's just a string (to determine what kind of resource it is) and an int/float to represent the value/abundance?

Share this post


Link to post
Share on other sites
Could each planet have a List<Resource> or something, instead of a separate member for all possibilities?

Share this post


Link to post
Share on other sites

Could each planet have a List<Resource> or something, instead of a separate member for all possibilities?


That seems a good idea.

I just made a tester List<> Property and it compiled.

I never made a List property before so it's a new idea to me.

I can then allocate the resources to a planet as it's constructed, and keep all of the resources in a seperate class.

I will call a resources class method to populate the List<Resources> for each planet instance and return the List parameter which was passed.

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.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!