Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>If it is related to the behavior, and only makes sense in the context of that behavior, then it should be stored as part of it.</p> <p>A movement rate only makes sense for something that can move. Which means it should be stored as part of the object that represents its ability to move, which seems to be your MoveBehavior.</p> <p>If that makes it too hard to access, it sounds more like a problem with your design. Then the question is not "should I cheat, and place some of the variables inside the Mob class instead of the behavior it belongs to", but rather "how do I make it easier to interact with these individual behaviors".</p> <p>I can think of several ways to implement this. The obvious is a simple member function on the Mob class which allows you to select individual behaviors, something like this:</p> <pre><code>class Mob { private List&lt;Behavior&gt; behaviors; public T Get&lt;T&gt;(); // try to find the requested behavior type, and return it if it exists } </code></pre> <p>Others can then do something like this:</p> <pre><code>Mob m; MovementBehavior b = m.Get&lt;MovementBehavior(); if (b != null) { b.SetMovementRate(1.20f); } </code></pre> <p>You might also place some of this outside the Mob class, creating a helper function which modifies the movement rate if it exists, and does nothing otherwise:</p> <pre><code>static class MovementHelper { public static SetMovementRate(Mob m, float movementrate){ MovementBehavior b = m.Get&lt;MovementBehavior(); if (b != null) { b.SetMovementRate(1.20f); } } } </code></pre> <p>and then others could use it like this:</p> <pre><code>MovementHelper.SetMovementRate(m, 1.20f); </code></pre> <p>which would provide easy access to modifying the behavior, but without cluttering up the Mob class. (Of course, it'd be tempting to turn this into an extension method, but that might lead to too much assorted clutter in the Mob class' public interface. It may be preferable to make it clear that this is helper functionality that resides outside the Mob class itself)</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload