Code:
template<typename T>
class CUIButton : public CUIObject
{
public:
CUIButton ()
{
CUITask::AddToList( this );
}
void Activate(int index=0)
{
// RUN whatever the button does
if ( !Object || !Function ) // prevent calling a null
return;
if ( Animation.GetCurrentFrame() == 0 )
Animation.SetCurrentFrame(1);
else
Animation.SetCurrentFrame(0);
//(Object->*Function)();
}
void ChangeFunctionPointer( T * NewObject, void (T::*NewFunction)(void) )
{
Function = NewFunction;
}
void ( T::*Function )( void );
T * Object;
}
See the Animation.Get / Set lines?
The (Object->*Function)() causes it to crash if I run those lines in it. This seems to be that Object isn't copying properly, which leads me to show you this:
Code:
class CTestTask : public ITask
{
public:
AUTO_SIZE;
bool Start();
void Update();
void Stop();
void Test();
private:
CSprite MySprite;
CUIButton<CTestTask> MyButton;
};
Code:
bool CTestTask::Start()
{
MyButton.ChangeFunctionPointer( this, Test );
return true;
}
void CTestTask::Test()
{
if ( MyButton.Animation.GetCurrentFrame() )
MyButton.Animation.SetCurrentFrame(0);
else
MyButton.Animation.SetCurrentFrame(1);
}
Why does the function pointer crash it?
Edit:
Code:
void CTestTask::Test()
{
if ( CUITask::Objects[0]->Animation.GetCurrentFrame() )
CUITask::Objects[0]->Animation.SetCurrentFrame(0);
else
CUITask::Objects[0]->Animation.SetCurrentFrame(1);
}
If I do that, then it works. Problem? I'd have to also store the index. But I don't think I need that, do I?