Page 1 of 1

Noob question about subclassing a IMeshSceneNode

Posted: Sun Apr 25, 2010 9:47 am
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?

Posted: Sun Apr 25, 2010 10:11 am
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

Posted: Sun Apr 25, 2010 10:26 am
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

Posted: Sun Apr 25, 2010 10:47 am
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

Posted: Sun Apr 25, 2010 1:08 pm
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.

Posted: Sun Apr 25, 2010 2:22 pm
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.

Posted: Sun Apr 25, 2010 3:18 pm
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

Posted: Mon May 31, 2010 11:49 am
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.