accessing object from a pointer to one parameter

Discussion about everything. New games, 3d math, development tips...
Post Reply
robertgarand
Posts: 69
Joined: Fri Feb 22, 2008 6:47 pm
Location: montreal
Contact:

accessing object from a pointer to one parameter

Post by robertgarand »

Hi all,
I made a class name jets looks about like:

Code: Select all

class jets
	{
	public:
	static int n_jet;
	bool isVisible;
        bool isDead;
	bool dying;
	irr::scene::IMeshSceneNode* jet_mesh;
       };
I etrieve a pointer to jet_mesh, from getSceneNodeFromScreenCoordinatesBB.
How can I access the dying parameter from the pointer ?
My project is getting damn close to alpha release...
TIA,
robert
If it can't be writen, it can't exist
Seven
Posts: 1034
Joined: Mon Nov 14, 2005 2:03 pm

Post by Seven »

this code is written from memory so dont cut and paste :0

set the node's ID to some value, then, when you get your node pointer, look up the Jet structure using the ID number.

Code: Select all


jets* array_of_jets[100];

for (int x=0; x<100; x++)
{
     array_of_jets[x] = new jets;
     IAnimatedMesh* AnimatedMesh = SMGR->getMesh("myJet.x");
     array_of_jets[x]->jet_mesh = SMGR->addIMeshSceneNode(AnimatedMesh);
     
     // set the meshscenenode ID to a known value
     array_of_jets[x]->jet_mesh = ->setID(7777 + x);
     array_of_jets[x]->isDead = false;
     array_of_jets[x]->isDying = false;
}

// get the pointer to the scene node
IMeshSceneNode* selected_node = getSceneNodeFromScreenCoordinatesBB();


// now... convert that to the structure using the ID as the index into the array
jets* a_jet_from_the_array = array_of_jets[7777 - selected_node->getID()];

// and finally, you can see the dead, dying stuff
if (a_jet_from_the_array->isDead)
{
// delete it from the array?
}
 

Seven
Posts: 1034
Joined: Mon Nov 14, 2005 2:03 pm

Post by Seven »

in my app, i actually use the setName() and the setID() functions to be able to get a poitner to the object class when selecting a node or a mesh. I use the ID to set the bit pattern for the 'picking' code, and i use the name (just the ID converted to a string) so that I can find the object later by converting the name back into an integer.

Code: Select all

void CSObject::LoadAnimatedSceneNode(char* filename)
{
	// return to the base directory
	RemoveAnimatedSceneNode();	

	m_AnimatedMesh = SMGR->getMesh(filename);
	if (m_AnimatedMesh)
	{
		m_AnimatedSceneNode = SMGR->addAnimatedMeshSceneNode(m_AnimatedMesh);
		if (m_AnimatedSceneNode)
		{
			m_AnimatedSceneNode->setPosition(core::vector3df(0,0,0));
			m_AnimatedSceneNode->setScale(core::vector3df(1,1,1));	
			m_AnimatedSceneNode->setMaterialFlag(video::EMF_LIGHTING, CS_USE_LIGHT);
			m_AnimatedSceneNode->setMaterialFlag(video::EMF_FOG_ENABLE, CS_USE_FOG);

			// add shadow
			m_AnimatedSceneNode->addShadowVolumeSceneNode();	
			SMGR->setShadowColor(video::SColor(220,0,0,0));
		}
		else m_AnimatedMesh->drop();
	}

	// set the object type for the editor
	SetObjectType(GetObjectType());
}

Code: Select all

// be careful here. the 'editor' object requires a bit pattern to be set in order to 
// distinguish between node types. if you add another node, make sure to set the id of it also
void CSObject::SetObjectType(OBJECT_TYPE ot) 
{ 
	m_ObjectType = ot; 
	if (m_Node) 
	{
		m_Node->setID(ot); 
		m_Node->setName(IntToString(m_Id));
	}
	if (m_AnimatedSceneNode) 
	{
		m_AnimatedSceneNode->setID(ot); 
		m_AnimatedSceneNode->setName(IntToString(m_Id));
	}		
} 


in the editor object

Code: Select all


SetSelectedNode(m_CollMan->getSceneNodeFromScreenCoordinatesBB(m_Level->m_App->m_Engine->m_Device->getCursorControl()->getPosition())); 


void CSObject_Editor::SetSelectedNode(ISceneNode* node)
{
	// remember this number	
	m_SelectedNode = node;

	// convert the node->name to a number
	if (m_SelectedNode) SetSelectedObject(atoi(m_SelectedNode->getName()));
	else SetSelectedObject(0);
}

