Unity “Update” method with lots of objects with logic
In my game I'm working on in Unity, I want to have energy and machines (i.e ore processing machines, smelting (think Minecraft modded)), but I was thinking about performance. The player could have quite a few of these machines in their base, all of them could be doing something and could be off screen, so this could effect performance as each machine will have logic to handle whatever it needs to do, and lots of them could be running at the same time. Machine example; Let's say we have a machine that crushes stone. Player puts it into the machine slot, machine then starts crushing it if it has enough energy, and after X seconds, it outputs the crushed stone. Player could keep the machine UI open and watch the progress (i.e progress bar), or be far away from it. Should these machines be running constantly in Update, if not, what would be a better way to do this?
Some things will only need to be updated every once in a while, for instance an ore processing machine might only need to update once every second or so, you can achieve this by giving the objects you need to update a nextUpdate attribute, a time when they next need to be updated in order to run properly. You’d just set this to maybe the current time + 1 second or whatever is suitable for that particular object. You’d then need to loop through the list and call update on only the objects where their nextUpdate is less than the current time.
You could even make this more complex (but also better performance) by having machines change their update frequency based on player distance, whether they have stuff to do etc etc.
An even better (however more complex) solution would be to load and unload certain objects based on if they need to be updated or not, for instance a processing machine without any items in it doesn’t need to update. You’d need to remove it from the update list while it was idle and then re-add it once you detected interaction with the machine.
You could record the time that the items were put into the machine, then when the player interacts with the machine to remove processed items, use the time difference between current time when the items were entered to determine how many items should have been processed in that time. If the number of items exceeds the fuel cost of producing, return the max for the amount of fuel you have.
This would mean that there is no processing at all until the player interacts with the machine.
As an idea, you could make the machines do so: If the player is not in that machines UI so he can’t see the loading bar or what ever, which means you can just set a timer depending on the amount of the material eg. 10 seconds, and then it will finnish/output result (so nothing happens every frame). However if the player is in the UI of the machine than a script that is ON THE PLAYER will show the progress of the particular machine, thus the constant updating will only occur at the machine that a player interacts with at the time (only one at a time possible). I haven’t tried this so somethings may not quite work as I assume they will.
- Database Administration Tutorials
- Programming Tutorials & IT News
- Linux & DevOps World
- Entertainment & General News
- Games & eSport