• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
Jaap85

Class inheritance?

12 posts in this topic

Hello everybody,

 

It's been a while since i have visited this forum, good to see it is still so active.

 

I have recently picked up game programming again and i am currently working in C# (XNA). I try to create a simple flying shooter game (Raptor clone).

 

Right now i am implementing the enemies and i would like to know what the best programming standard is for implementing the different types of enemies. I have already created a class "Enemy" which holds all the functions and variables needed for the enemies (such as the movement function, etc.)

 

I will create different subtypes of enemies, which differ in (for example): image, speed, firing rate, hitpoints, and so on.

 

What is the best way to implement is? I was first thinking of creating a separate class for each enemy type, which inherits from the enemy class, but that might be cumbersome.

 

Another option could be to just use the enemy class and assign every enemy a type when it is constructed, then work with lots of switch statements in the class based on the enemy type.

 

Is this the best way? Or is there a better way to do this? I feel a better way should be possible, but i cant really figure out how yet.

 

Thank you in advance for your replies. 

0

Share this post


Link to post
Share on other sites

Another option is to load the enemy data from a data file. Then the constructor only needs to know the name of the file (or index into an archive, etc.).

1

Share this post


Link to post
Share on other sites

Thank you, i will look into that. I thought that such a method should exist but have never worked with if before. What is a good file format to use for such a file? XML? Any examples would be very much appreciated.

0

Share this post


Link to post
Share on other sites


I will create different subtypes of enemies, which differ in (for example): image, speed, firing rate, hitpoints, and so on.

 

All the examples you cite above can be accomplished with just a single enemy class. There's no custom functionality at all, so no need to create new classes that inherit from enemy (and even if there were, composition would probably be a better solution than inheritance).

 

Just create new instances of the Enemy class with the required information (image, speed, etc...) supplied in the constructor. You could load that data from a file (as SiCrane suggests), but that's functionality that could be added later - it doesn't enable anything new, it just makes it easier to add enemy types or modify enemies without making code changes.

0

Share this post


Link to post
Share on other sites

Another option could be to just use the enemy class and assign every enemy a type when it is constructed, then work with lots of switch statements in the class based on the enemy type.

 

 

People seem to be overlooking this statement. Why do you need the switches? Is it because each enemy will behave differently? If so, then this will be where either inheritance or composition will take place. Inheritance is easy to think about in this example and since you are using C# can be just as dynamic if you make a plug-in system for each enemy and dynamically create enemies by class name. If you go the composition route you could store an AI interface class object inside Enemy and then have different AI objects that implement that interface. Each different AI class would be the functionality that you want. To use Left 4 Dead as an example you would have iAI interface with function like Update(). Then you would make classes that implement this interface like: ZombieAI, HunterAI, BoomerAI, SmokerAI, Tank, etc.

 

If you find yourself doing a bunch of switches then there is most likely a better way. Switch statements aren't very dynamic. You don't want to have to remember to update switch statements when you think of a new enemy type.

interface iAI
{
    void Update();
}
 
class ZombieAI : iAI
{
    void Update()
    {
        // zombies behave differently than other enemies so they get their own class
    }
}

class TankAI : iAI
{
    void Update()
    {
        // tanks behave differently than other enemies so they get their own class
    }
}
 
class Enemy
{
    private iAI ai;
    private Image img;
 
    // inject the ai into the enemy class so that it can be more than 1 type
    public Enemey(iAI a, Image i) { ai = a; img = i; }
 
    public Update() { ai.Update(); }
};
 
// read from a text file or db to get the type of enemy you want to make. I would then dynamically create an instance based on string name using reflection
List<Enemy> enemies = new List<Enemy>();
 
