Jump to content

* * * * *
Any object in a map in Leadwerks Editor can be saved as a prefab file.  The prefab can then be dragged into different scenes, and all their special properties will be loaded.  This provides us with an easy way to build reusable game-ready objects we can drop into a scene and play.  In this tutorial we'll learn how to create and use prefabs.

Creating Prefabs

Open the map "Prefabs.map".  The scene only contains a box brush for the floor, and a single model of a metal fire pit.  We're going to add some more objects to the scene and then save a reusable prefab.

Create a new point light, then select the Scene tab in the side panel, and select the Appearance tab in the properties panel, at the bottom of the scene panel.  Set the Diffuse property to orange (255,128,0).

Attached Image: prefabs2.png

Now position the point light so it is just above the fire pit model.  This will cast a nice dramatic shadow and add to the ambiance whenever we use this object in a map.

Attached Image: prefabs3.jpg

Now we will add a particle emitter for the fire.  Create a new emitter and place it somewhere down in burning logs.  Remember, to create a particle emitter, we select the Objects tab in the side panel, then in the objects panel we choose "Effects" and "Particle Emitter".

Attached Image: prefabs4.png

At this point, your particle emitter will look more like fairy dust than fire.  Let's see what we can do to fix that.

Attached Image: prefabs5.jpg

Select the Scene tab again in the side panel, then select the Emitter tab in the properties panel at the bottom of the scene panel.  Now make the following modifications:
  • Set Duration to 700.  This will make the particle lifetime shorter.
  • Set Particles  to 10.  This will use fewer particles.
  • Set Velocity to (0.0, 1.0, 0.0).  This will make the particles rise faster.
  • Set Area to (0.1,0.1,0.1).  This will make the particles originate in a more centralized position.
  • Set Max Scale to (3.0,3.0).  This will make the particles bigger.
Our particle emitter will behave a little more like a fire now, although it's obviously unfinished..

Attached Image: prefabs6.jpg

Select the Appearance tab in the properties editor, and set the Material property to use the file "Materials\Effects\fire.mat".

Attached Image: fire material.png

Now we're cooking with fire!  Literally.  Feel free to adjust the emitter further if you want to tweak the effect.

Attached Image: prefabs7.jpg

Now we have a nice little object to use in our maps, but it involved adjusting a lot of settings.  I would not want to have to repeat those steps every single time I place an object like this in a map.  Prefabs were invented so that we could set up our objects once, and then use them over and over again.

Saving Prefabs

It's super easy to save any object in a scene as a prefab.  Just right-click on its node in the scene panel and select the Save as Prefab popup menu item.  Enter the name of your prefab, press the Save button and you've got yourself a genuine bona fide prefab.

Attached Image: save prefab.jpg

But wait a minute...we don't want to just save our model by itself.  We want it to include the point light and particle emitter we so carefully set up.  How can we combine these three objects into one?  We can do this by parenting the light and emitter to the model.  Make sure the "Point Entities" and "Models" scene filters are both expanded, then click and drag the light onto the fire pit model.  Let go of the mouse, and it will be parented to the model.  Do the same for the emitter.  The scene tree should now look like this:

Attached Image: prefabs8.png

Right-click on the fire pit model's node and select the Save as Prefab popup menu item. Enter the name "My Prefab" and press the Save button.

Using Prefabs

Select the Assets tab in the side panel and navigate to the "Prefabs" folder in the asset browser.  You should see your prefab you created, staring right back at you.  (The fire won't be visible because the thumbnail renderer does not update timing to make the particle emitter play.)

Attached Image: my prefab.png

Drag this item into your scene next to your original model, and now there are two of them.

Attached Image: prefabs10.jpg

You can create as many instances of this object as you want, without having to set up each one individually.  Prefabs are always loaded from the prefab file.  If you ever decide to make a change to the original prefab by overwriting the prefab file, your changes will appear in any maps that use that item.


Prefabs are a great help to the Leadwerks workflow that save a lot of time and assist with team projects.  You can find more game-ready prefabs to place in your game in Leadwerks Workshop.