- Home /
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.
Answer by logicandchaos · May 04, 2020 at 08:21 AM
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 https://assetstore.unity.com/packages/tools/integration/update-loop-manager-163728 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.
Hey LogicAndChaos, thanks for the response. It's always great hearing about how others might do things, and I really like your idea of using a $$anonymous$$anager to update everything at once in a single location - this is good because even if the peasants are deactivated, the code still runs! As for the deactivation or destroy, I see what you mean and I agree many re-instantiations is probably more costly than just keeping objects alive in RA$$anonymous$$ to be re-used at a later date. I think I've got a way forward now, and it's using what you've recommended, thanks.
Your answer
Follow this Question
Related Questions
Project moved to Unity 2020 from 2017 using much less ram, less CPU, but performing much worse 0 Answers
Skinned mesh performance concerns 0 Answers
Cloned object has not materials/textures 0 Answers
how to improve the the colliding system 2 Answers
How would I enable a particle system based on size/magnitude? 1 Answer