Best way to have lots of villagers

Hello,


I’m building a Unity game which will have a village/town containing potentially hundreds of villagers/peasants. Now if I were to have all of these villagers moving around at the same time I’m sure my PC would blow up under the struggle of having to calculate so much, so I’m trying to think of ways to optimize my game so it runs smoothly. My idea was to have only a certain number of peasants walking around at any one time, and the rest can be “in their house”.


So, my question is, what’s the best way of doing this? Baring in mind, I also want my population to be a proper population, so each day/night cycle I want each peasant to earn a bit of money from their job, pay a percentage towards the towns treasury in tax, and so on. De-activating their objects might save on CPU or GPU performance, but they’d still be taking up RAM so will still have a performance hit, plus their components will be deactivated so it might prevent them from paying into the treasury if I’m relying on co-routines in my scripts or something like that.


I had a few ideas…

  • Use de-activation regardless. We could still access the components from an active script, and I assume changing the peasants script variables still works when de-activated. This idea isn’t very good on RAM.
  • Save a clone of their object when they go inside a building, and destroy their object. This clone could be re-instantiated at another time. I’m not sure how to do this though, would their variable data like money persist? Does it actually save on RAM if I still have the cloned object? How would I still increase their money overtime if they aren’t instantiated?
  • Give up on the idea of having actual villagers, just spawn a random peasant each time and don’t worry about specifics. This would be most performant probably, but it potentially takes away from the actual game in regards to a living & breathing eco-system and economy.

Any help and guidance would be much appreciated.

Well the main thing you want to do, which you haven’t mentioned, so not sure if you are doing it yet, is to make sure you don’t have an update on your villagers… You need a Villager Manager. If your villagers make the same money and pay the same taxes then you don’t need to do a the calculations over and over again, you just do it once and then times it by the number of villagers. if they make and pay different amounts then you will have to do more calculations, but you still want them to all be happening in one update. So if you have Update() in your villager script, then rename it VillagerUpdate and add it to the VillagerManager Update(). Lucky for you I have a free asset for doing this very thing Update Loop Manager | Integration | Unity Asset Store
I do something similar in my RTS asset and that allows me to move direct over 300 troops smoothly.

Destroying and reinstantuating is always the more processor heavy option, if you are going to use something again better to just deactivate it. When trying to create a living & breathing eco-system you will have to use some smoke and mirrors. LIke in a game where you earn money even when it’s not open, it’s not running in the background or a cloud it just takes the current time and the time you last logged in and calculates the amount. As long as the math is right it doesn’t matter, you want to get the same result with less effort.