Jump to content

[SOLVED] access plugin functions in c++ app or other plugin?


photo

Recommended Posts

hi:

   We currently want to separate a feature to a plugin and still could access that plugin from c++ level instead of script. so could you provide us a sample to access plugin functions across plugins or app?

thanks

 

Link to comment

Hi,

 

You can just include plugin's header and get plugin instance in project's world logic, for example:

plugin header file:

// some extern class declaration:
class MyExternClass {
...
};

// plugin declaration
class MyPlugin : public Plugin {
...
   // api
   MyExternClass *getExternClass();
}

project WorldLogic class:

class AppWorldLogic : public WorldLogic
{
public:
   AppWorldLogic() {}
   virtual ~AppWorldLogic() {}

   virtual int init()
   {
        // assume, that plugin has been loaded
	EnginePtr engine;
	int id = engine->findPlugin("plugin/plugin");
	if (id == -1) {
	        Log::message("can't find plugin\n");
		return 0;
	}

        // get plugin interface
	Plugin *pl = Engine::get()->getPluginInterface(id);
	if (!pl) {
		Log::message("plugin is null\n");
 		return 0;
	}
        
        // cast to our class
	MyPlugin *plugin = static_cast<MyPlugin*>(pl);
	if (!plugin) {
		Log::message("cant cast\n");
		return 0;
	}
	
        // create custom class from plugin
	MyExternClass *extern_class = new MyExternClass();
	extern_class->print("hello from c++");

        // use plugin's api
	MyExternClass *extern_class2 = plugin->getExternClass();
	extern_class2->print("hello from c++ 2");

	return 1;
   }
};

// starting main loop with system and world logic for example
int main(int argc,char **argv) {
   EnginePtr engine(UNIGINE_VERSION,argc,argv);

   AppSystemLogic system_logic;
   AppWorldLogic world_logic;
   engine->main(&system_logic, &world_logic, NULL);

   return 0;
}
Link to comment

thanks Maxi:

    We also would like to call an app function from a plugin directly. is this possible?

example:

    We have our own app to handle the updates and keyboard events. We want to create a plugin which could call the app function to register a keyboard callback event. so the plugin could handle keyboard events too.

  thanks alot

Link to comment

Please check out samples/Api/Systems/Plugins example. You can inherit any of logics classes (WorldLogic, SystemLogic, EditorLogic) in plugin's project, add this logic to engine by calling Engine::add...Logic and make your own logic there implementing init, update, shutdown.

Link to comment
  • 1 month later...

hi Maxi:

    just come back and continue this question:

we have a c++ class MyExternClass in a plugin. it's instanced in unigine script MyexternClass myclass=new MyexternClass() and it's starting to store some data in the instance from script update;

Now i need to access this instance from another plugin to get those data.

is this possible?

thanks

Link to comment

Hi,

 

 

also could you explain how 

this works without implementation?

 

This will work only for c++ when you add plugin's header to your project, implementation is available, when engine loads plugin's dll.

 

 

hi Maxi:

    just come back and continue this question:

we have a c++ class MyExternClass in a plugin. it's instanced in unigine script MyexternClass myclass=new MyexternClass() and it's starting to store some data in the instance from script update;

Now i need to access this instance from another plugin to get those data.

is this possible?

thanks

 

You can create a class in c++ and move it to the script, that will perform some logic with it, for example:

 

// C++

 

//function that return variable with extern class:

Unigine::Variable getExternClass()

{

   // create object or get it from storage whatever

   MyexternClass *myclass = new MeexternClass();

   Unigine::Variable var;

 

   // add your extern class to variable, check out append and manage flags for owning logic  

   var.setExternClassObject(Unigine::Engine::get()->getWorldInterpreter(), "MyexternClass", myclass);

  

   return var;

}

 

// add function to script, from a global or a member function

Unigine::Interpreter::addExternFunction("getExternClass", MakeExternFunction(&getExternClass));

 

// script

MyexternClass myclass = getExternClass();

... do some logic with it

Link to comment
×
×
  • Create New...