Best Way To Script State-Specific Behavior

Hi all. I’m working on a RTS-ish game. I want units to Update differently according to whether they are moving around, performing an ability or idle.

I thought I was being super clever by just turning Update into a wrapper function that called a delegate. I could then change the delegate function to update the unit in a state appropriate way. I then read an article that said delegates can be slow. sicklebrick.com is almost here!

So is there a method that is commonly acknowledged as better for handling state specific actions? Should I attach state components and enable/disable them as necessary? Should I just make a big if else wrapper in my Update function that calls state specific update functions?

Cheers

Honestly, your delegate method sounds fine to me. Is it running okay?

The article you mentioned is probably more concerned with very heavy use of reflection and delegation. It does eventually get to be a problem, but sometimes it’s the right tool for the job.

In terms of other options, the two you mentioned could work in a pinch. I’m not sure that they’d run any significantly faster or slower, though. Other things going on are probably hugely more expensive in CPU time.