<h1>Give objects weight<aclass="headerlink"href="#give-objects-weight"title="Permalink to this headline">¶</a></h1>
<p>All in-game objets you can touch usually has some weight. What weight does varies from game to game. Commonly it limits how much you can carry. A heavy stone may also hurt you more than a ballon, if it falls on you. If you want to get fancy, a pressure plate may only trigger if the one stepping on it is heavy enough.</p>
<spanclass="normal">16</span></pre></div></td><tdclass="code"><div><pre><span></span><spanclass="c1"># inside your mygame/typeclasses/objects.py</span>
<p>Yes, we know weight varies with gravity. ‘Mass’ is more scientifically correct. But ‘mass’ is less commonly used in RPGs, so we stick to ‘weight’ here. Just know if if your sci-fi characters can vacation on the Moon (1/6 gravity of Earth) you should consider using <codeclass="docutils literal notranslate"><spanclass="pre">mass</span></code> everywhere and calculate the current weight on the fly.</p>
</aside>
<ulclass="simple">
<li><p><strong>Line 6</strong>: We use the <codeclass="docutils literal notranslate"><spanclass="pre">ObjectParent</span></code> mixin. Since this mixin is used for <codeclass="docutils literal notranslate"><spanclass="pre">Characters</span></code>, <codeclass="docutils literal notranslate"><spanclass="pre">Exits</span></code> and <codeclass="docutils literal notranslate"><spanclass="pre">Rooms</span></code> as well as for <codeclass="docutils literal notranslate"><spanclass="pre">Object</span></code>, it means all of those will automatically <em>also</em> have weight!</p></li>
<li><p><strong>Line 8</strong>: We use an <aclass="reference internal"href="../Components/Attributes.html#using-attributeproperty"><spanclass="std std-doc">AttributeProperty</span></a> to set up the ‘default’ weight of 1 (whatever that is). Setting <codeclass="docutils literal notranslate"><spanclass="pre">autocreate=False</span></code> means no actual <codeclass="docutils literal notranslate"><spanclass="pre">Attribute</span></code> will be created until the weight is actually changed from the default of 1. See the <codeclass="docutils literal notranslate"><spanclass="pre">AttributeProperty</span></code> documentation for caveats with this.</p></li>
<li><p><strong>Line 10 and 11</strong>: Using the <codeclass="docutils literal notranslate"><spanclass="pre">@property</span></code> decorator on <codeclass="docutils literal notranslate"><spanclass="pre">total_weight</span></code> means that we will be able to call <codeclass="docutils literal notranslate"><spanclass="pre">obj.total_weight</span></code> instead of <codeclass="docutils literal notranslate"><spanclass="pre">obj.total_weight()</span></code> later.</p></li>
<li><p><strong>Line 12</strong>: We sum up all weights from everything “in” this object, by looping over <codeclass="docutils literal notranslate"><spanclass="pre">self.contents</span></code>. Since <em>all</em> objects will have weight now, this should always work!</p></li>
</ul>
<p>Let’s check out the weight of some trusty boxes</p>
<p>Here we make sure to add another <codeclass="docutils literal notranslate"><spanclass="pre">AttributeProperty</span></code> telling us how much to carry. In a real game, this may be based on how strong the Character is. When we consider how much weight we already carry, we should not include <em>our own</em> weight, so we subtract that.</p>
<p>To honor this limit, we’ll need to override the default <codeclass="docutils literal notranslate"><spanclass="pre">get</span></code> command.</p>
<p>In this example, we implement the beginning of the <codeclass="docutils literal notranslate"><spanclass="pre">CmdGet</span></code> and then call the full <codeclass="docutils literal notranslate"><spanclass="pre">CmdGet()</span></code> at the end. This is not very efficient, because the parent <codeclass="docutils literal notranslate"><spanclass="pre">CmdGet</span></code> will again have to do the <codeclass="docutils literal notranslate"><spanclass="pre">caller.search()</span></code> again. To be more efficient, you will likely want to copy the entirety of the <codeclass="docutils literal notranslate"><spanclass="pre">CmdGet</span></code> code into your own version and modify it.</p>
</aside>
<divclass="highlight-python notranslate"><divclass="highlight"><pre><span></span><spanclass="c1"># in mygame/commands/command.py </span>
<spanclass="n">caller</span><spanclass="o">.</span><spanclass="n">msg</span><spanclass="p">(</span><spanclass="s2">"You can't carry that much!"</span><spanclass="p">)</span>
<p>Here we add an extra check for the weight of the thing we are trying to pick up, then we call the normal <codeclass="docutils literal notranslate"><spanclass="pre">CmdGet</span></code> with <codeclass="docutils literal notranslate"><spanclass="pre">super().func()</span></code>.</p>