Sick Gaming
News - Blog: Using prefabs in game development - Printable Version

+- Sick Gaming (https://www.sickgaming.net)
+-- Forum: Sick Gaming Community (https://www.sickgaming.net/forum-1.html)
+--- Forum: Lounge (https://www.sickgaming.net/forum-8.html)
+--- Thread: News - Blog: Using prefabs in game development (/thread-85297.html)



News - Blog: Using prefabs in game development - xSicKxBot - 06-20-2018

Blog: Using prefabs in game development

<div style="margin: 5px 5% 10px 5%;"><img src="http://www.sickgaming.net/blog/wp-content/uploads/2018/06/blog-using-prefabs-in-game-development.png" width="640" height="100" title="" alt="" /></div><div><p><strong><i><small> The following blog post, unless otherwise noted, was written by a member of Gamasutra’s community.<br />The thoughts and opinions expressed are those of the writer and not Gamasutra or its parent company. </small></i></strong> </p>
<hr />
<div class="logos">
<div id="logo-1"><img src="http://www.sickgaming.net/blog/wp-content/uploads/2018/06/blog-using-prefabs-in-game-development.png" /></div>
<div id="logo-2"><img src="http://www.sickgaming.net/blog/wp-content/uploads/2018/06/blog-using-prefabs-in-game-development-1.png" /></div>
</div>
<p><em>Prefabs are not a new topic to game developers. These will be referenced in future articles of mine, so here’s an overview to a concept for mastering massive content.</em></p>
<p>If you’ve ever had to deal with tons of objects across multiple scenes, you’ll inevitably wish you could group similar or exact copies together for quicker management. If you’ve added a desk lamp to 30 different office rooms in 15 different scenes, wouldn’t it be nice to turn up the that light’s intensity once instead of <strong><em>450</em></strong> times?</p>
<p><em><strong>That’s the power of prefabs. </strong></em></p>
<p>So, what exactly is a prefab? It’s a collection of game objects used in multiple places across the game. It’s a concept I’ve made sure to use across past projects like <em>Six Days in Fallujah</em>, <em>Prey 2</em>, and <em>Mafia 3</em>.</p>
<div class="row2">
<div class="column2"><img src="http://www.sickgaming.net/blog/wp-content/uploads/2018/06/blog-using-prefabs-in-game-development-2.png" /></div>
<div class="column2"><img src="http://www.sickgaming.net/blog/wp-content/uploads/2018/06/blog-using-prefabs-in-game-development-3.png" /></div>
</div>
<p> <br />
A <strong>Prefab</strong> should be a <strong>Scene</strong>. The only thing that should make a prefab different than a scene (a level, map, environment, etc.) is it exists <u>inside</u> a scene. A large city scene can have several small prefab neighborhoods and those in turn can have repeatable prefab houses inside of each.</p>
<p>Some game engines develop prefabs as a unique feature, separate from a scene’s format. By developing a prefab as an extension of a classic scene means they inherit all features and power scene editing has!</p>
<p>Any feature that exists for a scene should exist for a prefab:</p>
<ul>
<li>A collection of models (a bench, a bus stop sign, a garbage can).</li>
<p> </p>
<li>A building with AI navigation and interaction nodes.</li>
<p> </p>
<li>An enemy with a unique change (BadGuy_Type_4 with dual pistols).</li>
<p> </p>
<li>A complex script sequence (depending on project’s script file handling).</li>
<p> </p>
<li>A skybox with all associated FX and animation.</li>
<p> </p>
<li>A Security Door (a door model &amp; logic, security palm switch &amp; logic, emergency lighting &amp; logic, surrounded by cover nodes &amp; path volumes).</li>
<p>
</ul>
<h2>Advantages</h2>
<ol>
<li>Repeat occurrences only have to be created once. When the original prefab is modified, all instances are updated.<br />  </li>
<p> </p>
<li value="2">Multiple people can work on the same scene by partitioning the scene into prefabs. Each user works on their individual prefab, testing their work in the master scene.</li>
<p>
</ol>
<h2>Game Usage</h2>
<p>Prefabs should be created in the editor as a new scene or from a selection within an existing scene. The most common type is a <strong><em>Static Prefab</em></strong>. They can be saved to the game library for reuse later. When the game is built and run, all static prefabs are ungrouped (sometimes called exploded). All parts should have their name prefixed to the prefab’s unique name for reference. All game logic now points to unique items maintaining unique scripting.</p>
<p>In the editor they can also be exploded into an existing scene to creating unique, free roaming objects, no longer connected to its source.  </p>
<p>Aside from static prefabs, <em><strong>Dynamic Prefabs</strong></em> are another option. These are used to stream in content. If done right, prefabs can be the primary means all content is contained and streamed into the game.</p>
<p>With procedural systems like <em><strong>Houdini</strong></em>, you could even extend prefabs into becoming <em><strong>Smart Prefabs</strong></em>. These feature a set of rules to decide what, where, and when to place a variety objects at different times. This article is only about the core prefab concept though.</p>
<p>Every scene can be made with a number of prefabs.  Take a look at the following diagram as an example:</p>
<p><img alt="" src="http://www.sickgaming.net/blog/wp-content/uploads/2018/06/blog-using-prefabs-in-game-development-4.png" /></p>
<p>Now let’s see how prefabs let several people work on the scene: </p>
<p><img alt="" src="http://www.sickgaming.net/blog/wp-content/uploads/2018/06/blog-using-prefabs-in-game-development-5.png" /></p>
<p>The Art Team works on the <strong>Ground Terrain A</strong> location:</p>
<ul>
<li><strong>Artist 1</strong> models <strong>Building D</strong> with indoor <strong>lighting</strong> to be used in 2 locations.</li>
<p> </p>
<li><strong>Artist 2</strong> models <strong>Building C</strong> to be used in 4 locations.</li>
<p> </p>
<li><strong>Artist 3</strong> models <strong>Building B</strong> to be used in 3 locations.</li>
<p> </p>
<li><strong>Artist 4</strong> models <strong>Building A</strong> with indoor <strong>lighting</strong> to be used in 4 locations.</li>
<p>
</ul>
<p><strong>Artist 5</strong> is <strong>lighting</strong> the whole scene. Some lighting features on-off logic with <strong>Gameplay</strong>.</p>
<p>The <strong>Audio</strong> department is working on ambient <strong>Sound</strong> for the scene, mixed with <strong>Gameplay</strong>.</p>
<p>The Design Team is providing gameplay to the <strong>Ground Terrain B</strong> location:</p>
<ul>
<li><strong>Designer 3</strong> is creating a generic prefab for assigning AI and stats for a <strong>Train Event</strong>.</li>
<p> </p>
<li><strong>Designer 2</strong> is creating <strong>City Tower</strong> logic to be used in 4 separate places in Ground B.</li>
<p>
</ul>
<p>And lastly, <strong>Designer 1</strong> is providing gameplay for the overall scene in the <strong>Master Scene</strong>.</p>
<p>When any person is finished, they check their prefab into Source Control to share with the rest of the team. When the master scene is opened again, any existing prefab is updated with the latest changes. When new content ready, the user should be able to pick them from the Resource Browser to add to the scene without restarting the editor.</p>
<h2>Example Scene</h2>
<p>Let’s group areas in a scene into prefabs, allowing several people to work on each area:</p>
<div class="row2">
<div class="column2"><img src="http://www.sickgaming.net/blog/wp-content/uploads/2018/06/blog-using-prefabs-in-game-development-6.png" /></div>
<div class="column2"><img src="http://www.sickgaming.net/blog/wp-content/uploads/2018/06/blog-using-prefabs-in-game-development-7.png" /></div>
</div>
<p> <br />
A Designer takes a city block and creates a series of prefabs from it:</p>
<ul>
<li>Each building is named appropriately and saved as a prefab.</li>
<p> </p>
<li>A city block of prefabs is saved as one sector (optional).</li>
<p> </p>
<li>The master scene is now a framework of roads, game logic, prefabs, and sectors.</li>
<p>
</ul>
<div class="row2">
<div class="column2"><img src="http://www.sickgaming.net/blog/wp-content/uploads/2018/06/blog-using-prefabs-in-game-development-8.png" /></p>
<p>Original Scene</p>
</div>
<div class="column2"><img src="http://www.sickgaming.net/blog/wp-content/uploads/2018/06/blog-using-prefabs-in-game-development-9.png" /></p>
<p>Scene cut into 3 Game Sectors</p>
</div>
</div>
<p>Artists individually open each prefab building to edit it:</p>
<ul>
<li>They can replace the stand-in model with a final version.</li>
<p> </p>
<li>They can even add new objects like doors, windows, lamps, lights, etc.</li>
<p> </p>
<li>They can open the sector to understand relations between each building prefab.</li>
<p>
</ul>
<p>When a Designer re-opens the scene, all prefabs are updated with changes the Artists have made.</p>
<div class="row2">
<div class="column2"><img src="http://www.sickgaming.net/blog/wp-content/uploads/2018/06/blog-using-prefabs-in-game-development-10.png" /></p>
<p><strong>Artist 2</strong> works on an Apartment<br /> <br />
within Game Sector 1</p>
</div>
<div class="column2"><img src="http://www.sickgaming.net/blog/wp-content/uploads/2018/06/blog-using-prefabs-in-game-development-11.png" /></p>
<p><strong>Artist 3</strong> working on individual Motel Prefab</p>
</div>
</div>
<p>How do you set up scenes to use sectors? How do you work on them? How should logic be arranged in them? Before we talk about sectors, we need to understand how the scene played in the game (the <strong>Master, Main, </strong>or<strong> Persistent</strong> scene) is organized. Each scene consists of four basic components:</p>
<p align="center"><img alt="" src="http://www.sickgaming.net/blog/wp-content/uploads/2018/06/blog-using-prefabs-in-game-development-12.png" /></p>
<p>Applying this concept to sectors, we can create a framework for maximum flexibility to add (or remove) parts of the master scene without impacting the master scene itself. Each component can become a prefab for each sector. </p>
<p align="center"><img alt="" src="http://www.sickgaming.net/blog/wp-content/uploads/2018/06/blog-using-prefabs-in-game-development-13.png" /></p>
<p>Some prefabs don’t have to be exclusive to a sector; they can span several sectors. This allows one person to check out a prefab to have access to the whole scene. The disadvantage is when sectors are move around in the master scene requiring contents to be moved manually to their new position.</p>
<p align="center"><img alt="" src="http://www.sickgaming.net/blog/wp-content/uploads/2018/06/blog-using-prefabs-in-game-development-14.png" /></p>
<h2>Editing Prefabs</h2>
<p>Two methods of prefab editing should exist:</p>
<ol>
<li>Edit the prefab in a new scene.</li>
<p> </p>
<li>Edit the prefab in the current scene.</li>
<p>
</ol>
<div class="row2">
<div class="column2"><img src="http://www.sickgaming.net/blog/wp-content/uploads/2018/06/blog-using-prefabs-in-game-development-15.png" /></div>
<div class="column2"><img src="http://www.sickgaming.net/blog/wp-content/uploads/2018/06/blog-using-prefabs-in-game-development-16.png" /></div>
</div>
<p>Editing prefab “A” in a new scene isolated from original scene.</p>
<div class="row2">
<div class="column2"><img src="http://www.sickgaming.net/blog/wp-content/uploads/2018/06/blog-using-prefabs-in-game-development-17.png" /></div>
<div class="column2"><img src="http://www.sickgaming.net/blog/wp-content/uploads/2018/06/blog-using-prefabs-in-game-development-18.png" /></div>
</div>
<p>Editing prefab “A” within the scene for instant feedback how it affects the scene. <br /> <br />
(The fences will need to be modified.)</p>
<p>By editing the prefab within the current scene, it removes switching back and forth between scenes to see your changes.</p>
<p>By their nature, prefabs placed in scenes are instances of the original prefab. If changes are made to the prefab, those changes propagate to all copies in every scene. This can be helpful for example, if you want to change a texture or a light across a hundred copies. You might however only want one small change.  You need a way to remember those individual changes, instead of creating a new prefab for each change, exponentially increasing the number in the game library. </p>
<p>This can be solved by tagging any differences and re-applying those differences after changes to the original is made. Technically speaking, values unique to any type of object are stored in the parent prefab or scene file to override its original values. When a change to the original is made, all copies reflect that change unless the value has been overridden on the instance.</p>
<p>An “Allow Deltas” action provides a way to maintain these overrides. With a prefab selected and “Allow Deltas” on (set to true), unique values will be accepted and maintained. If “Allow Deltas” is turned off, all values are reset to their default.</p>
<p align="center"><img alt="" src="http://www.sickgaming.net/blog/wp-content/uploads/2018/06/blog-using-prefabs-in-game-development-19.png" /><br /> <br />
Without Deltas, editing one prefab edits all of them.</p>
<p align="center"><img alt="" src="http://www.sickgaming.net/blog/wp-content/uploads/2018/06/blog-using-prefabs-in-game-development-20.png" /><br /> <br />
With Deltas, unique edits remain while<br /> <br />
any unchanged values inherit changes.</p>
<p>To prevent the need to open prefabs, another option is enabling Child-picking. This ignores prefab containers and directly picks any object in view, no matter how deep they’re nested. Normal editing of prefabs should be outlined in red to indicate changing this one instance will change all instances of it.  With Deltas on, objects inside prefabs should be outlined in cyan, letting you know any changes will only be applied to the selected object.</p>
<p>Today’s games involve thousands, if not millions, of objects. Managing all that content, making quick changes, and sharing team efforts, becomes a massive burden without a prefab system in place. If you want to spend more time discovering what’s exciting about your game and less time playing 52,000 card pickup, then take the time to invest in a proper prefab feature.</p>
<p><em>You can find more posts like this on my website at <a href="https://www.curiousconstructs.com">CuriousConstructs.com</a>.</em></p>
</div>