Help With Extern

Post your questions, suggestions and experiences regarding game design, integration of external libraries here. For irrEdit, irrXML and irrKlang, see the
ambiera forums
Post Reply
GameDude
Posts: 498
Joined: Thu May 24, 2007 12:24 am

Help With Extern

Post by GameDude »

Hey Guys, I am having some problems with my code. Any help would be appericated.

Player.h

Code: Select all

class Player
{
public:
	Player();
	~Player();
	 int Health;
	int Max_Health;
	int Init_Armor;
	int Armor;
	int Max_Armor;
	int Breathe;
	int Max_Breathe;
	int Weapon_Stock;
	int Max_Weapon_Stock;
	int Wanted_Level;
	int Max_Wanted_Level;
	int X;
	int Y;
	int Z;
	int Allies;
	int Max_Allies;
	int Damage;
	int Max_Damage;
	int Money;
	int Max_Money;
};
Player.cpp

Code: Select all

#include "player.h"

Player::Player()
{
	Player* gPlayer = new Player();
	gPlayer->Health = 100;
}

Player::~Player()
{
}
game.cpp

Code: Select all

#include "game.h"
#include "event.h"
#include "player.h"

int main(int argc, char* argv[])
{
	MainEvent receiver;

	extern Player* gPlayer;

	Device = createDevice(video::EDT_DIRECT3D9, core::dimension2d<s32>(800,600),
					32, false, true, false, &receiver);

	SEngine = createIrrKlangDevice();

	if(!SEngine)
	{
		return 0;
	}

	Driver = Device->getVideoDriver();
	Smgr = Device->getSceneManager();
	Env = Device->getGUIEnvironment();

	int lastFPS = -1;

	while(Device->run())
	{
		if(receiver.IsKeyDown(irr::KEY_ESCAPE))
		{
			Device->closeDevice();
		}

		Driver->beginScene(true, true, video::SColor(0,0,0,0));
		Smgr->drawAll();
		Env->drawAll();

		Driver->endScene();

		int fps = Driver->getFPS();

		if(lastFPS != fps)
		{
			core::stringw tmp(L"Car Junker - DarkScar Games [");
			tmp += Driver->getName();
			tmp += L"] FPS:";
			tmp += fps;
			tmp += L"Health:";
			tmp += gPlayer->Health;

			Device->setWindowCaption(tmp.c_str());
			lastFPS = fps;
		}
	}

	Device->drop();
	SEngine->drop();

	return 0;
}
sudi
Posts: 1686
Joined: Fri Aug 26, 2005 8:38 pm

Post by sudi »

Code: Select all

#include "player.h"

Player::Player()
{
   Player* gPlayer = new Player();
   gPlayer->Health = 100;
}

Player::~Player()
{
}
Thats an endless loop....
do this

Code: Select all

#include "player.h"

Player::Player()
{
   Health = 100;
}

Player::~Player()
{
}
We're programmers. Programmers are, in their hearts, architects, and the first thing they want to do when they get to a site is to bulldoze the place flat and build something grand. We're not excited by renovation:tinkering,improving,planting flower beds.
GameDude
Posts: 498
Joined: Thu May 24, 2007 12:24 am

Post by GameDude »

OK, but what about extern, so that I can use it in the game.cpp file.
sudi
Posts: 1686
Joined: Fri Aug 26, 2005 8:38 pm

Post by sudi »

ähhh u don't need it....at least not that i know off.
We're programmers. Programmers are, in their hearts, architects, and the first thing they want to do when they get to a site is to bulldoze the place flat and build something grand. We're not excited by renovation:tinkering,improving,planting flower beds.
GameDude
Posts: 498
Joined: Thu May 24, 2007 12:24 am

Post by GameDude »

Well how would I use the variable Health in the game.cpp without having to declare it again?
sudi
Posts: 1686
Joined: Fri Aug 26, 2005 8:38 pm

Post by sudi »

Player* gPlayer = new Player();

that should do the job
We're programmers. Programmers are, in their hearts, architects, and the first thing they want to do when they get to a site is to bulldoze the place flat and build something grand. We're not excited by renovation:tinkering,improving,planting flower beds.
GameDude
Posts: 498
Joined: Thu May 24, 2007 12:24 am

Post by GameDude »

Thanks, it worked
sudi
Posts: 1686
Joined: Fri Aug 26, 2005 8:38 pm

Post by sudi »

You should read up on some c++ stuff like pointers, instances, objects
We're programmers. Programmers are, in their hearts, architects, and the first thing they want to do when they get to a site is to bulldoze the place flat and build something grand. We're not excited by renovation:tinkering,improving,planting flower beds.
GameDude
Posts: 498
Joined: Thu May 24, 2007 12:24 am

Post by GameDude »

I have read about pointers and objects and stuff, but its kinda confusing. I wish Irrlicht had a wrapper for the Euphoria programming language (www.rapideuphoria.com), that would make it a lot easier.
MasterGod
Posts: 2061
Joined: Fri May 25, 2007 8:06 pm
Location: Israel
Contact:

Post by MasterGod »

Sudi wrote:

Code: Select all

#include "player.h"

Player::Player()
{
   Player* gPlayer = new Player();
   gPlayer->Health = 100;
}

Player::~Player()
{
}
Thats an endless loop....
do this

Code: Select all

#include "player.h"

Player::Player()
{
   Health = 100;
}

Player::~Player()
{
}
Better this:

Code: Select all

Player::Player() : Health(100)
{
}
:wink:
This is a better way of initializing members.
Image
Dev State: Abandoned (For now..)
Requirements Analysis Doc: ~87%
UML: ~0.5%
Post Reply