I have two questions that are related to each other.
Question 1:
Is there a delete operator in AngelScript?
ie:
Sound@ sound = LoadSound("bla1.wav");
// Do something, ie loading level data
delete sound;
@sound = LoadSound("bla2.wav");
// finished
delete sound;
Question 2:
I'm currently using AngelScript to setup and load a level.
The objects are created in AngelScript and the pointers are passed to the Level class. Both Entity and Level are registered with AngelScipt as a reference type.
The problem is that when the script finishes the pointers are deleted by the Release behavior.
But i don't want the objects to be deleted (yet) because they are used by Level.
So i circumvented the problem by commenting the delete line. (see below)
void ReferenceType::Release(){ // Decrease ref count and delete if it reaches 0 if( --mRefCount == 0 ) { //delete this; }}
The objects are deleted in the destructor of Level.
// Below is a generic function to register a reference type with AngelScript. The New is used for objects with a default constructor.
template <class T>void New(asIScriptGeneric* gen){ *(T**)gen->GetAddressOfReturnLocation() = new T();}template <class T>void Assign(asIScriptGeneric* gen){ T* a = (T*)gen->GetArgAddress(0); T* thisPointer = (T*)gen->GetObject(); *thisPointer = *a; gen->SetReturnAddress(thisPointer);}template <class T>void RegisterReferenceType(asIScriptEngine* engine, const char* type){ int r; // Register the reference type r = engine->RegisterObjectType (type, 0, asOBJ_REF); assert( r >= 0 ); // register reference type behaviours r = engine->RegisterObjectBehaviour(type, asBEHAVE_ADDREF, "void f()", asMETHOD(T,AddReference), asCALL_THISCALL); assert( r >= 0 ); r = engine->RegisterObjectBehaviour(type, asBEHAVE_RELEASE, "void f()", asMETHOD(T,Release), asCALL_THISCALL); assert( r >= 0 );}
// Below is a generic function to register the Entity type with AngelScript
template <class T>void RegisterEntity(asIScriptEngine* engine, const char* type){ int r; // Register the reference type RegisterReferenceType<T>(engine, type); char declaration[512]; sprintf(declaration, "%s@ f()", type); // Constructor r = engine->RegisterObjectBehaviour(type, asBEHAVE_FACTORY, declaration, asFUNCTION(NewEntity<T>), asCALL_GENERIC); assert( r >= 0 ); // Register = operator sprintf(declaration , "%s& opAssign(const %s& in)", type, type); r = engine->RegisterObjectMethod(type, declaration, asFUNCTION(Assign<T>), asCALL_GENERIC); assert( r >= 0 );}
I call: RegisterEntity<Entity>(scriptEngine, "Entity");
to register the Entity class. Level is registered in the same way.