Seven
Posts: 1034
Joined: Mon Nov 14, 2005 2:03 pm

Post by Seven »

It would be SOOOOOOO much easier if we had a void* UserData stuck in the node code, but my spider sense tells me that opening THAT can of worms again is not worth it :)
night_hawk
Posts: 153
Joined: Mon Mar 03, 2008 8:42 am
Location: Suceava - Romania
Contact:

Post by night_hawk »

Derive from IAnimatedMeshSceneNode (or whatever). Add pointers to your stuff, or direct data, (or whatever). When getting the scene node via above mentioned function, just cast it to your new scene node type. If done properly with all required checks, it should work.
Seven
Posts: 1034
Joined: Mon Nov 14, 2005 2:03 pm

Post by Seven »

It's a nice method if you have only a few nodes to derive. If you are looking to select any node though, including terrain, particles, lights etc..., then recreating each as a derived node gets a little more time consuming. storing a void* userData would make the task much simpler I think.
night_hawk
Posts: 153
Joined: Mon Mar 03, 2008 8:42 am
Location: Suceava - Romania
Contact:

Post by night_hawk »

Seven wrote:It's a nice method if you have only a few nodes to derive. If you are looking to select any node though, including terrain, particles, lights etc..., then recreating each as a derived node gets a little more time consuming. storing a void* userData would make the task much simpler I think.
Bitmask.
sio2
Competition winner
Posts: 1003
Joined: Thu Sep 21, 2006 5:33 pm
Location: UK

Post by sio2 »

Advice: name your class "Jet" if it hold a single instance, not "jets".

Inherit from IMeshSceneNode, for example:

Code: Select all

class Jet : public irr::scene::IMeshSceneNode
{
public:
bool m_bMyVar;
};
You can then use reinterpret_cast on the pointer returned by getSceneNodeFromScreenCoordinatesBB to convert the pointer to a class Jet and access your member vars...
robertgarand
Posts: 69
Joined: Fri Feb 22, 2008 6:47 pm
Location: montreal
Contact:

I get the idea

Post by robertgarand »

Thx seven and others,

I already use the bitmask id to target only certain enemies.
I'll use your indexing over it, and let you know how it worked out.

More I learn, more I see I know so little.

Regards,
Robert
If it can't be writen, it can't exist
Seven
Posts: 1034
Joined: Mon Nov 14, 2005 2:03 pm

Post by Seven »

night_hawk wrote:Bitmask.
I agree that the ID allows you to bitmask when picking, and I also agree that you can derive your scenenode from the base and then add variables to your hearts content. but in the example the original poster gave, it looks like he is storing his data outside of the scenenode (in jets) and wanting to access it based upon which scene node was selected, which he cannot do without some manner of knowing which jets structure is holding the scenenode. I guess he could also just scan the list of jets to see if the scene node matches. I would just prefer to store the link in the name field and then decode it to my understanding.
Seven
Posts: 1034
Joined: Mon Nov 14, 2005 2:03 pm

Re: I get the idea

Post by Seven »

robertgarand wrote:Thx seven and others,

I already use the bitmask id to target only certain enemies.
I'll use your indexing over it, and let you know how it worked out.

More I learn, more I see I know so little.

Regards,
Robert
I didnt say this was the best way to do it. I only answered what you said you wanted to do :)

the others are showing you a better way to do it.

be careful what you ask for.......you might just get it.
sio2
Competition winner
Posts: 1003
Joined: Thu Sep 21, 2006 5:33 pm
Location: UK

Post by sio2 »

Seven wrote:I agree that the ID allows you to bitmask when picking, and I also agree that you can derive your scenenode from the base and then add variables to your hearts content. but in the example the original poster gave, it looks like he is storing his data outside of the scenenode (in jets) and wanting to access it based upon which scene node was selected, which he cannot do without some manner of knowing which jets structure is holding the scenenode. I guess he could also just scan the list of jets to see if the scene node matches. I would just prefer to store the link in the name field and then decode it to my understanding.
I think dynamic_cast will do exactly what he wants. This will convert the specific object returned by the raycast call to a "jets" pointer. You can then get at the data via jets->isDead, etc. I believe you'll need "class jets : public scene::IMEshSceneNode" though.
robertgarand
Posts: 69
Joined: Fri Feb 22, 2008 6:47 pm
Location: montreal
Contact:

Work perfectly

Post by robertgarand »

Just to let you know, it worked perfectly,
even if itis not the best way, it is quick and efficient.
thanks again,
regards,
Robert.
If it can't be writen, it can't exist
Post Reply