<p>See the <aclass="reference internal"href="../api/evennia.contrib.game_systems.crafting.example_recipes.html#evennia-contrib-game-systems-crafting-example-recipes"><spanclass="std std-ref">sword example</span></a> for an example
of how to design a recipe tree for crafting a sword from base elements.</p>
</section>
<sectionid="intallation-and-usage">
<h2>Intallation and Usage<aclass="headerlink"href="#intallation-and-usage"title="Permalink to this headline">¶</a></h2>
<p>Import the <codeclass="docutils literal notranslate"><spanclass="pre">CmdCraft</span></code> command from evennia/contrib/crafting/crafting.py and
add it to your Character cmdset. Reload and the <codeclass="docutils literal notranslate"><spanclass="pre">craft</span></code> command will be
<p>Here, <codeclass="docutils literal notranslate"><spanclass="pre">caller</span></code> is the one doing the crafting and <codeclass="docutils literal notranslate"><spanclass="pre">*inputs</span></code> is any combination of
consumables and/or tool Objects. The system will identify which is which by the
<aclass="reference internal"href="../Components/Tags.html"><spanclass="doc std std-doc">Tags</span></a> on them (see below) The <codeclass="docutils literal notranslate"><spanclass="pre">result</span></code> is always a list.</p>
<p>To use crafting you need recipes. Add a new variable to
<p>All top-level classes in these modules (whose name does not start with <codeclass="docutils literal notranslate"><spanclass="pre">_</span></code>) will
be parsed by Evennia as recipes to make available to the crafting system. Using
the above example, create <codeclass="docutils literal notranslate"><spanclass="pre">mygame/world/recipes.py</span></code> and add your recipies in
there:</p>
<p>A quick example (read on for more details):</p>
<spanclass="sd"> Bread is good for making sandwitches!</span>
<spanclass="sd">"""</span>
<spanclass="n">name</span><spanclass="o">=</span><spanclass="s2">"bread"</span><spanclass="c1"># used to identify this recipe in 'craft' command</span>
<spanclass="p">{</span><spanclass="s2">"key"</span><spanclass="p">:</span><spanclass="s2">"Loaf of Bread"</span><spanclass="p">,</span>
<spanclass="s2">"desc"</span><spanclass="p">:</span><spanclass="s2">"A nice load of bread."</span><spanclass="p">,</span>
<spanclass="s2">"typeclass"</span><spanclass="p">:</span><spanclass="s2">"typeclasses.objects.Food"</span><spanclass="p">,</span><spanclass="c1"># assuming this exists</span>
<spanclass="s2">"tags"</span><spanclass="p">:</span><spanclass="p">[(</span><spanclass="s2">"bread"</span><spanclass="p">,</span><spanclass="s2">"crafting_material"</span><spanclass="p">)]</span><spanclass="c1"># this makes it usable in other recipes ...</span>
<codeclass="docutils literal notranslate"><spanclass="pre">evennia.contrib.crafting.crafting.CraftingRecipe</span></code>. This class implements the
most common form of crafting - that using in-game objects. Each recipe is a
separate class which gets initialized with the consumables/tools you provide.</p>
<p>For the <codeclass="docutils literal notranslate"><spanclass="pre">craft</span></code> command to find your custom recipes, you need to tell Evennia
where they are. Add a new line to your <codeclass="docutils literal notranslate"><spanclass="pre">mygame/server/conf/settings.py</span></code> file,
with a list to any new modules with recipe classes.</p>
<spanclass="n">name</span><spanclass="o">=</span><spanclass="s2">"wooden puppet"</span><spanclass="c1"># name to refer to this recipe as</span>
<p>This specifies which tags to look for in the inputs. It defines a
<aclass="reference internal"href="../Components/Prototypes.html"><spanclass="doc std std-doc">Prototype</span></a> for the recipe to use to spawn the
result on the fly (a recipe could spawn more than one result if needed).
Instead of specifying the full prototype-dict, you could also just provide a
list of <codeclass="docutils literal notranslate"><spanclass="pre">prototype_key</span></code>s to existing prototypes you have.</p>
<p>After reloading the server, this recipe would now be available to use. To try it
we should create materials and tools to insert into the recipe.</p>
<p>The recipe analyzes inputs, looking for <aclass="reference internal"href="../Components/Tags.html"><spanclass="doc std std-doc">Tags</span></a> with
specific tag-categories. The tag-category used can be set per-recipe using the
(<codeclass="docutils literal notranslate"><spanclass="pre">.consumable_tag_category</span></code> and <codeclass="docutils literal notranslate"><spanclass="pre">.tool_tag_category</span></code> respectively). The defaults
are <codeclass="docutils literal notranslate"><spanclass="pre">crafting_material</span></code> and <codeclass="docutils literal notranslate"><spanclass="pre">crafting_tool</span></code>. For
the puppet we need one object with the <codeclass="docutils literal notranslate"><spanclass="pre">wood</span></code> tag and another with the <codeclass="docutils literal notranslate"><spanclass="pre">knife</span></code>
<spanclass="n">wood</span><spanclass="o">=</span><spanclass="n">create_object</span><spanclass="p">(</span><spanclass="n">key</span><spanclass="o">=</span><spanclass="s2">"Piece of wood"</span><spanclass="p">,</span><spanclass="n">tags</span><spanclass="p">[(</span><spanclass="s2">"wood"</span><spanclass="p">,</span><spanclass="s2">"crafting_material"</span><spanclass="p">)])</span>
<p>In the call to <codeclass="docutils literal notranslate"><spanclass="pre">craft</span></code>, the order of <codeclass="docutils literal notranslate"><spanclass="pre">knife</span></code> and <codeclass="docutils literal notranslate"><spanclass="pre">wood</span></code> doesn’t matter - the
recipe will sort out which is which based on their tags.</p>
<p>For even more control you can override hooks in your own class:</p>
<ulclass="simple">
<li><p><codeclass="docutils literal notranslate"><spanclass="pre">pre_craft</span></code> - this should handle input validation and store its data in <codeclass="docutils literal notranslate"><spanclass="pre">.validated_consumables</span></code> and
<codeclass="docutils literal notranslate"><spanclass="pre">validated_tools</span></code> respectively. On error, this reports the error to the crafter and raises the
<li><p><codeclass="docutils literal notranslate"><spanclass="pre">craft</span></code> - this will only be called if <codeclass="docutils literal notranslate"><spanclass="pre">pre_craft</span></code> finished without an exception. This should
<li><p><codeclass="docutils literal notranslate"><spanclass="pre">post_craft</span></code> - this receives the result from <codeclass="docutils literal notranslate"><spanclass="pre">craft</span></code> and handles error messages and also deletes
any consumables as needed. It may also modify the result before returning it.</p></li>
<li><p><codeclass="docutils literal notranslate"><spanclass="pre">msg</span></code> - this is a wrapper for <codeclass="docutils literal notranslate"><spanclass="pre">self.crafter.msg</span></code> and should be used to send messages to the
crafter. Centralizing this means you can also easily modify the sending style in one place later.</p></li>
</ul>
<p>The class constructor (and the <codeclass="docutils literal notranslate"><spanclass="pre">craft</span></code> access function) takes optional <codeclass="docutils literal notranslate"><spanclass="pre">**kwargs</span></code>. These are passed
into each crafting hook. These are unused by default but could be used to customize things per-call.</p>
<spanclass="bp">self</span><spanclass="o">.</span><spanclass="n">msg</span><spanclass="p">(</span><spanclass="s2">"You are not good enough to craft this. Better luck next time!"</span><spanclass="p">)</span>
<p>In this example we introduce a <codeclass="docutils literal notranslate"><spanclass="pre">.difficulty</span></code> for the recipe and makes a ‘dice roll’ to see
if we succed. We would of course make this a lot more immersive and detailed in a full game. In
principle you could customize each recipe just the way you want it, but you could also inherit from
a central parent like this to cut down on work.</p>
<p>The <aclass="reference internal"href="../api/evennia.contrib.game_systems.crafting.example_recipes.html#evennia-contrib-game-systems-crafting-example-recipes"><spanclass="std std-ref">sword recipe example module</span></a> also shows an example
<p>If you want to build something even more custom (maybe using different input types of validation logic)
you could also look at the <codeclass="docutils literal notranslate"><spanclass="pre">CraftingRecipe</span></code> parent class <codeclass="docutils literal notranslate"><spanclass="pre">CraftingRecipeBase</span></code>.
It implements just the minimum needed to be a recipe and for big changes you may be better off starting
from this rather than the more opinionated <codeclass="docutils literal notranslate"><spanclass="pre">CraftingRecipe</span></code>.</p>
<p><small>This document page is generated from <codeclass="docutils literal notranslate"><spanclass="pre">evennia/contrib/game_systems/crafting/README.md</span></code>. Changes to this
file will be overwritten, so edit that file rather than this one.</small></p>