Having a problem getting object handles to work. I want to keep a (more or less) global TimerCollection, with which I can create new Timers and retrieve handles to them in the script to manipulate. I've added ADDREF and RELEASE behavior to the Timer class, as well as a CONSTRUCT behavior. I haven't added a destructor for it, as the class doesn't have a destructor anyway.
The TimerCollection keeps a vector of Timers. When you call Make_Timer(), it creates a new Timer with the name you supply. Get_Timer() returns a reference to a Timer in the vector. Here's the code that registers the important stuff:
r = engine->RegisterObjectType("Timer", sizeof(Timer), asOBJ_CLASS); assert (r >= 0);
r = engine->RegisterObjectMethod("Timer", "const string& Get_Name()", asMETHOD(Timer,Get_Name), asCALL_THISCALL); assert (r >= 0);
r = engine->RegisterObjectMethod("Timer", "bool Is_Active()", asMETHOD(Timer,Is_Active), asCALL_THISCALL); assert (r >= 0);
r = engine->RegisterObjectMethod("Timer", "void Set_Active(bool)", asMETHOD(Timer,Set_Active), asCALL_THISCALL); assert (r >= 0);
r = engine->RegisterObjectBehaviour("Timer", asBEHAVE_CONSTRUCT, "void f()", asFUNCTION(Timer_Cons), asCALL_CDECL_OBJLAST); assert (r >= 0);
r = engine->RegisterObjectBehaviour("Timer", asBEHAVE_ADDREF, "void f()", asMETHOD(Timer,AddRef), asCALL_THISCALL); assert (r >= 0);
r = engine->RegisterObjectBehaviour("Timer", asBEHAVE_RELEASE, "void f()", asMETHOD(Timer,Release), asCALL_THISCALL); assert (r >= 0);
r = engine->RegisterObjectType("TimerCollection", sizeof(TimerCollection), asOBJ_CLASS); assert (r >= 0);
r = engine->RegisterObjectMethod("TimerCollection", "void Make_Timer(const string &in)", asMETHOD(TimerCollection,Make_Timer), asCALL_THISCALL); assert (r >= 0);
r = engine->RegisterObjectMethod("TimerCollection", "Timer@ Get_Timer(const string &in)", asMETHOD(TimerCollection,Get_Timer), asCALL_THISCALL); assert (r >= 0);
r = engine->RegisterGlobalProperty("TimerCollection timers", &timers); assert (r >= 0);
Here's the reference counting code:
void Timer::Release()
{
if (--refCount == 0)
delete this;
}
void Timer::AddRef()
{
++refCount;
}
Timer::Timer()
{
startTicks = 0;
pausedTicks = 0;
running = false;
paused = false;
refCount = 1;
}
Here's the code for Get_Timer():
Timer& TimerCollection::Get_Timer(const string &name)
{
// search for it
for (vector<Timer>::iterator i = timers.begin(); i != timers.end(); ++i)
{
if (i->Get_Name().compare(name) == 0)
{
// found it!
return *i;
}
}
// didn't find it
return null_timer;
}
And finally, here's the line of script that throws a debug assertion failure:
Timer @t3 = timers.Get_Timer("t3");