Okhlos is a game about an angry mob in ancient Greece. You will have to travel all around Greece and conform a very large group of angry people to fight armies, mythological creatures and even the gods themselves! Each stage will have its unique challenges based around the gods they worshipped there. You will have lots of fighting in Sparta, lots of thinking to do in Athens, and lots of partying when facing Dionysius. You get the idea. Also, each unit will have its unique set of skills, making the mob unique each time you play. There will be heroes, warriors, philosophers, slaves, and much more!

Report article RSS Feed Prefab Inception

This week we are going to talk about a problem that, although we finally managed to solve, had us trying lots of different solutions and has given us many head aches, a problem named Nested Prefabs.

Posted by CoffeePower on Mar 6th, 2014

Yes, I know, inception is putting an idea in the head of someone and not precisely recursion, but nowadays is a recursion synonym. And the other option was naming this post “Yo dawg, I heard you like prefabs”.

(All THE IMAGES ARE SCALED DOWN. CLICK THEM TO OPEN THEM IN A NEW WINDOW)

Unity’s prefabs are instantiable objects. The problem is that these objects can’t be placed inside other objects, without losing the reference to the original object. It would be like a Flash MovieClip, but the main difference is that the MovieClip allows children to maintain their original reference.

Why did this bother us so much? Well, each stage in Okhlos, is composed by four chunks. Each chunk is, in turn, composed of different buildings. And each building is composed of different props and structures.

CHUNKS

chunk

This is a chunk. The main component of the chunks are buildings, but they can also have other elements like hazards, enemies, props, etc., and some of these can be completely randomized.

BUILDINGS

Building

The buildings are composed of a main structure and different props, also some other decorations which may be sprites or the like.

PROPS


Props are the weakest destructible elements. They usually have a small structure and some sprites. The following image will perhaps better illustrate how these things, chunks, buildings and props, fit together.

inceptiopn

As you can image by now, when we add this hierarchy to the prefab’s problem you end up with three o four levels of lost references for each chunk or building. At first we simply tried to deal with this without resorting to any kind of nesting of prefabs, but it came at a high price. Basically, we had to redo every chunk and building a couple of times.

What we do now, is use a script we named Building Holder. That script holds a reference to a prefab, and instantiates it on runtime.


This reference points to an object of GameObject type, thus it can be either a GameObject in the scene or a prefab. What we found out when we started using this was that if the reference pointed to itself, when we hit apply, it stored a reference to the Game Object in the scene, not to the actual prefab.

You can see it (kinda) more clearly here:

hierarchy

The green line shows how the reference points to the Project view (to the actual prefab). The red line shows the object in the Hierarchy view (which is the scene view, what we don’t want to happen). And the blue lines show the objects’ structure (chunk, building, prop).

To set the reference point to actual prefab and not  the instance is fundamental to be able to modify a building without having to modify its instance in each chunk (or to put it in other words, to be able to modify a building once instead of gazillion times) but luckily we now how to do it. 
Finally, a few screenshots showing some chunks together:

foto1
Editor View

foto2
Custom game view

foto3
Another editor view angle!

And closing up this week, a tree destruction .gif!


Post comment Comments
Satori4
Satori4 Mar 6 2014, 3:28pm says:

Nice work, looking forward to seeing more!

+4 votes     reply to comment
npc_violent
npc_violent Mar 7 2014, 10:45am says:

Looks fun, watching.

+2 votes     reply to comment
Big_Bone
Big_Bone Mar 7 2014, 12:29pm says:

GO TO HELL TREE!

+3 votes     reply to comment
studio9
studio9 Mar 8 2014, 1:03pm says:

This is so fantastic. Love the Greek and the game font, art is very cohesive.

Thanks for going into detail on the prefab system. I plan on learning Unity very soon and these articles are quite helpful.

+2 votes     reply to comment
Post a Comment
click to sign in

You are not logged in, your comment will be anonymous unless you join the community today (totally free - or sign in with your social account on the right) which we encourage all contributors to do.

2000 characters limit; HTML formatting and smileys are not supported - text only

Icon
Okhlos
Platforms
Windows, Mac, Linux
Engine
Unity
Contact
Send Message
Official Page
Coffeepoweredmachine.com
Release Date
TBD
Game Watch
Track this game
News
Browse
News
Report Abuse
Report article
Related Games
Okhlos
Okhlos Indie Single Player Arcade
Related Engines
Unity
Unity Commercial Released May 30, 2005
Related Groups
Coffee Powered Machine
Coffee Powered Machine Developer with 3 members