extending obects function pointers?

I am trying to figure out how to use a function pointer (probably not called that in javascript) in an extended class. I have an NPC base class containing all the generic AI for my enemies. It’s a simple Finite State Machine.

Then I have a series of specialized enemies which extend and override the NPC methods.

The problem is that my function pointer only wants to call the base methods - not my shiny new overridden methods. javascript:emoticon(‘:(’)

NPC.js

var state = Idle; // my state machine function pointer

function Idle()
  {
  // do idle things
  Debug.Log("base idle method");  
  }
function Wander()
  {
  // do wandery things 
  }

etc…

guard.js

class guard extends NPC
  {
  function Update()
    {
    state();
    }
  function Idle()
    {
    // guard specific idle stuff
    Debug.Log ("override idle method")
    }

Any ideas? I’ve tried using state(this) in my guard class but I get:
The best overload for the method ‘callable() as void’ is not compatible with the argument list ‘(guard)’.

I guess in the end I could go with a switch statement in every specialized enemy script. Seems repetitive though. The reason I was avoiding it to begin with was because I keep hearing strings are “slow” and there’s no way I’m gonna use integers.

This tests fine. It just irks me that I have to copy/paste/maintain a new FSM for each inherited special enemy.

In guard.js:

function Update()
 {
 switch(stateString)
   {
   case "Idle":Idle();break;
   case "Wander":Wander();break;
   }
 }

If someone knows of a smarter way, I’d appreciate it! Thanks!

Just write ‘virtual’ in front of the relevant functions:

// BaseClass.js

var state = Idle;

function Update()
{
	state();
}

virtual function Idle()
{
	print("BaseClass.Idle");
}
// DerivedClass.js

class DerivedClass extends BaseClass
{
	virtual function Idle()
	{
		print("DerivedClass.Idle");
	}
}

In fact, this is unrelated to the idea of using a ‘state’ variable to call the function. If you were to call Idle directly, it would also work the same way, calling the version of the function that relates to the actual type of the object instead of the type of the reference variable that it’s being called from.

You might sometimes want your derived class version of a function to call the base class version so that you don’t have to write the same code out in both versions. To do this, you can just write super.Idle(); anywhere in the derived class version of the function.

Thanks NCarter! I’ll try the virtual functions.