///////////////////////////////////////
//CEntityManager.h
///////////////////////////////////////
#ifndef __CENTITYMANAGER_H_INCLUDED__
#define __CENTITYMANAGER_H_INCLUDED__
#include "CEntity.h"
class CEntityManager {
private:
CEntity* entList[100];
s32 entCount;
public:
s32 getEntityCount();
void newEntity(s32 type);
};
#endif
///////////////////////////////////////
//CEntityManager.cpp
///////////////////////////////////////
#include "CEntityManager.h"
s32 CEntityManager::getEntityCount() {
return this->entCount;
}
void CEntityManager::newEntity(s32 type) {
switch (type) {
case ENT_NULL:
break;
case ENT_PLAYER:
//!!!!this->entList[this->entCount] = new CPlayer;
break;
}
//!!!!this->entCount++;
}
Notice the lines with the //!!!!. Whenever i uncomment either or both of those lines out and i execute the program... the program will just quit once it hits either or both of those lines?!? Whats up with that?!
🔒 Locked
Wierd problem.....
Advertisement
Original Post
I just started working on an entity manager for my first person shooter just to make things a bit easier to work with.... but i ran into a strange problem very early.
you need to initialise the entcout to 0 somewhere, typically in your constructor.
hmm, I tried that and it did the same thing
that still will quit once it hits the line ''this->entCount++;''!!
I''ve never run into this problem before! does enyone know what the problem might be?
///////////////////////////////////////
//CEntityManager.h
///////////////////////////////////////
#ifndef __CENTITYMANAGER_H_INCLUDED__
#define __CENTITYMANAGER_H_INCLUDED__
#include "CPlayer.h"
///////////////////////////////////////////////////
// The entity manager is just a class that handles
// all of the entities to make things simpler
///////////////////////////////////////////////////
class CEntityManager {
public:
// Constructors
CEntityManager(): entCount(0) {}
~CEntityManager() {}
void newEntity(s32 type);
// Accessers
s32 getEntityCount();
private:
CEntity* entList[100];
s32 entCount;
};
#endif
///////////////////////////////////////
//CEntityManager.cpp
///////////////////////////////////////
#include "CEntityManager.h"
s32 CEntityManager::getEntityCount() {
return this->entCount;
}
void CEntityManager::newEntity(s32 type) {
switch (type) {
case ENT_NULL:
break;
case ENT_PLAYER:
//this->entList[this->entCount] = new CPlayer;
break;
}
this->entCount++;
}
that still will quit once it hits the line ''this->entCount++;''!!
I''ve never run into this problem before! does enyone know what the problem might be?
I use this-> just for readability, just so i know for a fact that the variable is a part of that class.... But still im getting the same wierd problem... anyone know what might be the case?
I honestly don't know exactly where to begin, however in your newEntity() method, I don't think you should increment the entCount variable for both cases, only the ENT_PLAYER one. Also to perhaps save yourself some errors later down the track, you should initialize entList[n] = NULL in the constructor, and then to prevent memory leaks, free each entity from memory by using delete entList[n] in the destructor.
Oh also be sure to try rebuilding the entire app again - not just recompiling the current cpp file.
[edited by - wavarian on June 4, 2004 10:07:50 PM]
Oh also be sure to try rebuilding the entire app again - not just recompiling the current cpp file.
[edited by - wavarian on June 4, 2004 10:07:50 PM]
Thanks a lot for the tips! I added the stuff in the constructor and deconstructor... but obvioulsy that didn''t help the problem.
I rebuilt the whole app and it still quits on either line!
Man... what can possibly be happening
I rebuilt the whole app and it still quits on either line!
Man... what can possibly be happening
a few words:
you might consider making ''s32 CEntityManager::getEntityCount()'' const, just good form in my opinion
dumb question maybe, but I''m assuming CPlayer is a type of CEntity? just making sure
as for the error with ''this->entCount++'', I''m somewhat stumped. One suggestion (although I don''t see how a compiler could make this oversight) would be to put some parentheses in there, or get rid of the ''this->'' part, so one of the following instead to remove ambiguity:
(this->entCount)++; or
entCount++;
gcc handles it correctly in either case, but I''m don''t know what you''re using.
Another quick debuging idea for the other line that is still commented out: try doing something like ''this->entList[0] = new CPlayer;'' and see if it will run at least the once (obviously you''ll be stepping on memory problems if you try it over and over). Take what wAVaRiaN said to heart and consider his memory management technique early on to save yourself a headache.
One more suggestion: turn on all possible compile-time warnings, and maybe you''ll gain some insight.
Anyway, good luck with this, I''m thinking there''s a chance that your problem may not be isolated with this code you''ve supplied us, the bug may be somewhere else and creeping in.
Elijah
you might consider making ''s32 CEntityManager::getEntityCount()'' const, just good form in my opinion
dumb question maybe, but I''m assuming CPlayer is a type of CEntity? just making sure
as for the error with ''this->entCount++'', I''m somewhat stumped. One suggestion (although I don''t see how a compiler could make this oversight) would be to put some parentheses in there, or get rid of the ''this->'' part, so one of the following instead to remove ambiguity:
(this->entCount)++; or
entCount++;
gcc handles it correctly in either case, but I''m don''t know what you''re using.
Another quick debuging idea for the other line that is still commented out: try doing something like ''this->entList[0] = new CPlayer;'' and see if it will run at least the once (obviously you''ll be stepping on memory problems if you try it over and over). Take what wAVaRiaN said to heart and consider his memory management technique early on to save yourself a headache.
One more suggestion: turn on all possible compile-time warnings, and maybe you''ll gain some insight.
Anyway, good luck with this, I''m thinking there''s a chance that your problem may not be isolated with this code you''ve supplied us, the bug may be somewhere else and creeping in.
Elijah
Thanks for the tips!
CPlay is a part of CEntity through inheritance:
//
class CPlayer : public CEntity
//
But still the problem exists...
But.. I was messing around a bit, and I have come to the conclusion that this happends any time I change a variable within the class.... I was writting a class CConsole today and I ran into the same problem...
(*wierded out!)
It might be my framework (which is actualy based on somebody elses)... the whole program is based around one class (CGame)... There really not too much code to post, I could post the whole shibang. I will spend some more time messing around, and If I have no luck, I will come crawling back here....
(*runs to his room and sheds a tear)
CPlay is a part of CEntity through inheritance:
//
class CPlayer : public CEntity
//
But still the problem exists...
But.. I was messing around a bit, and I have come to the conclusion that this happends any time I change a variable within the class.... I was writting a class CConsole today and I ran into the same problem...
(*wierded out!)
It might be my framework (which is actualy based on somebody elses)... the whole program is based around one class (CGame)... There really not too much code to post, I could post the whole shibang. I will spend some more time messing around, and If I have no luck, I will come crawling back here....
(*runs to his room and sheds a tear)
Hmm... I am using CEntityManager as a pointer... would that be a reason?
CEntityManager* entityManager;
CEntityManager* entityManager;
Darn, I tried a lot of other things and found nothing... I guess ill just post the whole enchilata
This is based on the Irrlicht Engine if any of you guys know anything about it.
CEntityManager.h
CEntityManager.cpp
CGame.h
CGame.cpp
Main.cpp
This is based on the Irrlicht Engine if any of you guys know anything about it.
CEntityManager.h
///////////////////////////////////////
// CEntityManager.h
// By: Colin Hill 2004
///////////////////////////////////////
#ifndef __CENTITYMANAGER_H_INCLUDED__
#define __CENTITYMANAGER_H_INCLUDED__
#include "CGame.h"
class CGame;
#include "CPlayer.h"
//! The entity manager is just a class that handles
//! all of the entities to make is a lot more simplistic
class CEntityManager {
public:
// Constructors & Deconstructors
CEntityManager(): entCount(0) {
for(s32 i = 0; i < 100; i++) entList[i] = NULL;
}
~CEntityManager() {
for(s32 i = 0; i < 100; i++) delete [] entList[i];
}
// Accessors
const s32 getEntityCount();
// General Methods
void newEntity(s32 type);
private:
CEntity* entList[100];
s32 entCount;
};
#endif
CEntityManager.cpp
///////////////////////////////////////
// CEntityManager.cpp
// By: Colin Hill 2004
///////////////////////////////////////
#include "CEntityManager.h"
//! Returns the amount of entities
const s32 CEntityManager::getEntityCount() {
return this->entCount;
}
//! Creates a new entity
void CEntityManager::newEntity(s32 type) {
switch (type) {
case ENT_NULL:
break;
case ENT_PLAYER:
//this->entList[this->entCount] = new CPlayer;
break;
}
this->entCount++;
}
CGame.h
///////////////////////////////////////
// CGame.h
// By: Colin Hill 2004
///////////////////////////////////////
#ifndef __CGAME_H_INCLUDED__
#define __CGAME_H_INCLUDED__
//Includes
#include <windows.h>
#include <irrlicht.h>
#pragma comment(lib, "irrlicht.lib")
//Namespaces
using namespace audiere;
using namespace irr;
using namespace core;
using namespace scene;
using namespace video;
using namespace io;
using namespace gui;
#include "CEntityManager.h"
class CEntityManager;
class CGame : public IEventReceiver {
public:
CEntityManager* entityManager;
IVideoDriver* driver;
ISceneManager* smgr;
IGUIEnvironment* guienv;
IrrlichtDevice *device;
void init(); //Initializes everything
void deInit(); //Deinitialize everything
void render(); //Renders
void mainLoop(); //Loops
virtual bool OnEvent(SEvent event);
};
#endif
CGame.cpp
///////////////////////////////////////
// CGame.cpp
// By: Colin Hill 2004
///////////////////////////////////////
#include "CGame.h"
#include <fstream>
void CGame::init() {
//Create Irrlicht device
device = createDevice(EDT_DIRECTX9, dimension2d<s32>(1024, 768), 32, true, false, this);
//Set caption
device->setWindowCaption(L"GoretekFPS Alpha v0.02");
//Set up the driver, scene manager, and GUI enviornment
driver = device->getVideoDriver();
smgr = device->getSceneManager();
guienv = device->getGUIEnvironment();
//Draw loading screen
ITexture* load;
load = driver->getTexture("load.bmp");
driver->beginScene(true, true, SColor(0,0,0,0));
driver->draw2DImage(load, position2d<s32>((1024/2)-256,(768/2)-64), rect<s32>(0,0,512,128), 0, SColor(255,255,255,255), true);
driver->endScene();
//Load data pack3 file
device->getFileSystem()->addZipFileArchive("data0.pk3");
entityManager->newEntity(ENT_PLAYER);
}
void CGame::deInit() {
device->drop();
}
void CGame::render() {
driver->beginScene(true, true, SColor(255, 0, 0, 0));
smgr->drawAll();
driver->endScene();
}
void CGame::mainLoop() {
render();
}
bool CGame::OnEvent(SEvent event) {
return false;
}
Main.cpp
///////////////////////////////////////////
// Goretek.h //
// //
// By: Colin Hill 2004 //
// //
// About: Goretek is a network play //
// first person shooter game that is //
// coded very simply for easy modifying. //
// It is made for my own satisfaction //
// and for my own learning. :) //
///////////////////////////////////////////
#include "CGame.h"
INT WINAPI WinMain(HINSTANCE hInst, HINSTANCE, LPSTR strCmdLine, INT) {
CGame game;
game.init();
while(game.device->run())
{
game.mainLoop();
}
game.deInit();
return 0;
}
It appears that the variable entityManager in CGame is never initialized. So in newEntity(), this is probably garbage.
John Bolton
Page 44 Studios
Current project: NHL Faceoff 2005 PS2
John Bolton
Page 44 Studios
Current project: NHL Faceoff 2005 PS2
lol!
all this time and energy over that!
I cant believe I missed that!!
Thanks a lot!!!
it works fine now!
all this time and energy over that!
I cant believe I missed that!!
Thanks a lot!!!
it works fine now!
There are still quite a few things wrong with your CEntityManager class.
1) I''m not sure if you''re trying to inline your constructor and destructor by defining them in the header file, but you can bet that the compiler is going to ignore you because you''re looping through 100 elements. You''re also deallocating memory (incorrectly, I''ll get to that next) in your destructor so there''s really no chance of inlining there.
2) It looks like entList is an array of CEntity pointers, which point to a single new CEntity object, but for some reason you are calling delete[] on these single objects. Your destructor right now is doing some strange things. Your destructor should look like this:
virtual ~CEntityManager() {
for(s32 i = 0; i < 100; ++i) delete entList;
}
I made the destructor virtual because it looks like you''re using a lot of inheritance. You should always do this just to be safe.
3) Your getEntityCount() function puts the const in the wrong place. Right now, the function returns a const *copy* of the entityCount member. This is really pointless and probably not what you want. You probably meant to do this:
s32 getEntityCount() const
{
return entCount;
}
const here means that the function won''t change the object. Now it really depends on what kind of type s32 is, but if it''s *not* a pointer then you should be good with that. It will return a copy of the entCount member.
If you want to return a member and don''t want people to modify it, and in addition you want the performance hack of pass-by-reference, you should do this:
const s32& getEntityCount() const
{
return entCount;
}
but you need to think to yourself whether implementing performance hacks is really what you want to do when you''re trying to write programs. You should be making them work first and optimizing later. For this same reason I suggest avoiding pointers and arrays as much as possible until your program works. Then go back and use them *if necessary*.
1) I''m not sure if you''re trying to inline your constructor and destructor by defining them in the header file, but you can bet that the compiler is going to ignore you because you''re looping through 100 elements. You''re also deallocating memory (incorrectly, I''ll get to that next) in your destructor so there''s really no chance of inlining there.
2) It looks like entList is an array of CEntity pointers, which point to a single new CEntity object, but for some reason you are calling delete[] on these single objects. Your destructor right now is doing some strange things. Your destructor should look like this:
virtual ~CEntityManager() {
for(s32 i = 0; i < 100; ++i) delete entList;
}
I made the destructor virtual because it looks like you''re using a lot of inheritance. You should always do this just to be safe.
3) Your getEntityCount() function puts the const in the wrong place. Right now, the function returns a const *copy* of the entityCount member. This is really pointless and probably not what you want. You probably meant to do this:
s32 getEntityCount() const
{
return entCount;
}
const here means that the function won''t change the object. Now it really depends on what kind of type s32 is, but if it''s *not* a pointer then you should be good with that. It will return a copy of the entCount member.
If you want to return a member and don''t want people to modify it, and in addition you want the performance hack of pass-by-reference, you should do this:
const s32& getEntityCount() const
{
return entCount;
}
but you need to think to yourself whether implementing performance hacks is really what you want to do when you''re trying to write programs. You should be making them work first and optimizing later. For this same reason I suggest avoiding pointers and arrays as much as possible until your program works. Then go back and use them *if necessary*.
Topic Locked
This topic has been locked by a moderator. New replies are not allowed.
Sign in to reply to this topic.
Advertisement