use existing class instance?

Started by
0 comments, last by WitchLord 19 years, 4 months ago
Got another for you... is there a way to have angelscript use a method within an existing class instance for a function call from angelscript, instead of creating a new instance? i.e.: // c++ class MyClass { public: void myMethod() {} }; MyClass myInstance; // angelscript whateverFunction(); ^^^ myInstance.myMethod() gets called here? I'm working on a somewhat unusual project where I absolutely have to use the existing instance.
Advertisement
If the script is calling a global function there is no way AngelScript can make it a class method as it has no knowledge of the object instance. However you have a couple of options for solving your problem.

1. You can register the class method as an object method, and then register the instance as a global property. That way the script would be able to call the method on the globally available object.

// AngelScriptvoid ScriptFunction(){  // obj is a globally available variable registered by the application  obj.Function(12, 34);}



2. You can also write a wrapper function for the class method. The wrapper function would then get the object instance and call the class method:

// C++ wrapper functionint Function(int a, int b){  Obj *obj = GetObjectInstance();  return obj->Function(a, b);}

AngelCode.com - game development and more - Reference DB - game developer references
AngelScript - free scripting library - BMFont - free bitmap font generator - Tower - free puzzle game

This topic is closed to new replies.

Advertisement