Jump to content
  • Advertisement
Sign in to follow this  
kyborg

Structure of program - polymorphism

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

Hello
I have some troubles with structure of my game. I want to have about 30 types of buildings, so I wrote virtual class building, and for each type of building one derived class. I have all my buildings in one array called building_list. Problem is that every type of building has different functions. So it will be about 90 functions probably. I dont think, that its a good idea to have 90 virtual functions in base class building, but I dont know how can I avoid it. And I want to have all buildings in one array. Is it possible to do it somehow?

Thanks for answers.

Share this post


Link to post
Share on other sites
Advertisement
Do you really need each building type to have its own type in the language as well, and not just have one building type that has data members describing the different buildings instead?

Share this post


Link to post
Share on other sites
I didnt think about just one class, because it would have really lots of data and functions, but it can work. But I think that in games like Age of Empires or Caesar it is done in some nicer way.

Share this post


Link to post
Share on other sites
Of course you can separate your data in different ways more than just one single big class, but modelling the game in the language is not really flexible. What is the alternative to lots of members; lots of functions? At least the data members can be easily set dynamicallyfrom external sources so you can tweak eveyrhing as you like and create new building, units, weapons, monsters, player classes, cities and whatever by just poking around in a text file. Your alternative here would be to program them in the source code and recompile your whole game.

A better and more specific answer to your particular situation can perhaps be given if you describe your situation and what all you 30 types and 90 functions are. Edited by Brother Bob

Share this post


Link to post
Share on other sites
OK, thanks for the answer. I thought that there is some common way or some trick how to deal with this problem. But it looks like it isnt, so I will do it somehow.

Share this post


Link to post
Share on other sites
You may be running into the M*N problem (a typical issue in OOP), with M being #buildings, and N #operations-on-buildings, and M*N being your code complexity.
A solution wouldn't be to go back to procedural programming (as someone noted: "What is the alternative to lots of members; lots of functions?").
Instead, consider a generic programming (GP) solution -- if there is a common algorithmic functionality to what you're trying to achieve, perhaps a few (instead of "lots") free-standing (i.e., not member) function templates parametrized on your building-type might be the way to go? In case of STL, this solution allowed to bring down code complexity from M*N (which would've been the case if the containers & algorithms were to be organized into a class hierarchy and distinguished when operated upon via virtual member functions -- the OOP way) to M+N (which is the case for generic container classes (no virtuals) operated on by generic (parametrized by container and/or iterator type) algorithms) -- so, arguably, that's a common way to deal with this problem :-)
See:
http://gsd.web.elte....cles/icai99.pdf
https://github.com/b...aph_library.pdf Edited by Matt-D

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!