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

Classes that need to know each other..

12 posts in this topic

Hey, so since I am a noob I found myself stuck on a small problem. And the problem is that I have a Entity class that contains a Healthbar instance and I need the healthbar to contain a pointer to its owner (the entity). And this is where I got stuck.

 

CEntity class:

#ifndef _CENTITY_H_
#define _CENTITY_H_

#include "SDL.h"

class CEntity
{

	public:
		CEntity();
		~CEntity();

	public:
		bool OnInit();
		void OnRender(SDL_Surface* destination);
		void OnLoop();

		int GetX();
		int GetY();

		int GetW();
		int GetH();

		int GetMaxHealth();
		int GetCurrentHealth();

	private:
		SDL_Rect Image;
		
		CHealthBar HealthBar;
		int MaxHealth;
		int CurrentHealth;

		int LastHurtTime;

};

#endif

 

CHealthBar class:

#ifndef _CHEALTHBAR_H_
#define _CHEALTHBAR_H_

#include "SDL.h"

class CHealthBar 
{
	
	public:
		CHealthBar();
		~CHealthBar();

	public:
		bool OnInit();
		void OnRender(SDL_Surface* _destination);
		void OnLoop();

		void SetOwner(CEntity* _owner);

	private:
		CEntity* Owner;

		SDL_Rect BarBackground;
		SDL_Rect Bar;

		int MaxBarLenght;

		SDL_Color HighHealth;
		SDL_Color AverageHealth;
		SDL_Color LowHealth;

};

#endif

 

 

The compiler complains about this to no end, what should I do about this? Is this not a good way to handle the way a healthbar is connected to an entity? I've tried to include them here and there to no success, even tried to forward declare and still not getting it to work.

 

Thanks

0

Share this post


Link to post
Share on other sites

Don't you need to #include the files?

 

At the top of "CHealthBar.h" do #include "CEntity.h"  to tell the compiler you are using the class defined in the file, else it wont know what CEntity is and likewise in the other file.

 

You may also need to pre-define the classes, so at the top of CHealthBar ... I vaguly remeber having toto this in my last game.

#ifndef _CHEALTHBAR_H_
#define _CHEALTHBAR_H_

#include "SDL.h"
#inlcude "CEntity.h";

class CEntity;


class CHealthBar
{ ...

And likewise in the other file, hope this help smile.png

 

-edit: all the bad types and code tags

Edited by Andy474
0

Share this post


Link to post
Share on other sites

Don't you need to #include the files?

 

At the top of "CHealthBar.h" do #include "CEntity.h"  to tell the compiler you are using the class defined in the file, else it wont know what CEntity is and likewise in the other file.

 

You may also need to pre-define the classes, so at the top of CHealthBar ... I vaguly remeber having toto this in my last game.

#ifndef _CHEALTHBAR_H_
#define _CHEALTHBAR_H_

#include "SDL.h"
#inlcude "CEntity.h";

class CEntity;


class CHealthBar
{ ...

And likewise in the other file, hope this help smile.png

 

-edit: all the bad types and code tags

 

Well I did not have that in this example but I have tried that and tried it again now and it still gives me errors like "uses undefined class CHealthBar" etc.

0

Share this post


Link to post
Share on other sites

ok, have you predefined the class in each file? so as aove in CEntity you have put class CHealthBar, and can you post the Compiler Errros, might make it easier for other to diagnose.

 

also, iirc they have to be in the same directory level.

 

E.g.

If your directory looks like

-Classes

   - CEntity.h

   - GUI.

      - CHealthBar

 

then the include in CENtity must be #include "GUI\CHealthBar"

Edited by Andy474
0

Share this post


Link to post
Share on other sites

ok, have you predefined the class in each file? so as aove in CEntity you have put class CHealthBar, and can you post the Compiler Errros, might make it easier for other to diagnose.

#include "CHealthBar.h"

class CHealthBar;

 

this is in my CEntity.h file.

 

and this is in my CHealthBar file:

#include "CEntity.h"

class CEntity;

 

and when i compile i get this error:

error C2079: 'CEntity::HealthBar' uses undefined class 'CHealthBar'

0

Share this post


Link to post
Share on other sites

If you predefine 

class CHealthBar;

 

you do not need to #include it. You #include it in .cpp file.

0

Share this post


Link to post
Share on other sites

If you predefine 

class CHealthBar;

 

you do not need to #include it. You #include it in .cpp file.

Yes and when I do that I get these errors:

 

error C2079: 'CEntity::HealthBar' uses undefined class 'CHealthBar'

error C2228: left of '.SetOwner' must have class/struct/union

error C2228: left of '.OnRender' must have class/struct/union

error C2228: left of '.OnLoop' must have class/struct/union

0

Share this post


Link to post
Share on other sites

CHealthBar HealthBar;

 

That is your problem.

 

 

When using a forward declaration, you can only use a POINTER to the object, you cant actually declare an instance, as the compiler has no idea of the actual composition and size of a forward declared class.

 

Change CHealthBar to a reference or pointer, and your code should work.

 

 

EDIT: This StackOverflow answer explains it in a bit more detail.  So either change it so CEntity is aware of CHealthBar, or if you are going to continue using a forward declaration, make CHealthBar in CEntity a pointer or reference.

Edited by Serapth
1

Share this post


Link to post
Share on other sites

hmm, I have run these two files (using VS2012)  and have no problems.

 

mb cus i've got no CPP files but tongue.png also i havea  feeling you need todo CHealthBar* HealthBar;

 

Edit - Serapth got in first :)

Edited by Andy474
0

Share this post


Link to post
Share on other sites

CHealthBar HealthBar;

 

That is your problem.

 

 

When using a forward declaration, you can only use a POINTER to the object, you cant actually declare an instance, as the compiler has no idea of the actual composition and size of a forward declared class.

 

Change CHealthBar to a reference or pointer, and your code should work.

 

 

EDIT: This StackOverflow answer explains it in a bit more detail.  So either change it so CEntity is aware of CHealthBar, or if you are going to continue using a forward declaration, make CHealthBar in CEntity a pointer or reference.

Ok, thank you. Will try to get that up and running then.

Yep got it working, thank you :)

Edited by Nausea
0

Share this post


Link to post
Share on other sites

CHealthBar HealthBar;

 

That is your problem.

 

 

When using a forward declaration, you can only use a POINTER to the object, you cant actually declare an instance, as the compiler has no idea of the actual composition and size of a forward declared class.

 

Change CHealthBar to a reference or pointer, and your code should work.

 

 

EDIT: This StackOverflow answer explains it in a bit more detail.  So either change it so CEntity is aware of CHealthBar, or if you are going to continue using a forward declaration, make CHealthBar in CEntity a pointer or reference.

Pointer or reference as long as you don't dereference them.  Its also worth noting that you can use a smart pointer so you don't have to use naked pointers.

0

Share this post


Link to post
Share on other sites
As a side note, should Entity really have a health bar? I would think that something like 'Actor' would derive from Entity and Actor would have a health bar.

I dunno your layout though. Just a though.
0

Share this post


Link to post
Share on other sites

Usually having these kinds of circular references in an indication of dubious design. I don't know, though. It happened a lot when I started programming and now I always avoid it. Try refactoring with a different architecture, maybe?

1

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