problem with classes in c++

If you are a new Irrlicht Engine user, and have a newbie-question, this is the forum for you. You may also post general programming questions here.
elmuppito69
Posts: 6
Joined: Sat Jul 26, 2008 6:46 pm
Location: England

problem with classes in c++

Post by elmuppito69 »

Hello all :) ,

I have a problem with OOP+irrlicht custom scene nodes

I am trying to create a class for each entity within my game,and manipulate them induvidually.
I have a class called Bug, i create n number of objects, place them in a 3d world etc...
i use

Code: Select all

IAnimatedMesh* buggmesh = smgr->getMesh("bug2.b3d");
	IAnimatedMeshSceneNode* bugg = 
smgr->addAnimatedMeshSceneNode( buggmesh );
and use

Code: Select all

bugg->setPosition(....
to move the bugg, scale it....

which works fine.
The problem i have is this....
In the class i have my AI setup, which,say, tells the bug what to do.But as all the objects have the same mesh identifier

Code: Select all

IAnimatedMeshSceneNode* bugg = 
smgr->addAnimatedMeshSceneNode( buggmesh );[/code]
when i use

Code: Select all

bugg->setPosition(bugg->getPosition()+vector3df(0,0,-0.15));
it moves all of the objects, not the one i want.

Hmmm, Sorry, i have difficulty writing what im thinking.
I know how to create static objects that use the same mesh, it is just a case of loading them, then forgetting them(if you wish)
but i need to identify each movable object....
eg.
i want to write...

Code: Select all

for(n=0;n<10;n++)
bug.update(n)
where n is the bugs id#

I hope this makes sense, if not i will try to explain again.
pls note i am fairly good at programming,just not explaining.

thanks for reading this all the same, i will add to this if i think of another way to explain
Q.Why isn't Linux Main stream?
A.People love to spend money!
Seven
Posts: 1034
Joined: Mon Nov 14, 2005 2:03 pm

Post by Seven »

Class Bug
{
public :

IAnimatedMesh* buggmesh;
IAnimatedMeshSceneNode* bugg;

Bug::Bug()
{
buggmesh = NULL;
bugg = NULL;
}

virtual bug::~Bug();
{
// destroy bugmesh and bug
}

bool Create()
{
buggmesh = smgr->getMesh("bug2.b3d");
bugg = smgr->addAnimatedMeshSceneNode( buggmesh );
}

void SetPosition(vector3df pos)
{
if (bugg) bugg->setPosition(pos);
}

void Update()
{
// move bug around and cause havoc in the world
}

}



// main

for(n=0;n<10;n++)
{
bugs[n] = new Bug();
bugs[n]->Create();
Bugs[n]->SetPosition(vector3df(GetRandomInt(),0,GetRandomInt());
}


// loop

for(n=0;n<10;n++)
{
if (bugs[n]) bugs[n]->update();
}


but maybe that isnt what you asked? are you trying to OOP the scenenode so that your bugg is actually a scenenode or are you trying to create a game object class that houses a scenenode?

sorry if i misunderstood the question.....
elmuppito69
Posts: 6
Joined: Sat Jul 26, 2008 6:46 pm
Location: England

Post by elmuppito69 »

thnaks for the quick reply, i have to goto work now, i will have to read your post fully tomorrow.
dont worry if you didn't understand question, im not too good at explaining.

am just trying to create several "enemies" that think and move on thier own.i have done this in DarkGdk for VC++.but that uses an integer for an id#,not a pointer for the mesh.
Darkgdk uses something like this:
-Load mesh.
-Createnode(id#,mesh.....)
-movenode(id#,....)
etc. this makes it real simple to make a class, as all i have to do is remember the id numbers. so i could call bug.update(n),where n is the id#

i hope this makes sense.

you might have answered the question already, if so...sorry.
shall be back tomorrow, to read your first reply again(too tired to think)
Thanks all the same tho! :D
Q.Why isn't Linux Main stream?
A.People love to spend money!
Seven
Posts: 1034
Joined: Mon Nov 14, 2005 2:03 pm

Post by Seven »

ok, the cool part about this all is that you can then do this...


Class A_Different_Bug : public bug
{
public :

A_Different_Bug::A_Different_Bug()
{
}

virtual A_Different_Bug::~A_Different_Bug();
{
}

bool Create()
{
buggmesh = smgr->getMesh("A_Different_Bug.b3d");
bugg = smgr->addAnimatedMeshSceneNode( buggmesh );
}

}



// main
bug bugs[10];

for(n=0;n<10;n++)
{
use either bugs[n] = new Bug();
or bugs[n] = a_different_bug();

bugs[n]->Create();
Bugs[n]->SetPosition(vector3df(GetRandomInt(),0,GetRandomInt());
}


// loop

for(n=0;n<10;n++)
{
if (bugs[n]) bugs[n]->update();
}

and it will update the bug whether it is a bug or a_different_bug
rogerborg
Admin
Posts: 3590
Joined: Mon Oct 09, 2006 9:36 am
Location: Scotland - gonnae no slag aff mah Engleesh
Contact:

Re: problem with classes in c++

Post by rogerborg »

elmuppito69 wrote:pls note i am fairly good at programming,just not explaining.
Image

I'm sure a 1337 h4xx0rz like you can figure this out all by yourself.
Please upload candidate patches to the tracker.
Need help now? IRC to #irrlicht on irc.freenode.net
How To Ask Questions The Smart Way
frostysnowman
Posts: 83
Joined: Fri Apr 11, 2008 3:06 am
Location: Beaverton, Oregon, US

Post by frostysnowman »

Have you taken computer science III yet?

EDIT: ^^It's called polymorphism so you know.
Seven
Posts: 1034
Joined: Mon Nov 14, 2005 2:03 pm

Post by Seven »

sheesh.... you guys are rough on this one. we all learned somewhere. I'm glad some good people took the time to help me out when I was starting. think back to your 'good old days' and see if we cant help this guy out a little bit.
zeno60
Posts: 342
Joined: Sun May 21, 2006 2:48 am
Location: NC, USA
Contact:

Post by zeno60 »

elmuppito69 wrote:shall be back tomorrow, to read your first reply again(too tired to think)
I for one wish I could see his face when he returns...

Bright morning, fresh coffee, birds chirping, ahhhh, now to check the forums....EWWAAAAAAAH?!
dlangdev
Posts: 1324
Joined: Tue Aug 07, 2007 7:28 pm
Location: Beaverton OR
Contact:

Post by dlangdev »

what a big surprise.
Image
Seven
Posts: 1034
Joined: Mon Nov 14, 2005 2:03 pm

Post by Seven »

zeno60 wrote:
elmuppito69 wrote:shall be back tomorrow, to read your first reply again(too tired to think)
I for one wish I could see his face when he returns...

Bright morning, fresh coffee, birds chirping, ahhhh, now to check the forums....EWWAAAAAAAH?!

hehehe... it should get him all perked up and ready to go I think :)
elmuppito69
Posts: 6
Joined: Sat Jul 26, 2008 6:46 pm
Location: England

Post by elmuppito69 »

Yep im uber thick! :oops: i think. After rereading my origional post i realized i was asking the wrong question, shame i only realized when i was at work!

This might explain better:

Code: Select all

virtual IAnimatedMeshSceneNode* irr::scene::ISceneManager::addAnimatedMeshSceneNode ( IAnimatedMesh * mesh, ISceneNode * parent = 0, s32 id = -1, const core::vector3df & position = core::vector3df(0, 0, 0), const core::vector3df & rotation = core::vector3df(0, 0, 0), const core::vector3df & scale = core::vector3df(1.0f, 1.0f, 1.0f), bool alsoAddIfMeshPointerZero = false) [pure virtual]
the part i cant work out is:
id,: Id of the node. This id can be used to identify the scene node.
i already have a class which controls the "bug" etc. but i need to be able to write:

Code: Select all

bug->setPosition(bugg->getPosition()+vector3df(0,0,-0.15));
say,but using and id.

ie...

Code: Select all

if (faerie)

	{

		node = smgr->addAnimatedMeshSceneNode(faerie);

		node->setPosition(core::vector3df(-70,0,-90));

		node->setMD2Animation(scene::EMAT_RUN);

		node->getMaterial(0) = material;



		node = smgr->addAnimatedMeshSceneNode(faerie);

		node->setPosition(core::vector3df(-70,0,-30));

		node->setMD2Animation(scene::EMAT_SALUTE);

		node->getMaterial(0) = material;



		node = smgr->addAnimatedMeshSceneNode(faerie);

		node->setPosition(core::vector3df(-70,0,-60));

		node->setMD2Animation(scene::EMAT_JUMP);

		node->getMaterial(0) = material;

	}

how can i manipulate the 3 faeries seperatle inside my render loop?

Hope this makes sense.
thanks for the replies tho...hmmm...the helpful ones nyway.
Q.Why isn't Linux Main stream?
A.People love to spend money!
elmuppito69
Posts: 6
Joined: Sat Jul 26, 2008 6:46 pm
Location: England

Post by elmuppito69 »

by the way, i work at night, so wake up to dark sky, police sirens, drunks shouting, ahhh..lol
Q.Why isn't Linux Main stream?
A.People love to spend money!
Seven
Posts: 1034
Joined: Mon Nov 14, 2005 2:03 pm

Post by Seven »

you will have to keep track of all 3 fairies independently, either by reference or by pointer. you are trying to have a single class (bug) that somehow updates multiple instances of a class without remembering where they are. I think that the overall design probably needs rethought but it's hard to tell. If you wanted to zip the foler up and send t to me I would be willing to review it, otherwise, we can just chat here. In any event, you will need to have a list of either ID numbers or SceneNode pointers that you can traverse through in order to do what you are trying to do.


// an array of ID numbers relating to scenenodes
int Bugs[10];

// create the scene nodes
for (int x=0; x<10; x++)
{
IAnimatedMesh* buggmesh = smgr->getMesh("bug2.b3d");
IAnimatedMeshSceneNode* bugg = smgr->addAnimatedMeshSceneNode( buggmesh );

// set the scene nodes ID to something
bugg->setId(900+x);

// remember the ID number in the array of ID's
Bugs[x] = bugg->getId();
}

// now, in the loop
for(n=0;n<10;n++)
{
// get a pointer to the scenenode that you want to move
// do this by finding the scenenode using the ID that you saved
// earlier in the Bugs[] array
ISceneNode* node = smgr->getNodeByID(Bugs[n]);

// move the scenenode
node->setPosition(node->getPosition()+vector3df(0,0,-0.15));
}
Seven
Posts: 1034
Joined: Mon Nov 14, 2005 2:03 pm

Post by Seven »

Code: Select all

// this class maintains a list of CSObject derived instances
// that were created by the ObjectFactory
class CSObjectManager
{
	// list variables
	ADD_PUBLIC_SETGET(ObjectList,m_List,List);		// the list of CSObject pointers

	CSObjectManager();						// class constructor
	virtual ~CSObjectManager();				// class destructor
	
	virtual void Initialize();				// set all variables to a known value
	virtual bool Create();					// dual creation allows for better error handling
	virtual bool Cleanup();					// cleanup whatever memory mess we made

	virtual int	GetUniqueId();				// select a unique id value

	virtual void		AddObject(CSObject* obj);	// add an object to the list
	virtual CSObject*	FindObjectByID(int id);		// retrieve pointer to object
	virtual void		DeleteObjectByID(int id);	// delete the requested object	
	virtual void		Frame();					// let all objects do their thing

	virtual CSObject* GetNextObject(bool newsearch);

	void KillDeadObjects() { m_List.KillDeadObjects(); };
	void SetAllObjectsToDead() { m_List.SetAllObjectsToDead(); };

	void SetDebug(bool value,OBJECT_TYPE type);
	virtual int GetCount() { return m_List.GetCount();	};
	void SetFogEnable(bool value)	{	return m_List.SetFogEnable(value);		};
	void SetLightEnable(bool value)	{	return m_List.SetLightEnable(value);	};
};

Code: Select all

#include "stdafx.h"
#include "CSObjectManager.h"

////////////////////////////////////////////////////////////////////////////////////////
// object class list
////////////////////////////////////////////////////////////////////////////////////////

ObjectList::ObjectList()
{
	// initialize all variables
	Initialize();
}

ObjectList::~ObjectList()
{
	// cleanup our memory mess
	Cleanup();
}

void ObjectList::Initialize()
{
	m_ID = 0x10000001;
}

bool ObjectList::Cleanup()
{
	// run through the list of messages and delete them
	for (m_Iterator = m_List.begin(); m_Iterator != m_List.end();)
	{
		delete(*m_Iterator);
		m_Iterator = m_List.erase(m_Iterator);
	}

	// clear the list once and for all
	m_List.clear();

	// always return false from a cleanup function
	return false;
}

bool ObjectList::Create()
{
	// everythign went fine
	return true;
}

bool ObjectList::Add(CSObject* data)
{
	m_List.push_front(data);

	//Debug_Logf("Adding object with ID = %d\n", data->GetId());

	return true;
}

void ObjectList::DeleteObjectByID(int id)
{
	CSObjectListIterator i;
	for (i = m_List.begin(); i != m_List.end(); )
	{
		if ( (*i)->GetId() == id )
		{
			delete(*i);
			i = m_List.erase(i);
		}
		else 
		{
			++i;
		}
	}

}

void ObjectList::SetAllObjectsToDead()
{
	CSObjectListIterator i;
	for (i = m_List.begin(); i != m_List.end(); ++i)
	{
		(*i)->SetDead(true);
	}
}

// retrieve pointer to object
CSObject* ObjectList::FindObjectByID(int id)
{
	CSObjectListIterator i;
	for (i = m_List.begin(); i != m_List.end(); ++i)
	{
		if ( (*i)->GetId() == id ) return(*i);
	}

	return NULL;
}

void ObjectList::KillDeadObjects()
{
	CSObjectListIterator i;
	for (i = m_List.begin(); i != m_List.end(); )
	{
		if ( (*i)->GetDead())
		{
			delete(*i);
			i = m_List.erase(i);
		}
		else 
		{
			++i;
		}
	}
}

void ObjectList::Frame()
{
	CSObjectListIterator i;
	for (i = m_List.begin(); i != m_List.end(); ++i)
		(*i)->Frame();
}

CSObject* ObjectList::GetNextObject(bool newsearch)
{
	if (m_List.empty()) return NULL;

	if (newsearch) 
	{
		m_Index = m_List.begin();
		return (*m_Index);
	}

	if (++m_Index == m_List.end())	return NULL; else return (*m_Index);

	return NULL;
}

void ObjectList::SetDebug(bool value,OBJECT_TYPE type)
{
	CSObjectListIterator i;
	for (i = m_List.begin(); i != m_List.end(); ++i)
	{
		if ((*i)->GetObjectType() == type)	(*i)->SetDebug(value);
	}
}

int ObjectList::GetCount()
{
	int count = 0;
	CSObjectListIterator i;
	for (i = m_List.begin(); i != m_List.end(); ++i)
	{
		count++;
	}
	return count;
}

void ObjectList::SetFogEnable(bool value)
{
	CSObjectListIterator i;
	for (i = m_List.begin(); i != m_List.end(); ++i)
	{
		(*i)->SetFogEnable(value);
	}
}

void ObjectList::SetLightEnable(bool value)
{
	CSObjectListIterator i;
	for (i = m_List.begin(); i != m_List.end(); ++i)
	{
		(*i)->SetLightEnable(value);
	}
}


////////////////////////////////////////////////////////////////////////////////////////

	
CSObjectManager::CSObjectManager()
{
	// set all variables to a known value
	Initialize();
}

CSObjectManager::~CSObjectManager()
{
	// cleanup whatever memory mess we made
	Cleanup();
}

void CSObjectManager::Initialize()
{
}

bool CSObjectManager::Create()
{
	// everything went fine
	return true;
}

bool CSObjectManager::Cleanup()
{
	// make sure the list is clear
	m_List.Cleanup();

	// always return false from a cleanup function
	return false;
}

// retrieve pointer to object
CSObject* CSObjectManager::FindObjectByID(int id)
{
	return m_List.FindObjectByID(id);
}

void CSObjectManager::AddObject(CSObject* obj)
{
	// add the object to the object list
	m_List.Add(obj);
}

int CSObjectManager::GetUniqueId()
{
	return m_List.GetUniqueID();
}

void CSObjectManager::DeleteObjectByID(int id)
{
	m_List.DeleteObjectByID(id);
}

void CSObjectManager::Frame()
{
	m_List.Frame();
}

CSObject* CSObjectManager::GetNextObject(bool newsearch)
{
	return m_List.GetNextObject(newsearch);
}

void CSObjectManager::SetDebug(bool value, OBJECT_TYPE type)
{
	m_List.SetDebug(value, type);
}

elmuppito69
Posts: 6
Joined: Sat Jul 26, 2008 6:46 pm
Location: England

Post by elmuppito69 »

thanks for the reply.i think what i'm trying to do isn't possible in irrlicht.your method is nice,but i didn't want to do it like that :(
i'm sure i will have to stick with my origional design(1 object for each node).
i know how to change the "id" and get it using setid() + getid(), i just thought i could manipulate any node just using this "id".
i can understand why you think this is a bad idea, but i just cant explain why i want to do this,(just don't know how to).
anyway, i would just like to thank you for your time and effort to help me. :)
Q.Why isn't Linux Main stream?
A.People love to spend money!
Post Reply