Sign in to follow this  
Zich

use existing class instance?

Recommended Posts

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.

Share this post


Link to post
Share on other sites
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.


// AngelScript
void 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 function
int Function(int a, int b)
{
Obj *obj = GetObjectInstance();
return obj->Function(a, b);
}

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this