Function pointers?

When all the array headache has been sorted out I’ve been able to go back to work on the actual game. (The game engine, rather.. :) )

Today I’ve expanded a bit on the component way of thinking. I used to be able to create a game object and add transform and sprite components to them, and today I also added an AI component. I also figured I want each component to have an update function that is called each frame.

Each component have an index into an array, so the transform is index 0, sprite is index 1, AI is index 3 etc.. So I figured it would be sweet if I could have a look up table with function pointers for each component. So I tried simply reading a label into a variable, and that seems to work fine. I made this little test:

vmthread MAIN
{
  CALL( ClearLog )
  DATA16 TestVar
  MOVE16_16( TestFunc, TestVar )
  CALL( WriteLog16, 'TestVar: ', TestVar )
  CALL( WriteLog16, 'TestFunc: ', TestFunc )
  CALL( TestFunc )
  CALL( TestVar )
}

subcall TestFunc
{
  CALL( WriteLog, 'From testfunc' )
}

And the log looked like this:

TestVar: 0x00000002
TestFunc: 0x00000002
From testfunc

and after that the APP halted with a VM verification error. I had a quick look in the code and it seemed like only constants could evaluate as labels, but now that I think about it my code wouldn’t have to evaluate as a label. If the label have the value 2 and my variable hold the value 2, and the code looks like this:

ObjectIdToCall  =  *(OBJID*)PrimParPointer();
if ((*VMInstance.pObjList[ObjectIdToCall]).ObjStatus == STOPPED)

So close but no cigar!

Leave a Reply