profectus-docs/assets/guide_dynamic-layers.md.a3c24a2c.js

6 lines
2.2 KiB
JavaScript

import{_ as a,c as n,o as e,a as s}from"./app.8e8712eb.js";const h='{"title":"Dynamic Layers","description":"","frontmatter":{},"headers":[],"relativePath":"guide/dynamic-layers.md"}',t={},o=s(`<h1 id="dynamic-layers" tabindex="-1">Dynamic Layers <a class="header-anchor" href="#dynamic-layers" aria-hidden="true">#</a></h1><p>You can dynamically add and remove layers using the <code>addLayer</code> and <code>removeLayer</code> functions. Note that removing a layer does not change the player save data in any way, so you can safely add and remove the same layer. In fact, there is a <code>reloadLayer</code> to do just that, which is used for when the structure of a layer changes - e.g., adding a new feature.</p><p>If you&#39;re going to be procedurally generating layers, all with a similar structure, it might make sense to use a utility function like the following in order to easily access a correctly typed reference to a layer with a given ID:</p><div class="language-ts"><pre><code><span class="token keyword">function</span> <span class="token function">getDynLayer</span><span class="token punctuation">(</span>id<span class="token operator">:</span> <span class="token builtin">string</span><span class="token punctuation">)</span><span class="token operator">:</span> DynamicLayer <span class="token punctuation">{</span>
<span class="token keyword">const</span> layer <span class="token operator">=</span> layers<span class="token punctuation">[</span>id<span class="token punctuation">]</span><span class="token punctuation">;</span>
<span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token operator">!</span>layer<span class="token punctuation">)</span> <span class="token keyword">throw</span> <span class="token string">&quot;Layer does not exist&quot;</span><span class="token punctuation">;</span>
<span class="token keyword">return</span> layer <span class="token keyword">as</span> DynamicLayer<span class="token punctuation">;</span> <span class="token comment">// you might need an &quot;as unknown&quot; after layer</span>
<span class="token punctuation">}</span>
</code></pre></div>`,4),c=[o];function r(p,i,l,d,u,y){return e(),n("div",null,c)}var _=a(t,[["render",r]]);export{h as __pageData,_ as default};