0

I have an issue with one of my classes. It has many bools in it, and the functions in said class have to handle many different paths based on these values. All of these paths make the code very bloated and hard to read. I was wondering if there was a design pattern or some principle rule I could follow to made the code run the same but easier to understand and read.

To be more specific, the class is called Actor. It is for a video game that I'm helping with. The Actor class manages the hands of a humanoid in the game. The hands work together in some actions and operate independently in others. So there are a massive amount of Boolean variables to check what each of them are doing. This is all in the update loop (somewhere) and I think this functionality should be moved or simplified somehow. I think there are about 20 different bool values, with more on the way.

Also, I know my post is game dev related, but I feel the question is a general one.

So are there any ways to fix / help this?

Edit:

I'll explain more. Say you have 4 bools: isAttacking, isDefending, isCasting, isDrugged.

So you have to check each hand if it is busy and handle the the paths like: Player tries to attack with a two handed weapon ->

if not isDefending and not isCasting then isAttacking = true, if isDrugged then attack with value 1 else 5.

It just gets complicated quickly and when you factor in more bools it gets very complex. So what I'm saying is: I'm lazy is there a better way to do this?

Megabytte
  • 1
  • 2
  • Why no `std::array` or `std::tuple`? – NaCl Nov 06 '14 at 17:13
  • That would help the declarations in the header, but not the multiple code paths issue. – Megabytte Nov 06 '14 at 17:16
  • 1
    Maybe you could make use of the Strategy Pattern (http://www.oodesign.com/strategy-pattern.html). – king_nak Nov 06 '14 at 17:16
  • Have a look at taking a data-driven approach( http://stackoverflow.com/questions/1065584/what-is-data-driven-programming) or a Finite State Machine (where each bool governs a state transition)? – Necrolis Nov 06 '14 at 17:18
  • replace bool with floating point values, that fades in and out, and then extrapolate in all the possible actions weighted :P – sp2danny Nov 06 '14 at 17:19
  • You could use some cool custom class, which manages those booleans, where you can give it lambdas, which get executed if those those booleans go true. – NaCl Nov 06 '14 at 17:20
  • In the example, could an actor be attacking AND defending at the same time (for example)? Is it appropriate to use independent bools for all these state variables? – harmic Nov 07 '14 at 08:57

1 Answers1

1

With your example with 4 bool, you have 16 (2**4) possible behaviors:

so you may build an array of behaviors and then dispatch it, something like:

void UpdateTick()
{
    const std::uint32_t behaviorIndex =
        (isAttacking << 0)
        | (isDefending << 1)
        | (isCasting << 2)
        | (isDrugged << 3);

    const std::function<void(void)> behaviors[16] = {
        f_NoDrugNoCastNoDefNoAtt,
        f_NoDrugNoCastNoDefAtt,
        f_NoDrugNoCastDefNoAtt,
        f_NoDrugNoCastDefAtt,
        f_NoDrugCastNoDefNoAtt,
        f_NoDrugCastNoDefAtt,
        f_NoDrugCastDefNoAtt,
        f_NoDrugCastDefAtt,
        f_DrugNoCastNoDefNoAtt,
        f_DrugNoCastNoDefAtt,
        f_DrugNoCastDefNoAtt,
        f_DrugNoCastDefAtt,
        f_DrugCastNoDefNoAtt,
        f_DrugCastNoDefAtt,
        f_DrugCastDefNoAtt,
        f_DrugCastDefAtt,
    }
    behaviors[behaviorIndex]();
}
Jarod42
  • 203,559
  • 14
  • 181
  • 302