1
\$\begingroup\$

So, I have two ideas how to do that:

  • Use enum to define current state and check current state value before running certain method group in Update. Code:

    void Update()
    {switch(currentState){
        case 1:
            InputGroup1();
            break;
        case 2:
            InputGroup2();
            break;
        <...>
    }
    

    And so on. I don't think that this is a good idea because of possible performance drops.

  • Same concept about current state, but instead of checking it and running method groups, "head" script will check current state and activate corresponding script, disabling others. For me it looks like best way in terms of performance and usability.

And also, is it better to keep all input checks in one giant script/component and call functions you need from it? Essentially creating centralized Input Manager.

\$\endgroup\$
1
  • 3
    \$\begingroup\$ "possible performance drops" are not a real problem. Actual, measured performance drops are a problem. So: did you try writing and running this code? Did a single switch really turn out to be a substantial drain on your performance? If not, go do that before asking here. Both these options are valid - it's up to you what works better for the style of coding you and your team prefer for the needs of your current project. \$\endgroup\$
    – DMGregory
    Jul 27 '19 at 3:59
1
\$\begingroup\$

You are prematurely optimizing and wasting precious time and brain juice.

Deciding where an if/switch should go when it's called only once or so per frame is irrelevant, most of the time.

Until you have your game quite developed, you should not worry about such optimizations. Then use a profiler to see where the bottlenecks are and fix and optimize actual issues.

Meanwhile, write the code that is the clearest for you and your team. In 6 months from now, you'll be glad you wrote clean and clear code because it will make it much easier to maintain.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.