<h1><spanclass="section-number">10. </span>Creating things<aclass="headerlink"href="#creating-things"title="Permalink to this headline">¶</a></h1>
<p>We have already created some things - dragons for example. There are many different things to create in Evennia though. In the <aclass="reference internal"href="Beginner-Tutorial-Learning-Typeclasses.html"><spanclass="doc std std-doc">Typeclasses tutorial</span></a>, we noted that there are 7 default Typeclasses coming with Evennia out of the box:</p>
<p>This has the drawback of being two operations; you must also import the class and have to pass
the actual database field names, such as <codeclass="docutils literal notranslate"><spanclass="pre">db_key</span></code> instead of <codeclass="docutils literal notranslate"><spanclass="pre">key</span></code> as keyword arguments. This is closest to how a ‘normal’ Python class works, but is not recommended.</p>
</li>
<li><p>Secondly you can use the Evennia creation helpers:</p>
<p>This is the recommended way if you are trying to create things in Python. The first argument can either be the class <em>or</em> the python-path to the typeclass, like <codeclass="docutils literal notranslate"><spanclass="pre">"path.to.SomeTypeClass"</span></code>. It can also be <codeclass="docutils literal notranslate"><spanclass="pre">None</span></code> in which case the Evennia default will be used. While all the creation methods
are available on <codeclass="docutils literal notranslate"><spanclass="pre">evennia</span></code>, they are actually implemented in <aclass="reference internal"href="../../../api/evennia.utils.create.html"><spanclass="doc std std-doc">evennia/utils/create.py</span></a>. Each of the different base classes have their own creation function, like <codeclass="docutils literal notranslate"><spanclass="pre">create_account</span></code> and <codeclass="docutils literal notranslate"><spanclass="pre">create_script</span></code> etc.</p>
</li>
<li><p>Thirdly, you can use the <codeclass="docutils literal notranslate"><spanclass="pre">.create</span></code> method on the Typeclass itself:</p>
<p>Since <codeclass="docutils literal notranslate"><spanclass="pre">.create</span></code> is a method on the typeclass, this form is useful if you want to customize how the creation process works for your custom typeclasses. Note that it returns <em>two</em> values - the <codeclass="docutils literal notranslate"><spanclass="pre">obj</span></code> is either the new object or <codeclass="docutils literal notranslate"><spanclass="pre">None</span></code>, in which case <codeclass="docutils literal notranslate"><spanclass="pre">err</span></code> should be a list of error-strings detailing what went wrong.</p>
</li>
<li><p>Finally, you can create objects using an in-game command, such as</p>
<p>As a developer you are usually best off using the other methods, but a command is usually the only way to let regular players or builders without Python-access help build the game world.</p>
</li>
</ul>
<sectionid="creating-objects">
<h2><spanclass="section-number">10.1. </span>Creating Objects<aclass="headerlink"href="#creating-objects"title="Permalink to this headline">¶</a></h2>
<p>An <aclass="reference internal"href="../../../Components/Objects.html"><spanclass="doc std std-doc">Object</span></a> is one of the most common creation-types. These are entities that inherits from <codeclass="docutils literal notranslate"><spanclass="pre">DefaultObject</span></code> at any distance. They have an existence in the game world and includes rooms, characters, exits, weapons, flower pots and castles.</p>
> rose = evennia.create_object(key="rose")
</pre></div>
</div>
<p>Since we didn’t specify the <codeclass="docutils literal notranslate"><spanclass="pre">typeclass</span></code> as the first argument, the default given by <codeclass="docutils literal notranslate"><spanclass="pre">settings.BASE_OBJECT_TYPECLASS</span></code> (<codeclass="docutils literal notranslate"><spanclass="pre">typeclasses.objects.Object</span></code> out of the box) will be used.</p>
<p>The <codeclass="docutils literal notranslate"><spanclass="pre">create_object</span></code> has <aclass="reference internal"href="../../../api/evennia.utils.create.html#evennia.utils.create.create_object"title="evennia.utils.create.create_object"><spanclass="xref myst py py-func">a lot of options</span></a>. A more detailed example in code:</p>
<p>Here we set the location of a weapon as well as gave it an <aclass="reference internal"href="../../../Components/Attributes.html"><spanclass="doc std std-doc">Attribute</span></a><codeclass="docutils literal notranslate"><spanclass="pre">desc</span></code>, which is what the <codeclass="docutils literal notranslate"><spanclass="pre">look</span></code> command will use when looking this and other things.</p>
</section>
<sectionid="creating-rooms-characters-and-exits">
<h2><spanclass="section-number">10.2. </span>Creating Rooms, Characters and Exits<aclass="headerlink"href="#creating-rooms-characters-and-exits"title="Permalink to this headline">¶</a></h2>
<p><codeclass="docutils literal notranslate"><spanclass="pre">Characters</span></code>, <codeclass="docutils literal notranslate"><spanclass="pre">Rooms</span></code> and <codeclass="docutils literal notranslate"><spanclass="pre">Exits</span></code> are all subclasses of <codeclass="docutils literal notranslate"><spanclass="pre">DefaultObject</span></code>. So there is for example no separate <codeclass="docutils literal notranslate"><spanclass="pre">create_character</span></code>, you just create characters with <codeclass="docutils literal notranslate"><spanclass="pre">create_object</span></code> pointing to the <codeclass="docutils literal notranslate"><spanclass="pre">Character</span></code> typeclass.</p>
<sectionid="linking-exits-and-rooms-in-code">
<h3><spanclass="section-number">10.2.1. </span>Linking Exits and Rooms in code<aclass="headerlink"href="#linking-exits-and-rooms-in-code"title="Permalink to this headline">¶</a></h3>
<p>An <codeclass="docutils literal notranslate"><spanclass="pre">Exit</span></code> is a one-way link between rooms. For example, <codeclass="docutils literal notranslate"><spanclass="pre">east</span></code> could be an <codeclass="docutils literal notranslate"><spanclass="pre">Exit</span></code> between the <codeclass="docutils literal notranslate"><spanclass="pre">Forest</span></code> room and the <codeclass="docutils literal notranslate"><spanclass="pre">Meadow</span></code> room.</p>
<divclass="highlight-none notranslate"><divclass="highlight"><pre><span></span>Meadow -> east -> Forest
</pre></div>
</div>
<p>The <codeclass="docutils literal notranslate"><spanclass="pre">east</span></code> exit has a <codeclass="docutils literal notranslate"><spanclass="pre">key</span></code> of <codeclass="docutils literal notranslate"><spanclass="pre">east</span></code>, a <codeclass="docutils literal notranslate"><spanclass="pre">location</span></code> of <codeclass="docutils literal notranslate"><spanclass="pre">Meadow</span></code> and a <codeclass="docutils literal notranslate"><spanclass="pre">destination</span></code> of <codeclass="docutils literal notranslate"><spanclass="pre">Forest</span></code>. If you wanted to be able to go back from Forest to Meadow, you’d need to create a new <codeclass="docutils literal notranslate"><spanclass="pre">Exit</span></code>, say, <codeclass="docutils literal notranslate"><spanclass="pre">west</span></code>, where <codeclass="docutils literal notranslate"><spanclass="pre">location</span></code> is <codeclass="docutils literal notranslate"><spanclass="pre">Forest</span></code> and <codeclass="docutils literal notranslate"><spanclass="pre">destination</span></code> is <codeclass="docutils literal notranslate"><spanclass="pre">Meadow</span></code>.</p>
<divclass="highlight-none notranslate"><divclass="highlight"><pre><span></span>Meadow -> east -> Forest
Forest -> west -> Meadow
</pre></div>
</div>
<p>In-game you do this with <codeclass="docutils literal notranslate"><spanclass="pre">tunnel</span></code> and <codeclass="docutils literal notranslate"><spanclass="pre">dig</span></code> commands, bit if you want to ever set up these links in code, you can do it like this:</p>
<h2><spanclass="section-number">10.3. </span>Creating Accounts<aclass="headerlink"href="#creating-accounts"title="Permalink to this headline">¶</a></h2>
<p>An <aclass="reference internal"href="../../../Components/Accounts.html"><spanclass="doc std std-doc">Account</span></a> is an out-of-character (OOC) entity, with no existence in the game world.
You can find the parent class for Accounts in <codeclass="docutils literal notranslate"><spanclass="pre">typeclasses/accounts.py</span></code>.</p>
<p>Normally, you want to create the Account when a user authenticates. By default, this happens in the <codeclass="docutils literal notranslate"><spanclass="pre">create</span><spanclass="pre">account</span></code> and <codeclass="docutils literal notranslate"><spanclass="pre">login</span></code> default commands in the <codeclass="docutils literal notranslate"><spanclass="pre">UnloggedInCmdSet</span></code>. This means that customizing this just means replacing those commands!</p>
<p>So normally you’d modify those commands rather than make something from scratch. But here’s the principle:</p>
<p>The inputs are usually taken from the player via the command. The <codeclass="docutils literal notranslate"><spanclass="pre">email</span></code> must be given, but can be <codeclass="docutils literal notranslate"><spanclass="pre">None</span></code> if you are not using it. The <codeclass="docutils literal notranslate"><spanclass="pre">accountname</span></code> must be globally unique on the server. The <codeclass="docutils literal notranslate"><spanclass="pre">password</span></code> is stored encrypted in the database. If <codeclass="docutils literal notranslate"><spanclass="pre">typeclass</span></code> is not given, the <codeclass="docutils literal notranslate"><spanclass="pre">settings.BASE_ACCOUNT_TYPECLASS</span></code> will be used (<codeclass="docutils literal notranslate"><spanclass="pre">typeclasses.accounts.Account</span></code>).</p>
</section>
<sectionid="creating-channels">
<h2><spanclass="section-number">10.4. </span>Creating Channels<aclass="headerlink"href="#creating-channels"title="Permalink to this headline">¶</a></h2>
<p>A <aclass="reference internal"href="../../../Components/Channels.html"><spanclass="doc std std-doc">Channel</span></a> acts like a switchboard for sending in-game messages between users; like an IRC- or discord channel but inside the game.</p>
<p>Users interact with channels via the <codeclass="docutils literal notranslate"><spanclass="pre">channel</span></code> command:</p>
<p>If a channel named, say, <codeclass="docutils literal notranslate"><spanclass="pre">myguild</span></code> exists, a user can send a message to it just by writing the channel name:</p>
<divclass="highlight-none notranslate"><divclass="highlight"><pre><span></span>> myguild Hello! I have some questions ...
</pre></div>
</div>
<p>Creating channels follows a familiar syntax:</p>
<p>Channels can also be auto-created by the server by setting the <codeclass="docutils literal notranslate"><spanclass="pre">DEFAULT_CHANNELS</span></code> setting. See <aclass="reference internal"href="../../../Components/Channels.html"><spanclass="doc std std-doc">Channels documentation</span></a> for details.</p>
</section>
<sectionid="creating-scripts">
<h2><spanclass="section-number">10.5. </span>Creating Scripts<aclass="headerlink"href="#creating-scripts"title="Permalink to this headline">¶</a></h2>
<p>A <aclass="reference internal"href="../../../Components/Scripts.html"><spanclass="doc std std-doc">Script</span></a> is an entity that has no in-game location. It can be used to store arbitrary data and is often used for game systems that need persistent storage but which you can’t ‘look’ at in-game. Examples are economic systems, weather and combat handlers.</p>
<p>Scripts are multi-use and depending on what they do, a given script can either be ‘global’ or be attached “to” another object (like a Room or Character).</p>
<p>A convenient way to create global scripts is define them in the <codeclass="docutils literal notranslate"><spanclass="pre">GLOBAL_SCRIPTS</span></code> setting; Evennia will then make sure to initialize them. Scripts also have an optional ‘timer’ component. See the dedicated <aclass="reference internal"href="../../../Components/Scripts.html"><spanclass="doc std std-doc">Script</span></a> documentation for more info.</p>
</section>
<sectionid="conclusion">
<h2><spanclass="section-number">10.6. </span>Conclusion<aclass="headerlink"href="#conclusion"title="Permalink to this headline">¶</a></h2>
<p>Any game will need peristent storage of data. This was a quick run-down of how to create each default type of typeclassed entity. If you make your own typeclasses (as children of the default ones), you create them in the same way.</p>
<p>Next we’ll learn how to find them again by <em>searching</em> for them in the database.</p>