[SOLVED] Unigine Script Callbacks from external C++ script


photo

Recommended Posts

Hi, I've managed to make a plugin that is loaded successfully by the game. Right now, I'm trying to call a function in the world script via the plugin. I can't figure out how to make the sample in the documentation work. I tried inserting the provided code into my plugin script, but it does not seem to load correctly. I suppose it's because main(int argc,char **argv) is not called. I would have attempted to call aforementioned function in the definition of my plugin init but I do not know what parameters to provide. Any help on the matter would be appreciated.

Link to post

I got it to work already. Here is my code:

 

#include <Unigine.h>

#include <UniginePlugin.h>

#include <UnigineInterpreter.h>

using namespace Unigine;

 

void TestFunction() {

Log::message("This is a test function.\n");

}

 

const Variable &runWorld(const char *name,const Variable &v) {

Log::warning("runworld(%s,%s): called\n",name,v.getTypeName());

Engine *engine = Engine::get();

return engine->runWorld(name,v);

}

 

class CallbackPlugin : public Plugin {

public:

CallbackPlugin() { }

virtual ~CallbackPlugin() { }

virtual int init();

virtual int shutdown();

virtual void update();

};

 

int CallbackPlugin::init()

{

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

char *string = "this is a string";

char **stringPointer = &string;

return 1;

}

 

int CallbackPlugin::shutdown() {

return 1;

}

 

void CallbackPlugin::update() {

Engine *engine = Engine::get();

Variable ret = engine->runWorld("counter");

if(ret.getInt() != -1) {

Log::message("counter is: %d\n",ret.getInt());

}

}

 

extern "C" UNIGINE_API void *CreatePlugin() { return new CallbackPlugin(); }

extern "C" UNIGINE_API void ReleasePlugin(void *plugin) { delete static_cast<CallbackPlugin*>(plugin); }

 

My mistake was in assuming that the code provided in the documentation should be used as is. Thanks.

Link to post
  • 1 month later...