1
\$\begingroup\$

I have a few string arrays for which I can cycle what the current active element is (of which there can only be one at a time) using the number keys. Something like the item inventory in Half-Life. I want to arrange it so that when I have a particular element selected, and press the select key, I set my weapon state machine to the corresponding weapon. I would do that with something like this:

    void SelectionButton(string _selectionButton)
{
    if (_selectionButton == "a11")
    {
        _weaponStateManager.SwitchState(_weaponStateManager._weaponStateDrag);
    }
    //continue this for each element in each array
}

The problem is that I would need to write an if statement for each element in each array. I am concerned that maybe this is not the most efficient way to do this - maybe looping through the entire list of possible options every time I switch weapons is not the best idea? I thought to maybe use dictionaries, but I was not sure how to relay the state definition for the corresponding element in the array to the state manager.

If using a long string of if statements is the correct way to handle this, then that is OK. I am just concerned that this is inefficient.

\$\endgroup\$
2
  • 1
    \$\begingroup\$ Step 0) Don't overthink it Step 1) get it working. Step 2) get the project done Step 3) improve gameplay Step 4) make code nice. As long as you do not need to write 50 if statements but only a couple of them, go for the fast implementation. Doing a round trip through a small list might be inefficient for us, but you won't even notice a performance drop. \$\endgroup\$
    – Zibelas
    Apr 4, 2023 at 10:13
  • \$\begingroup\$ This doesn't answer the question, but maybe you should look into making each weapon state its own class with a common base class or interface, and get rid of the if else statements entirely. I agree with Zibelas that this is not the time to optimize. \$\endgroup\$
    – Evorlor
    Apr 4, 2023 at 16:36

1 Answer 1

2
\$\begingroup\$

When you compare the same variable against multiple values, then you can do that with a switch/case statement:

switch (_selectionButton) {
    case "a11": 
      _weaponStateManager.SwitchState(_weaponStateManager._weaponStateDrag);
      break;
    case "a12": 
      _weaponStateManager.SwitchState(_weaponStateManager._weaponStateDrog);
      break;
    case "a13": 
      _weaponStateManager.SwitchState(_weaponStateManager._weaponStateDrug);
      break;
}

Is it "efficient"? Well, does it matter? You are talking about code that reacts to a button press. That doesn't even happen every frame. Micro-optimizing code that gets executed infrequently and already has a negligible runtime is usually more effort than it is worth.

But if you really want to optimize this, then you could use a lookup table.

Identify the buttons not by a string but by an int, with those ints being indexes to an array of whatever type _weaponStateDrag is. That way you can directly fetch the correct state for the array index out of that array. Accessing an array entry by index is a very fast operation.

private WeaponState[] weaponStatesByButtonId; // propagate via inspector or via code in Start or Awake

void SelectionButton(int _selectionButtonId)
{
       _weaponStateManager.SwitchState(weaponStatesByButtonId[_selectionButtonId]);
}
\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .