- Home /
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. http://sicklebrick.com/?p=411
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
Answer by rutter · May 13, 2013 at 10:00 PM
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.
@Rutter Totally agreed. @sportsfan78 presu$$anonymous$$g that you are using a real delegate, not invoking a $$anonymous$$ethodInfo, then its nearly as fast as a native method call, and nothing to be afraid of.
The delegate method works fine as far as I can tell. I was planning on using it regularly when I came across that article, and I didn't want to set myself up for a performance nightmare just because I neglected to ask someone with experience.
That article references reflection and delegation in the same sentence - that sounds alarm bells with me. Reflection is slow, but fine for configuration and setup. Delegation (by using delegates not invokation) is as fast as it gets for dynamic program$$anonymous$$g.
Your answer
Follow this Question
Related Questions
Best way to check object selection 0 Answers
How can I check if I pressed F in the class other than Update()? C# 2 Answers
Update not behaving like it should with enum states 1 Answer
Using else if 4 Answers