I was looking for a lightweight scripting system to implement in a 2d space shooter arcade that I'm trying to build in my spare time and after taking a look at lua + derivatives ) and squirrel I came across angel script. The are some reasons I've chosen angel script over squirrel. I love it's syntax and the way registering works, allowing me to register already existent classes whilst not forcing me to write methods aware of the virtual machine state, stack and such.
I did happen to get to a problem though. I don't know if this is possible. What I'm trying to do is to export an already created object's reference into the script. Sample script follows ( sorry for the length )
// c++
class aManager
{
public:
aManager();
~aManager();
void Print();
void SetProperty( int aValue );
aManager GetNew()
{
return aManager();
}
int GetProperty();
int m_Property;
};
aManager::aManager()
{
m_Property = 10;
printf( "constructing new object with pointer %p\n", this );
}
aManager::~aManager()
{
}
void aManager::Print()
{
printf( "%p says : %d\n", this, m_Property );
}
void aManager::SetProperty( int aValue )
{
m_Property = aValue;
}
int aManager::GetProperty()
{
return m_Property;
}
void aManager_Constructor( aManager *obj )
{
new( obj ) aManager();
}
void aManager_Destructor( aManager *obj )
{
obj->~aManager();
}
int main()
{
asIScriptEngine *engine = asCreateScriptEngine( ANGELSCRIPT_VERSION );
engine->RegisterObjectType( "aManager", sizeof( aManager ), asOBJ_CLASS | asOBJ_CLASS_CONSTRUCTOR | asOBJ_CLASS_DESTRUCTOR );
engine->RegisterObjectBehaviour( "aManager", asBEHAVE_CONSTRUCT, "void aManager()", asFUNCTION(aManager_Constructor), asCALL_CDECL_OBJLAST );
engine->RegisterObjectBehaviour( "aManager", asBEHAVE_DESTRUCT, "void aManager()", asFUNCTION(aManager_Destructor), asCALL_CDECL_OBJLAST );
engine->RegisterObjectProperty( "aManager" , "int m_Property", offsetof( aManager, m_Property ) );
engine->RegisterObjectMethod( "aManager", "void SetProperty( int )", asMETHOD(aManager,SetProperty), asCALL_THISCALL );
engine->RegisterObjectMethod( "aManager", "int GetProperty()", asMETHOD(aManager,GetProperty), asCALL_THISCALL );
engine->RegisterObjectMethod( "aManager", "void Print()", asMETHOD(aManager,Print), asCALL_THISCALL );
engine->RegisterObjectMethod( "aManager", "aManager GetNew()", asMETHOD(aManager,GetNew), asCALL_THISCALL );
// load and compile script
char theFilename[] = "test.as";
char *theBuffer;
int theLength;
GetFileContent( theFilename, &theBuffer, &theLength );
engine->AddScriptSection( "module", "section", theBuffer, theLength, 0 );
delete( theBuffer );
engine->Build( "module" );
// create context
asIScriptContext *theContext;
engine->CreateContext( &theContext );
// get function and run it
char theFunctionDeclaration[] = "void main()";
int theFunctionID = engine->GetFunctionIDByDecl( "module", theFunctionDeclaration );
theContext->Prepare( theFunctionID );
theContext->Execute();
// clean up
theContext->Release();
return ( 0 );
}
and here's the script file :
// test.as file
void main()
{
aManager obj1;
obj1.Print();
obj1.SetProperty( obj1.GetProperty() + 10 );
obj1.Print();
aManager obj2 = obj1.GetNew();
}
If we look to "aManager obj2 = obj1.GetNew();" we notice the system creates two objects instead of one. I understand this is because when declaring the type of obj2 "aManager obj2", the constructor is called automatically and I'm forced to do that because it needs to have variables declared. I was wondering though if there's any way of returning and assigning a reference of an object created in c++ to an undeclared script var. or perhaps a workaround.
thank you and keep on the great work !
benishor.