Noob question about subclassing a IMeshSceneNode

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.
Post Reply
Tranen
Posts: 34
Joined: Mon May 05, 2008 5:43 pm

Noob question about subclassing a IMeshSceneNode

Post by Tranen »

Hi, I want to have an object that is an IMeshSceneNode but that contains other informations (for example a status), something like:

MyNode :: IMeshSceneNode
(
MyNode( ... );
int status;
int getStatus();
setStatus(int);
)

what is the best way to do this?
ent1ty
Competition winner
Posts: 1106
Joined: Sun Nov 08, 2009 11:09 am

Post by ent1ty »

Create you own struct/class (whichever keyword you like more) which has all you want in it.
I like struct

Code: Select all

struct myMeshSceneNode{
private:
IMeshSceneNode* node;
int status;

public:
int getStatus() const
{
  return status;
}

void setStatus(int newStatus)
{
  status= newStatus;
  return;
}

void setNode(IMeshSceneNode* newNode)
{
  node= newNode;
  return;
}

IMeshSceneNode* getNode() const
{
  return node;
}

};
I hope I did not make any mistake, did not test it
irrRenderer 1.0
Height2Normal v. 2.1 - convert height maps to normal maps

Step back! I have a void pointer, and I'm not afraid to use it!
Scarabol
Posts: 167
Joined: Sat Jan 03, 2009 5:26 pm
Location: Aachen, Germany

Post by Scarabol »

Hi,

another possibility is to inherit from IMeshSceneNode like:

Special to this is that you do not need setnode() or getnode(), because myNode has the same functions and vars as IMeshSceneNode.

enity wrote: "myMeshSceneNode is a class and has following properties..."
>> he create a complete new class
I wrote: "myNode is a variation of IMeshSceneNode and has following changes"
>> I change an existing class

Both codes are correct just decide on your own.

Code: Select all

class myNode : public IMeshSceneNode
{
public:
myNode();
~myNode();

int getstatus(void);

private:
int _status;
}
MfG
Scarabol
Tranen
Posts: 34
Joined: Mon May 05, 2008 5:43 pm

Post by Tranen »

Thanks to all, I need something like the second solution, because in this way I can attach the node to the scenegraph and an animator to the node. The animator then reads the status of the node (using a cast in the animateNode function).

The problem is that I get those errors related to abstract funcion (I guess):

Code: Select all

main.cpp: In function `int createScene(irr::scene::ISceneManager*, irr::gui::IGUIEnvironment*)':
main.cpp:312: error: invalid return type for function `CPin testsc()'
main.cpp:312: error:   because the following virtual functions are abstract:
include/ISceneNode.h:138: error:  virtual void irr::scene::ISceneNode::render()
include/ISceneNode.h:173: error:  virtual const irr::core::aabbox3d<irr::f32>& irr::scene::ISceneNode::getBoundingBox() const
include/IMeshSceneNode.h:34: error:  virtual void irr::scene::IMeshSceneNode::setMesh(irr::scene::IMesh*)
include/IMeshSceneNode.h:38: error:  virtual irr::scene::IMesh* irr::scene::IMeshSceneNode::getMesh()
include/IMeshSceneNode.h:44: error:  virtual void irr::scene::IMeshSceneNode::setReadOnlyMaterials(bool)
include/IMeshSceneNode.h:49: error:  virtual bool irr::scene::IMeshSceneNode::isReadOnlyMaterials() const
Here is my class:

Code: Select all

#include <irrlicht.h>
using namespace irr;


class CPin : public irr::scene::IMeshSceneNode 
{

public:
CPin();
~CPin();

int getstatus(void);

private:
 int _status; 
}; 
In main.cpp I simply call CPin testsc();

Can you help me?

Thanks
randomMesh
Posts: 1186
Joined: Fri Dec 29, 2006 12:04 am

Post by randomMesh »

Tranen wrote:The problem is that I get those errors related to abstract funcion (I guess)
Yes, you need to override these since they are pure virtual.
If you don't know how inhertitance works, i suggest you go with composition rather than inheritance.

Composition would be more suitable in this case anyway, there is no point in having another type of IMeshSceneNode, just to have a status variable.
"Whoops..."
hybrid
Admin
Posts: 14143
Joined: Wed Apr 19, 2006 9:20 pm
Location: Oldenburg(Oldb), Germany
Contact:

Post by hybrid »

Yep, otherwise you'd have to include the new node into the engine code, which is definitely not necessary. Moreover, a proper MVC design will be happy about a clear separation of View (the scene node) and Model/Control.
Tranen
Posts: 34
Joined: Mon May 05, 2008 5:43 pm

Post by Tranen »

>there is no point in having another type of IMeshSceneNode, just to have a status variable

I need to subclass the meshscenenode class for this reason:
I need to create an animator that will do different animation (it changes position and or cololr) based on the status of this object, so I need to know the status of the object in the animateNode of the animator. The same animator instance is shared between more nodes I can get position/color from the node but nothing else.
This is also the reason why I cannot use composition.

Thanks
greenya
Posts: 1012
Joined: Sun Jan 21, 2007 1:46 pm
Location: Ukraine
Contact:

Post by greenya »

That sounds like feature request, but would be cool to have very small addition to ISceneNode and IGUIElement interfaces:

Code: Select all

public: void* UserData = 0;
that is it.

Engine will not touch/check/use this value (no allocation and no destroy). UserData is user's ability to pin own data to certain node. User may define own datastructure and save pointer to it into UserData.

P.S.: this is kind of technique uses WinForms (.NET): each Control has Tag which has Object type (sort of void* in C++). A very useful thing.
Post Reply