// this enemy now acts like a zombie
enemies.Add(new Enemy(new ZombieAI(), new Image(filename));
 
// this enemy now acts like a zombie
enemies.Add(new Enemy(new TankAI(), new Image(filename));

Here is a decent example that compares the 2 approaches: http://www.javaworld.com/article/2076814/core-java/inheritance-versus-composition--which-one-should-you-choose-.html

Edited by rpiller
2

Share this post


Link to post
Share on other sites


What is a good file format to use for such a file? XML?

XML is stil bloated, still costly to parse, and still has a couple of features that doesn't help in this use case. The major opinion is to create a customized binary file format fitted to your needs *if* you speak of the load file format; it is something other if you speak of file formats for the development phase where interchangeability plays a role.

0

Share this post


Link to post
Share on other sites
Yeah, it seems you wouldn't need another class. You could just put the type in the constructor so that each enemy has a type. I don't know how it is done in C# but I lua I would implement it like so:

badGuy = Enemy(type)
0

Share this post


Link to post
Share on other sites

All right, thank you very much for responding everybody :-)

 

If i try to summarize your findings, i come to the following conclusions:

  • If the behaviour of all the different enemies is  the same, the best way to code everything is one of the following:
    1. Pass the type to the constructor, then use a switch statement in the constructor to set the different values
    2. Pass the different values directly to the constructor

When considering these methods i prefer the first one, since it will allow me to create new enemies easier. An even more advanced approach would be to dynamically generate the enemies by loading them from an external data file. 

 

  • If the behaviour of the enemies is different, composition is the best option. This is something new to me (i am not yet very experienced) but something i will definitely look into.

 

I hope the above is correct. Thank you very much for all your replies.

0

Share this post


Link to post
Share on other sites


If the behaviour of all the different enemies is  the same, the best way to code everything is one of the following:
 1) Pass the type to the constructor, then use a switch statement in the constructor to set the different values
 2) Pass the different values directly to the constructor
When considering these methods i prefer the first one, since it will allow me to create new enemies easier. An even more advanced approach would be to dynamically generate the enemies by loading them from an external data file. 

 

Except with the first one, you need to go change the Enemy class every time you create a new enemy type.

0

Share this post


Link to post
Share on other sites

I don't understand why he would have to change the enemy class every time he creates a new enemy. If the class is designed correctly, everything could be adjusted outside of the class. 

 

This is pseudocode for how it could be done (python)

class Enemy():
def _init_(self) pass
self.hitpoints = hit_points;
self.image_speed = image_speed;

badGuy = Enemy()
badGuy2 = Enemy()

print (badGuy.hit_points)
print (badGuy2.hit_points)





If you wanted to specify a type, you could make the type a boolean value and make that an argument of the object. 

badGuy = Enemy(sniper)


In the class you would have:

sniper = false

And you would have a switch or conditional statement in the class like:

if (sniper == true)
{}
else if (bomber == true)
{}
else if (tank == true)

etc

class Enemy()

The boolean type would be in the constructor of the class, and it is designated upfront. All you do is check the type in the class, and perform whatever actions you want to do if a certain type is chosen. 

 

You'd create all of your enemy functions inside the class, so that once a type is selected, then those functions are triggered. 

Edited by Tutorial Doctor
0

Share this post


Link to post
Share on other sites


I don't understand why he would have to change the enemy class every time he creates a new enemy.

 

He means, every time you create a new enemy type, not a new enemy instance. It's a little messy to have a giant if/switch statement in a class like that. There are cleaner ways.

0

Share this post


Link to post
Share on other sites

Yeah, there could be a switch statement for elements in an array (which would be the types, and which would also be boolean values) or however else. I gotcha though. Yeah, adding a new type would have to be done in the class. 

0

Share this post


Link to post
Share on other sites

 


What is a good file format to use for such a file? XML?

XML is stil bloated, still costly to parse, and still has a couple of features that doesn't help in this use case. The major opinion is to create a customized binary file format fitted to your needs *if* you speak of the load file format; it is something other if you speak of file formats for the development phase where interchangeability plays a role.

 

There are two problems with binary source formats from a development perspective.

  1. They are difficult for developers to edit with confidence
  2. Source control can't give you a useful diff history.

 

OTOH, you're right about xml being bloated at runtime. One option would be to write the source data in xml/json/whatever and then write a simple tool to parse the xml into your custom binary format as a build step. This should be relatively trivial to do and then you get the best of both worlds; human readable source that is easily put into version control and fast, compact files to distribute.

0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0