2
\$\begingroup\$

I'm writing a small game to sharpen my programming skills, and I've chosen to use behaviors (that basically is a class that modifies its parent, to do things like movement, damage and stuff) to avoid duplicating code... maybe...

Whenever I need to modify the properties of a behavior, I need to get it from the "Actor", like this:

actor.getBehaviour(BehaviourType).method();

It's OK until I need to modify multiple properties, like following:

actor.getBehaviour(BehaviourType).method();
actor.getBehaviour(BehaviourType).setFoo(10);
actor.getBehaviour(BehaviourType).setBar("privet", "russian_hello");

Wouldn't that be more convenient if I just return a reference to the behavior on every method (every method needed)?:

actor.getBehaviour(BehaviourType).method().setFoo(10).setBar("privet", "russian_hello");

I apologize for any mistakes or broken logic (including broken grammar).

Thank you very much and have a nice day/afternoon/night/pokemongo/vim>sublime but i use sublime anyways!

\$\endgroup\$
  • \$\begingroup\$ TBH, I think you'll get your answer only when you'll be done with your project. \$\endgroup\$ – Vaillancourt Jul 19 '16 at 17:16
  • 1
    \$\begingroup\$ I think this is much more a general programming question than anything specific to games. \$\endgroup\$ – Sean Middleditch Jul 19 '16 at 17:30
  • \$\begingroup\$ What exactly are you looking for here? Your only actual question appears to be "would this be more convenient?" which is pretty subjective in nature. Are you asking how you'd make this change, or what the downsides might be? Or are you purely looking for a discussion about the pros and cons of the technique? You might want to ask on GDNet instead. \$\endgroup\$ – Josh Jul 22 '16 at 15:33
1
\$\begingroup\$

You do already return a reference to the behaviour with the getBehaviour method. You can store the reference in a variable and work with it. Personally, I wouldn't return the components from the other methods.

Something like the following if a local variable is enough:

YourBehaviour behaviour = actor.getBehaviour(BehaviourType);
behaviour.method();
behaviour.setFoo(10);
behaviour.setBar("privet", "russian_hello");

I'm not quite sure which language you're using, but it appears that you might benefit from some kind of generics (C#/Java) or templates (C++) to get a reference of the specific behaviour type and call the corresponding methods, so that you won't need to cast it explicitly all the time.

\$\endgroup\$

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