Ode to Nested Prefabs from a noob indie dev
I must admit that i’ve been a bit what you could call ‘lazy’ for the past few months. Why is lazy hyphenated? Well, because i’m not really lazy, i just tried to finish a game from 0 knowledge of Unity in under two years, which is not an easy task. I spent a lot of sleepless nights working, had a few burnouts, but one thing ultimately slowed down my progress to almost halt.
When i finished most of the mechanics for the game and got to the most important part – making content – i simply couldn’t find any more willpower because of the tedious process involved in making hundreds of enemy waves. I believe someone with better coding skills could make a level editor and finish it much quicker, but with my knowledge, i had to do everything by hand and i kind of lost the motivation. Let’s delve a bit deeper into the problem.
One of the things i really love about Unity is use of Prefabs. As someone who is not a programmer by trade, it was easy for me to relate to something drag and droppable, an object with belonging properties that is simple to instantiate and easy to manipulate without much hassle. Two years ago, one of my first questions on Unity Forums was about something that i didn’t even know what is called back then – Nested Prefabs. I couldn’t understand why instantiated objects could have child objects that have child objects who also have child objects can exist in the scene, but not as a Prefab. That pretty much broke my building blocks concept of making a game.
As you can see, i imagined the waves of enemies to be compiled of squadrons (as well as waypoint and single enemies, about which i wrote in my previous logs) which would be a Prefab object with lots of children objects (singular ships and their engine jets, weapon, tags for missile homing and so on). Unfortunately, Unity supports only one level of vertical nesting in the project, so while an object can have literally hundreds of children, non of them can have their own. Since i read that the Nested Prefabs are something that was planned more than five years ago and not yet in the making i tried a few assets that simulate Nested Prefabs but to no avail. You’ve probably seen the horrible reviews on the Asset Store, most of them are abandoned, buggy, slow or complicated. Since i found no decent asset that will enable me to work the way i imagined, i resorted to the usual workflow of instantiating a Prefab and populating it with components that i needed.
It wasn’t too hard for single enemies, all i needed to do was instantiate appropriate objects on designated locations and that’s it. I learned a lot of things in the process, getting and setting the properties of many available components and their variables, the importance of pooling and the way it works, managing performance and so on. I must admit i had more than a handful of situations where i didn’t know how to overcome some of the challenges, but i’m grateful for them since they were an opportunity to learn something new through problem solving. When most of the stuff that make the core of the games look and feel were finished, the harder part of making a game in the true sense of words came. I won’t repeat myself too much, you can read more about my process of making waves in this and this log. In short, instead of dragging and dropping positions where i want the ships to spawn, assign the wanted behavior to each one depending on the wave structure and simply save all that as a prefab i need to:
- Have specific spawner types. That means single enemy, waypoint enemy and squadron enemy spawner with their locations.
- Make a specialized movement FSM’s for almost every enemy type that will dictate movement direction and scale of ships and ships’ children. For example, engine jet needs to be a separate object so it doesn’t flash with the ship upon bullet contact but it must be properly rotated and scaled depending on the spawning position and spawner parent of the parent (yeah, even i lost it while reading).
- Assign more elements to pool which slows down the compilation time and time required to start the game. Instead of pooling one ship with all the needed components i need to pool the ship prefab, jet prefab, weapon prefaband in some cases multiple weapon prefabs so the pool size for ships is actually at least three times the size in terms of object number. I’m fairly certain that it’s better to have fewer objects to instantiate regardless of their complexity (number of components attached).
- Manually set the spawning position of each ship in the wave. This is the worst part, it got me completely devastated. I need spawners for assigning some general behaviors and general screen position, but all the fine arrangement of ships in the wave must be done by hand. Not completely, but i need to put the ships in the scene so i can get their coordinates, then copy them into the spawning FSM of the squadron. Sure, i need to position the ships with nested prefabs too, but only once and that’s it. Doesn’t sound like much of a fuss, but imagine having hundreds of waves to make with some of them having double digit number of enemies that need to be repositioned upon spawning.
I’m sure some people don’t even use prefabs but create instances and populate them on runtime and i presume some more C#-savvy people will find nothing unusual in this and develop their own systems for handling the situation, especially big teams. But i’m neither of those and, for the time being, i really need nested prefabs to finish what i’ve started. Prefabs are great game building blocks that further upgrade great tool that Unity already is and we should be really glad they are taking into account the needs of small or one man teams. I’m anxious to see further improvements that the new prefab system will bring to the table in the future versions.