Jump to content

  • Log In with Google      Sign In   
  • Create Account


Creating game objects with a class?


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
4 replies to this topic

#1 Solid_Spy   Members   -  Reputation: 371

Like
0Likes
Like

Posted 29 November 2013 - 05:05 AM

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?



Sponsor:

#2 kauna   Crossbones+   -  Reputation: 2142

Like
1Likes
Like

Posted 29 November 2013 - 07:02 AM

 
//factory.h
 
#pragma once
 
class GameObject;
 
class SomeFactory()
{
public:
     GameObject *CreateGameObject();
}
 
//factory.cpp
 
#include "GameObject.h"
 
GameObject *SomeFactory::CreateGameObject(){return new GameObject();}
 

 

Easy as that to fix circular references.

 

Cheers!


Edited by kauna, 29 November 2013 - 07:03 AM.


#3 TheComet   Members   -  Reputation: 1363

Like
2Likes
Like

Posted 29 November 2013 - 07:26 AM

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>

YOUR_OPINION >/dev/null


#4 Solid_Spy   Members   -  Reputation: 371

Like
0Likes
Like

Posted 29 November 2013 - 06:19 PM

 

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, 29 November 2013 - 06:19 PM.


#5 Dante12129   Members   -  Reputation: 969

Like
0Likes
Like

Posted 29 November 2013 - 07:14 PM

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?






Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS