Jump to content

[SOLVED] User Object Node type


photo

Recommended Posts

Hi,

 

Is there a way to create a user type which is derived from one of the Unigine Node types (Node, Object, etc) and use the user type in an editor? For example,

 


class MyObject : Object
{
};

...

MyObject x = new MyObject();
engine.editor.addNode(x);

 

 

Currently, it throws an error:

 

Variable::getExternClassObject(): can't convert user class to class Node *

Link to comment

Hi,

 

You can 'inherit' from Nodes, Objects, etc.

 

Technically, it's not inheritance (but it's using same syntax). You can't pass user classes (classes created in UnigineScript) to the editor, but you can pass external classes such as Nodes or Objects.

 

By 'inheriting' from external class, Unigine script will add each method and field of 'parent' external class to your 'child' user class. Also, it'll add 'extern' field which contain reference to the external class, so you can use it to pass it to the editor.

 

Also, you should take care about memory management and if you link any Node with editor then you should unlink it with world. For additional info see here: https://developer.un...mory_management

 

Here, I prepared code for you, hope it'll help you:

 

#include <unigine.h>

/*
*/
Node add_editor(Node node) {
 engine.editor.addNode(node);
 return node_remove(node);
}

void release_editor(Node node) {
 engine.editor.releaseNode(node);
 return node_append(node);
}

class MyObject : Object {
 private:
   int is_editor_node;

 public:

   MyObject() {
     is_editor_node = false;
     extern = new ObjectDummy();
   }

   ~MyObject() {
     if(!is_editor_node) {
       delete extern;
     }
   }

   void setEditorNode(int value) {
     if(is_editor_node == value) return;

     is_editor_node = value;

     if(value) {
       extern = add_editor(extern);
     }
     else {
       extern = release_editor(extern);
     }
   }
};

int init() {
 MyObject x = new MyObject();
 x.setEditorNode(true);
 return 1;
}

int shutdown() {
 return 1;
}

int update() {
 return 1;
}

  • Like 2
Link to comment
×
×
  • Create New...