Jump to content

[SOLVED] Using polymorphic classes as plugin in UnigineScript


photo

Recommended Posts

Hi,

 

I currently want to integrate my custom plugin into UnigineScript but having a problem to access functions from my child class. I am using the following pseudo-code

class CustomBaseClass
{


public:

	/*
	returning a pointer of the object for using it in UnigineScript (virtual functions aren't allowed!) */
	ObjectPtr getObject() {return ObjectPtr::Ptr(); };		//returning null pointer in base class

	void showText(Log::message("Show text in base class!"););
};


class CustomDerivedClass : public CustomBaseClass
{
protected:

	ObjectMeshStaticPtr staticObject;

public:


	ObjectPtr getObject(){
		return staticObject->getObject();
	}

	void showText(Log::message("Show text in derived class!"););
};



class MyPlugin
{

public:

	MyPlugin();

	CustomBaseClass* returnCustomDerivedClass()
	{
		CustomDerivedClass* sampleClass = new CustomDerivedClass();

		//do some stuff in derived class like creating a new ObjectMeshStatic....

		return sampleClass;
	};
};

class MyCustomPlugin : public Plugin
{

public:

	virtual int init();
	virtual int shutdown();
	virtual int update();
};

int MyCustomPlugin::init()
{
	//export my plugin and the base class

	ExternClass<MyPlugin>* exportedPlugin = MakeExternClass<MyPlugin>();
	exportedPlugin->addConstructor();

	exportedPlugin->addFunction("returnCustomDerivedClass",&MyPlugin::returnCustomDerivedClass);

	Interpreter::addExternClass("MyPlugin",exportedPlugin);

	ExternClass<CustomBaseClass>* exportedBaseClass = MakeExternClass<CustomBaseClass>();
	exportedBaseClass->addConstructor();
	exportedBaseClass->addFunction("getObject",CustomBaseClass::getObject);
	exportedBaseClass->addFunction("showText",CustomBaseClass::showText);

	Interpreter::addExternClass("CustomBaseClass",exportedBaseClass);

}

The export went successful and now i want to access the classes and functions in UnigineScript:

//UnigineScript
void init(PluginMeta m) 
{
	MyPlugin newPlugin = new MyPlugin();

	CustomBaseClass baseClass = newPlugin.returnCustomDerivedClass();		//returning a derived class with an newly created object

	Object obj = baseClass.getObject();				//returns the object from the base class (which is NULL) but not from derived class
	obj.showText();									//shows "Show text in base class!"



}

During the creation of the application my plugin is loading fine and the Interpreter doesn't complain, that he doesn't now my derived class. So therefor (it seems to me) it only calls the functions from my base class.

In the second run, I also exported my CustomDerivedClass without any problems. Anyway, my baseClass variable in UnigineScript still calls the base class functions even if it must store the derived one. Is this a normal behavior?

Are there any workaround to get the ObjectPtr I created in derived class without casting the base class in UnigineScript to his child one?

 

Many thanks in advance!

Link to comment

Sorry for the inconvenience, but my Visual Studio Compiler messed up building. The post can be marked as solved!

Link to comment
×
×
  • Create New...