Just another OOP question
What would be the best way to ''select'' objects. Let''s say I have 10 different classes derived from class NPCmonster. How would I tell program that player is fighting specific monster (i.e. orc thief created from class orc derived form NPCmonster class). How could I do this ? I need a way to select objects created from many different classes and be able to dynamically create objects, modify their variables(such as health or skills) load them from file and destroy them (creating Corpse object at same time). If there is some book that would help me with my problem please tell me ?
May 12, 2001 10:32 PM
I wouldn''t make every single type of creature a seperate class unless you only have a few. That''s the way teachers teach it in my school but personally I think they''re wrong and the books I''ve read agree with me. An orc isn''t fundamentally different from a goblin. They merely have different max hp, different graphics, etc... they might be different in ways that seem more fundamental, AI for example. In that case just have them delegate their AI to an object. That''s called the Strategy pattern so you might want to do a search on that, I think it''s a very good way of doing things. When in doubt don''t use inheritance, using interfaces (inheritance of responsibility but not of implementation) is just as good in most cases.
When I was trying to make my RPG I encountered a similar problem, I wanted to make maps out of tiles. Each tile type had distinct properties. I didn''t want to make each tile type a seperate class because I could have have thousands of classes and that''s just silly. I wanted to be able to define it all in files. So I made a generic tile class (I also made a couple subclasses, for animated tiles and other stuff) and then when I loaded up the game it made one tile of each type and put them in a hash. Then as I loaded the map it would read each tile and request a copy from the hash, using the clone() method (this was in java btw).
As for books I like Design Patterns, a very nice book that has solutions. It isn''t preachy. It has one big chapter on useful ways to use OOP, one chapter is an example, the next three chapters are patterns, which are problems and solutions. Each one is given like five pages. The book is big and well worth it. Keep in mind that while OOP can help you it can give you so much help that you never ever finish even as your program gets better and better in theory.
When I was trying to make my RPG I encountered a similar problem, I wanted to make maps out of tiles. Each tile type had distinct properties. I didn''t want to make each tile type a seperate class because I could have have thousands of classes and that''s just silly. I wanted to be able to define it all in files. So I made a generic tile class (I also made a couple subclasses, for animated tiles and other stuff) and then when I loaded up the game it made one tile of each type and put them in a hash. Then as I loaded the map it would read each tile and request a copy from the hash, using the clone() method (this was in java btw).
As for books I like Design Patterns, a very nice book that has solutions. It isn''t preachy. It has one big chapter on useful ways to use OOP, one chapter is an example, the next three chapters are patterns, which are problems and solutions. Each one is given like five pages. The book is big and well worth it. Keep in mind that while OOP can help you it can give you so much help that you never ever finish even as your program gets better and better in theory.
My teachers say that to know if you shoul implement all option as separeted classes you should follow this rule of thumb:do the class diagram... if it fits in a single paper leaf.. than you may implement as separated classes.
If brtute force doesn''''t solve your problem....you''''re not using enough!
![](smile.gif)
If brtute force doesn''''t solve your problem....you''''re not using enough!
If brute force does not solve your problem....you're not using enough!
The Book is "Design Patterns: Elements of Reusable Object-Oriented Software" by Erich Gamma & etc. al.
It''s boring as snot, but very useful.
It''s not about games at all, but about OOD (Object Oriented Design).
Magmai Kai Holmlor
- The disgruntled & disillusioned
It''s boring as snot, but very useful.
It''s not about games at all, but about OOD (Object Oriented Design).
Magmai Kai Holmlor
- The disgruntled & disillusioned
- The trade-off between price and quality does not exist in Japan. Rather, the idea that high quality brings on cost reduction is widely accepted.-- Tajima & Matsubara
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement
Recommended Tutorials
Advertisement