I’m working on a hunger, fatigue, thirst, and mood system. The vitals decline over time, so I’m worried about creating a method that clogs up the CPU with calls every frame. If I used something like a coroutine, what time interval would be optimal for checking vitals? Are coroutines even the best way to go?
Calls every frames does not necessarily means high CPU usage. Is your simulation that intensive?
Generally, such issues are resolved by performing some benchmarks and profiling. That would allow you to found out which part of your program needs to be optimized or eventually to be executed less frequently. Until then, don’t worry that much about CPU usage. Implement your game and later on you can check the profiler.
Try using InvokeRepeating() with like 10 second intervals or something.
Like ericbegue stated, just because you do things every frames doesnt mean the CPU usage is going to be very high.
However, yes, coroutines are nice because you can repeat them every X amount of seconds which you can decide.
It truly depends on how important you need to check these vitals. Honestly, you can check them each second, or even every 10 seconds. It all depends on how important you want to check them. If you have no problem (performance-wise) check each second, why not go for it. (Of course, if you need to check every second.)
Thanks a lot guys. That’s all really helpful.
This. It’s a game design question first. How often does your game need the vitals check?
I wouldn’t need to check too many times for one unit, but I’m making a town building/defense game and making performance the limiting factor of population count (I’m aiming for around thirty people, as my experience with games like Timber and Stone and Stone Hearth is that they start to lag or get buggy when about 30 units are in the scene). I’m guessing thirty units all checking their hunger, mood and sleep would be pretty taxing, no?
I would suggest them only needing checking vitals when they need to. For stuff like health only check this when they take damage, but hunger/energy/stamina/ect. 10 seconds should be fine. To add variation you could make it something like 200 hunger and deprecate 3 if they are idle and 7 if they are busy ever 10 seconds. Maybe you could even go with a random check like ever 8-12 seconds.
Checking some state variables might not become the bottleneck for your game, again don’t worry about it.
[quote=“devstudent14, post:7, topic: 628338, username:devstudent14”]
I’m guessing thirty units all checking their hunger, mood and sleep would be pretty taxing, no?
[/quote] That’s pure speculation.
The only way to know what drop the performance of these games as their unity count increases, is to profile them. It could be caused by graphics, the animations, the AIs, the physics, the sounds,…, or a combination of those, or whatever.
Don’t focus on checking the unit stats, there is a great chance that your eventual performance problem will not be there.
Stone Hearth lags out at 30. Age of Empires hit 200. Supreme Comander hits 2000. AI wars gets near a million.
You can optimise your game to hit any unit count. Often displaying the units is more expensive then having them.
For best results build a stress test and run it through a profiler.
Still very far from performing a stress test as the project is still in it’s infancy. I’ll definitely run one when it’s time instead freaking out about how often I should be checking a stat at this stage (every 10 seconds with variation depending on activity is an excellent suggestion, however, and one that I’ll take on board).
@ericbegue a bit off topic but I noticed you have a behaviour tree asset? I’m interested in learning how to script behaviour trees. I use FSMs all the time, but I do not even know where to begin with behaviour trees, and I haven’t found any solid tutorials. I do not even know if behaviour trees are something you can ‘script’ like you can an FSM. Any advice? I code in c#.
Stress testing this would be trivial. Make a script that changes a dozen floats every update. See you many you can put in a scene before performance becomes an issue.
Good point. I thought I’d have to wait until all the assets, animations etc. were complete… but then it would be harder to ascertain which components were causing the drop if there was one. I’ll perform the test as soon as the scripts are done. Thanks
not really,
http://docs.unity3d.com/Manual/Profiler.html
https://unity3d.com/learn/tutorials/topics/interface-essentials/introduction-profiler
https://unity3d.com/learn/tutorials/topics/interface-essentials/profiler-overview-beginners
I’m glad you’re interested in Behaviour Tree. Though FSMs are simple and easy to understand, you’ve already noticed how unpractical and how hard they are to modify, specially whith large FSMs. BTs have a steeper learning curve, but once you’ve got a grip on it, it’ll be a breeze to write complex logic.
Scripting is more a Panda BT feature than a natural aspect of BT. They are other BT tools on the Asset Store that provide a visual node-based editor. But Panda BT is more oriented towards programmers and based on scripts (yet the execution of the tree can be visualized at runtime); you define your tasks as C# functions, then use them while writing BT scripts to define your behaviour trees.
Behaviour Tree is relatively young compared to FSM. There are far less ressources available about BT. As a starting point, I’d suggest these resources to get a grasp what behaviour trees actually are:
http://www.pandabehaviour.com/?page_id=48
Once you are familiar with some BT basic concepts, using Panda BT should be straight forward. Then have a look and toy around with the examples provided in the package.
Of course, I’m here if you have any question. You’re also welcome on this thread:
http://forum.unity3d.com/threads/ai-scripting-panda-bt-behaviour-tree-scripting.388429/
@ericbegue Thanks a lot. I think this is exactly what I’m looking for. I asked this behaviour tree question somewhere else on the forum: Behaviour trees: Looking for a way in - Questions & Answers - Unity Discussions It might be good to paste your response there as it answers the question perfectly. Look forward to learning more about BTs using Panda.
and there I was thinking that ‘profiling’ meant looking at the stats tool in game view. Thanks
Thank you for the suggestion. I’ve posted the answer there too. I not very familiar with with answers.unity3d.com though.
Have fun learning about BT, I’m curious how you’ll still love FSMs after learning more about BTs;).