Using a methode of a c++ class in an Uhro3D angel script :
1st we create a sample class (.cpp & .h)
SimpleMath.cpp:
#include "SimpleMath.h"
void SimpleMath::testSanRien(){
int i;
i=i+1;
}
//Double the Value !!!
Urho3D::String SimpleMath::DoubleTheValue(int value)
{
char buff[32];
sprintf(buff, "%d", value*2); // int => Char
Urho3D::String ss(buff);
return ss;
}
SimpleMath.h file :
class SimpleMath
{
public:
Urho3D::String DoubleTheValue(int value);
void testSanRien();
private:
int myPrivateValueNotUsedForNow(0);
};
now in you’r main.cpp file :
...
#include <angelscript.h>
#include "SimpleMath.h"
...
void Urho3DPlayer::Start()
{
int r(0);
// Instantiate and register the AngelScript subsystem
context_->RegisterSubsystem(new Script(context_));
asIScriptEngine* engine = GetSubsystem<Script>()->GetScriptEngine();
//Register a global function
engine->RegisterGlobalFunction("String doubleTheNumber (int t)", asFUNCTION(doubleTheNumber), asCALL_CDECL);
assert( r >= 0 );
// Registering the type "simplemath"
engine->RegisterObjectType("simplemath", sizeof(SimpleMath), asOBJ_VALUE | asOBJ_POD | asOBJ_APP_CLASS_CAK);
assert( r >= 0 );
// Registering without argument
engine->RegisterObjectMethod("simplemath", "void testSanRien()", asMETHOD(SimpleMath,testSanRien), asCALL_THISCALL);
assert( r >= 0 );
//With arguments
engine->RegisterObjectMethod("simplemath", "String DoubleTheValue(int value)",asMETHODPR(SimpleMath, DoubleTheValue,(int),String), asCALL_THISCALL);
assert( r >= 0 );
...
and lastly in you’r .as scripts :
//Global Function
Print(doubleTheNumber (10));
//With a class
simplemath veryComplexMath;
//Testing the value
Print(veryComplexMath.DoubleTheValue(33));
Now you can see the result in the console “20” and “66”.
hope this had help someone, and i’ve not make alot of mistake