• Advertisement
Sign in to follow this  

Creating game objects with a class?

This topic is 1513 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've just recently hit a big wall with my game programming.

 

I've been trying to make a class that can create any kind of game object, but it needs to use the header files for other objects in order to work.

 

Sure, doesn't sound like much of a problem but... what if I want the Player to use the object creator class to create more objects?

 

As you can see, i've run into a circular dependancy problem. Does anyone have a solution to this? Or is my method just conveluted?

Share this post


Link to post
Share on other sites
Advertisement

[source]

 

//factory.h

 

#pragma once

 

class GameObject;

 

class SomeFactory()

{

public:

     GameObject *CreateGameObject();

}

 

//factory.cpp

 

#include "GameObject.h"

 

GameObject *SomeFactory::CreateGameObject(){return new GameObject();}

 

[/source]

 

Easy as that to fix circular references.

 

Cheers!

Edited by kauna

Share this post


Link to post
Share on other sites

Just to elaborate a little more on kauna's solution:

#pragma once
//stuff

The above code is equivalent to header guards, as seen in the following code. It is used to make sure things are only declared exactly once. Examples of when they are needed are circular inclusion and polymorphism.

#ifndef __FACTORY_H__
#define __FACTORY_H__
// stuff
#endif // __FACTORY_H__

In the case of circular inclusion, it is often possible to omit including the header of the other class and simply use a forward declaration, as seen here:

class GameObject; // instead of "#include <GameObject.h>

Share this post


Link to post
Share on other sites

 

Just to elaborate a little more on kauna's solution:

#pragma once
//stuff

The above code is equivalent to header guards, as seen in the following code. It is used to make sure things are only declared exactly once. Examples of when they are needed are circular inclusion and polymorphism.

#ifndef __FACTORY_H__
#define __FACTORY_H__
// stuff
#endif // __FACTORY_H__

In the case of circular inclusion, it is often possible to omit including the header of the other class and simply use a forward declaration, as seen here:

class GameObject; // instead of "#include <GameObject.h>

But dont Forward declarations prevent you from accessing methods and variables from the objects? If i want to create a new player with the object creator, and a new bullet with the player, how do I access the methods?

Edited by Solid_Spy

Share this post


Link to post
Share on other sites

Look at the source file, it includes GameObject.h. The header only uses a pointer to GameObject, so it doesn't actually need to know about it. The source file is what is actually using it, hence why you include it there. Since pointers and references are known sizes, the compiler can use them without full definitions of types.

 

Also, why is your factory method in a class when it could just be a free function in this case?

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement