• 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
StoneMask

Can you put multiple types of classes in the same array?

20 posts in this topic

Okay, so I have this ENEMY class right? I guess for simplicity, the subclasses are RED, BLU, GRN.

 

My game wants it to be that there is a certain chance that some of these varities spawn more than others, and I store all the enemies in a vector array.

 

The problem here is that if I make a vector array of ENEMY pointers, I can't access the member functions that are exclusive to the RED, BLU, and GRN subclasses. I can only access the base class' member functions, and even if I've overloaded RED/BLU/GRN's functions to behave differently, they default to the base class' methods. If this is easily explainable, or if there's a solution to this problem, please let me know; otherwise, I'll post my code and we'll see what we can do.

0

Share this post


Link to post
Share on other sites

Are you declaring your overrides virtual? If you don't it will call the implementation based on the actual pointer type in the array. If they are virtual, everything will work as intended.

 

EDIT: They need to be virtual in the base class for this to work. The derived classes will automatically be virtual then. If you don't declare the base class function virtual it won't work either.

Edited by Paradigm Shifter
0

Share this post


Link to post
Share on other sites

Yeah, I declared it virtual. It's telling me the ENEMY class' move() function doesn't take only one argument, when the RED object that is in the array is supposed to.

 

let's say:

 

 

class ENEMY
{
   friend class RED;  

   public:
      virtual void move(param1, param2);
}

class RED : public ENEMY
{
   public:
      void move(param1);
}

int main()
{
   vector<ENEMY*> ENEMIES;
   ENEMIES.push_back(new RED);
   ENEMIES[0]->move(param1);
   return 0;
}
 

 

It's giving me this error:

 

error C2660: 'ENEMY::move' : function does not take 1 arguments


 

Edited by StoneMask
0

Share this post


Link to post
Share on other sites

It's not an override unless it takes the same number and type of parameters. You need to add a second parameter to the function in the derived class.

 

EDIT: And you don't need to declare your derived class a friend of the base class, there are no private methods or members and you can make them protected instead if you need to access them in a derived class.

Edited by Paradigm Shifter
0

Share this post


Link to post
Share on other sites

I also can't use a function exclusive to my subclass, right? If it's in this array, at least.

 

Is it acceptable to just have a junk parameter? I literally have no use for whatever I'd make as the second parameter.

0

Share this post


Link to post
Share on other sites

Yes, have a parameter that isn't used in the function. Don't give it a name if you get a warning about the parameter not being used.

 

Why does the base class implementation take more parameters anyway?

 

EDIT: You can use a function exclusive to the subclass IF you call it from your virtual function in the derived class. You can also use dynamic_cast to get the real type but that isn't a good design.

Edited by Paradigm Shifter
0

Share this post


Link to post
Share on other sites

The regular ENEMY objects require the second parameter to seed its RNG when it chooses a random direction to move in that is distinct from all the other ENEMY objects.

 

The RED subclass moves towards specified coordinates, and thus doesn't need a seed for any RNG.

Edited by StoneMask
0

Share this post


Link to post
Share on other sites

I also can't use a function exclusive to my subclass, right? If it's in this array, at least.

 

Is it acceptable to just have a junk parameter? I literally have no use for whatever I'd make as the second parameter.

How long have you been programming with C++?  It doesnt seem like you have a firm grasp of polymorphism.  If that's the case, you might want to really brush up and get a good idea of how c++ polymorphism works before you code with it.

 

But to answer your question, if you're trying to find ways to hack your code to get what you want, it likely means that your design is flawed. 

 

While casting is not usually a great idea, it sounds like maybe it could help you here.  Another alternative might be a message-passing system to get the behavior out of your child classes through virtual method calls on the base.

0

Share this post


Link to post
Share on other sites

The regular ENEMY objects require the second parameter to seed its RNG when it chooses a random direction to move in that is distinct from all the other ENEMY objects.

 

The RED subclass moves towards specified coordinates, and thus don't need a seed for any RNG.

Why are you trying to seed your RNG every time you're going to use it?

0

Share this post


Link to post
Share on other sites

The regular ENEMY objects require the second parameter to seed its RNG when it chooses a random direction to move in that is distinct from all the other ENEMY objects.

 

The RED subclass moves towards specified coordinates, and thus don't need a seed for any RNG.

 

You're passing a random seed every time you call move on an ENEMY? Why don't you set the seed once when the enemy is created?

 

See my edit to my post above re calling derived class methods as well.

0

Share this post


Link to post
Share on other sites

On and off a couple years; my C++ book that I'd been studying out of was actually lost around the time I got to the polymorphism chapter, so yeah, I'm a bit lacking in this area.

 

You make a good point about setting the seed at the creation though.

0

Share this post


Link to post
Share on other sites

On and off a couple years; my C++ book that I'd been studying out of was actually lost around the time I got to the polymorphism chapter, so yeah, I'm a bit lacking in this area.

 

You make a good point about setting the seed at the creation though.

Set the seed at game startup and then dont change it, unless you have a specific need for it... which you probably dont.

 

