Jump to content

[SOLVED] How to call UnigineScript function from C++ with custom struct as argument?


photo

Recommended Posts

Hi.

 

I've been reading the documentation on class and data export at "Programming/C++/C++ Usage Examples/ *** Export", but this specific case eludes me:

How can I call an UnigineScript function that has a custom struct (Correctly defined and exported) as argument? (Example below)

/////:::::::::::: UNIGINESCRIPT :::::::::::::::::::::::::::::::::::::


void myUnigineFunction(MyClass argument){
  /* Do stuff */
}


/////:::::::::::: CPP :::::::::::::::::::::::::::::::::::::::::::::::

using namespace Unigine;

class MyClass {
public:

  int var1, var2;

  MyClass() {/**/}
  ~MyClass() {/**/}

  static void RegisterMyClass(){
    // Function called on main, before Engine definition and Engine->main call

    ExternClass<MyClass>* exported = MakeExternClass<MyClass>();

    exported->AddConstructor();
    exported->AddFunction("function1", &MyClass::Function1);
    exported->AddFunction("function2", &MyClass::Function2);

    Interpreter::AddExternClass("MyClass", exported);

  }

  void Function1() {/**/}
  void Function2() {/**/}


};

// ---------

void OneGenericFunction() {
  
  MyClass data;
  // data.var1 = something;
  // data.var2 = somethingelse;
  
  /******/
  
  // <-- How should I do this?
  Engine::get()->runWorldFunction(Variable("myUnigineFunction"), Variable(data));

}

 

EDIT: I assume I have to play around with TypeInfo and/or the interpreter, but i had no luck.

 

Any help on the matter will be much appreciated.

Thanks for your time.

Edited by enrique.trilles
Link to comment

Hello! 
You did everything right!

Interpreter *world = (Interpreter*)Engine::get()->getWorldInterpreter();
if (world)
{
	MyClass data(3,2);

	Unigine::Variable v(world, TypeInfo(TypeID<MyClass*>()), &data); // through type faster
//	Unigine::Variable v(world,"MyClass", &data);  // so it works too

	Engine::get()->runWorldFunction(Variable("myUnigineFunction"), v);
}


https://developer.unigine.com/en/docs/2.9/code/cpp/usage/classes#memory_management

Edited by cash-metall
added link to doc
Link to comment
  • morbid changed the title to [SOLVED] How to call UnigineScript function from C++ with custom struct as argument?
×
×
  • Create New...