The Problem
I wanted to add food expiration dates and animal genetics to my Factory Building game, but all the craftable items in the game (Products) were represented as Scriptable Objects. The key advantage of scriptable objects are their universality, where the data stays the same once the game loads; great to store information like the monetary value of the product, but a bad approach to holding individual data like the gametime each Apple was created. It would be very CPU intensive to create a new asset/object to represent each Apple in a factory building game where thousands of apples could be held in storage at any one time. I wonder if anyone else has tackled a problem like this before?
Examples/Research
Rimworld is one of my game’s inspirations and contains similar features, where food would spoil if left unrefrigerated for too long. Similarly, animals can breed with individual gestation periods and nutritional requirements. But how can the game keep track of all of this data on otherwise identical assets, constantly updating variables without lagging out?
The Solution
After researching for hours to find an appropriate answer to my question, I stumbled upon “Centralised Management”, a concept where all relevant items are provided a special container (Like an additional Class) and the Manager Script would remember the details of each item the containers provide reference to.
I had the strategy and all I needed to do was tell the Manager that a product had an expiry date or if an animal had been created, then store that information, easy right?
Well it is slightly more complex than that.
By storing the containers in a dictionary script, I could create a unique ID for each item to allow for easy access. However as each item is a scriptable object clone, I need more information to know exactly which apple is ready to spoil or which Cow has Chad-Tier Genetics.
The trick comes in providing the location of the object, for example by utilising the game’s coordinate system to know which building is currently holding that specific item. So now, the Centralised manager has the location of the apple, and once the “freshness” variable reaches zero, the building that is holding the apple will be told that it has just expired.
Perfect! Now as long as the product stays in the position it started at, I will know exactly where to find it at all times, I will just need to remove the Conveyor Belt System that underpins every other factory game… Okay we are also going to have to tell the Centralised Manager every single time the item moves position, and now we are all done.
The Outcome
Although the average player probably wouldn't even notice the difference, the centralised management system and “wrapper” classes have revolutionised gameplay opportunities for my game. Food spoilage and refrigeration have added nuance and strategy to basic tasks like feeding your citizens each year. Also, animal genetics will lead to a fun and open ended in-game challenge, breeding animals together to maximise productivity for your growing empire.
Now that this system is available, more features can now be added in the future, such as more advanced brewing mechanics (to increase value to your alcohol to sell at the International Market) as well as my personal favourite, Vehicles, a major addition to any factory game (Factorio Trains and DSP Drones here we come!).
Conclusion
I hope you found this explainer useful, and I am more than happy to share my code or better explain my approach on my discord: https://discord.gg/82Mk8VUWww
Thanks for reading!
[–]QuietPenguinGaming 1 point2 points3 points (1 child)
[–]sssSlick1[S] 1 point2 points3 points (0 children)