The simplest solution to what you're trying to do is to have a virtual function that takes in an enum value, the message.  Then in the derived class you can switch on that and call the appropriate function to handle it.  You can send in a second parameter with a pointer to some data if you want.  The simplest way there is to send a void* and case to the right type once you know what the message is.  Not great to do in c++, but simple and will solve your problem.

0

Share this post


Link to post
Share on other sites

I'm guessing each enemy wants a separate seed so that each enemy does something deterministic independent of the other enemies (so that destroying an enemy won't put the other enemies out of sync due to different numbers of calls to rand() or whatever). That's just a guess though!

 

I wouldn't recommend having an uber virtual function and a message which you switch with, that kind of defeats polymorphism. If you have an enormous number of messages you need to handle, it gets more attractive, though. And void* isn't good advice either for C++.

0

Share this post


Link to post
Share on other sites

Set the seed at game startup and then dont change it, unless you have a specific need for it... which you probably dont.

 

When I don't seed the different enemies' RNG values each time they move, they all move in the same directions at each loop iteration. I seed with the index variable so each seed number is different and each of them moves at a different time in a different direction.

 

I don't know what you mean by number of messages, or even what you mean by messages.

 

If I set the seed for each of the enemies, I think that would be good for eliminating the need for the second parameter though. This seems like a pretty dead simple solution.

0

Share this post


Link to post
Share on other sites

That sounds like you're not using your RNG properly. If you seed it just once at the beginning of the program all the enemies should do different things.

 

Setting the seed explicitly for each enemy has a use, that is deterministic randomised behaviour for each enemy, but I don't think that is what you are after.

0

Share this post


Link to post
Share on other sites

It's not necessarily a good design idea, but you could add an enum to your base class that determines what derived class to call:

 

enum EnemySubType
{
  BASE_ENEMY,
  RED_ENEMY,
  BLU_ENEMY,
  GRN_ENEMY
};
 
class ENEMY
{
  protected:
    EnemySubType mySubType;
};

 

When you need to call your derived class functions that don't exist in the base class:

if(myEnemy.mySubType == RED_ENEMY)
{
  ((RED) myEnemy).myDerivedClassFunction();
}
else if......

 

If Else statments or Switch Case would work.

 

I agree that you should potentially redesign your classes/look up polymorphism more, but this could work if you just want to get something running.

0

Share this post


Link to post
Share on other sites
You could try a dynamic_cast<RED>, dynamic_cast<BLU>, etc and do work if the resultant pointer is valid.
0

Share this post


Link to post
Share on other sites

Pretty much a problem of using subclassed objects in a list or whatever type of collection. Realistically the code and the compiler don't know if an object is of a particular subclass unless you cast it to it. Paradigm was basically suggesting you include a virtual copy of the method you're trying to call in the base class so it has a definition to work with, though that only works if every subclass has it. I.e. if each color of enemy overrides move and you're calling move or something.

 

Ideally if you're going to use a collection like that you only want to use it for iterating purposes, like calling update on groups of objects even if the objects do different things when update is called on them. I'm not really sure what you're trying to accomplish with the AI and seed thing though.

0

Share this post


Link to post
Share on other sites

Set the seed at game startup and then dont change it, unless you have a specific need for it... which you probably dont.

 

When I don't seed the different enemies' RNG values each time they move, they all move in the same directions at each loop iteration. I seed with the index variable so each seed number is different and each of them moves at a different time in a different direction.

 

I don't know what you mean by number of messages, or even what you mean by messages.

 

If I set the seed for each of the enemies, I think that would be good for eliminating the need for the second parameter though. This seems like a pretty dead simple solution.

It sounds like you're doing something wrong.  If you seed the RNG once at startup, then every number you get after that from the rand() function will be pseudorandom.  In other words, any character that uses it to determine their movement should move in a different direction.  

 

Maybe you can include some code so we can see what you're doing.

 

By "messages" I mean something like this:

enum MESSAGE
{
	MSG_DO_SOMETHING,
	MSG_DO_SOMETHING_ELSE,
};

class ENEMY
{
public:
	virtual void move(void){}
	virtual void handle_message(MESSAGE msg) = 0;
}

class RED : public ENEMY
{
public:
	virtual void move(void);
	virtual void handle_message(MESSAGE msg)
	{
		if(msg == MSG_DO_SOMETHING)
		{
			do_something();
		}
	}
private:
	void do_something(void);
}

class BLUE : public ENEMY
{
public:
	virtual void move(void);
	virtual void handle_message(MESSAGE msg)
	{
		if(msg == MSG_DO_SOMETHING_ELSE)
		{
			do_something_else();
		}
	}
private:
	void do_something_else(void);
}

 

This method has its problems, but it's also an easy thing to implement if you want to call different functionality in your derived class objects through a pointer to their base class.

0

Share this post


Link to post
Share on other sites

When I don't seed the different enemies' RNG values each time they move, they all move in the same directions at each loop iteration. I seed with the index variable so each seed number is different and each of them moves at a different time in a different direction.

Sounds like you're creating each Enemy's PRNG with the same seed.  Like some have suggested you could use a since global variable for your PRNG (which is the classic C style), or you could have a PRNG member of your game class and use that to generate a sequence of seeds for each enemy at creation time.

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