- Home /
Question is off-topic or not relevant
Unity: recursion or not recursion?
Hi everyone. My questions is more theoretic than real, but I'd like to create the right mindset to improve my unity skills.
The reason of the question is:
I haven't seen any recursive method in unity yet. I'm using third party code and other code I found on the web. I've also followed quite a few tutorials, but yet no recursion.
The question is:
Can anyone explain me why there is so few recursion in Unity? Maybe it's just me and I've not yet worked on advanced code. Or maybe it's because of unity logic. I just like to know if I should or should not implement recursion in Unity.
Discussion on program$$anonymous$$g paradimes is for the forums. Hang around here and you will see the occasional recursive script, where the problem warrants it.
Answer by Redeemer86 · Oct 28, 2014 at 08:33 AM
Well, unity logic is one thing ... Don't forget recursive processes are complex and expensive on your executable code. The recursive and tail recursive algorithms in theory are more calculative and continuous process which depends on its previous outputs and inputs until base conditions are reached.
For the purposes of a game engine like unity there really is no particular necessity of an inbuilt recursive method or interface. Furthermore most inbuilt frame dependent methods like update etc... Are frame dependent unless you are using your own custom classes or functions. The philosophy of per frame calls in unity mono behavior scripts would further complicate and lead to catastrophic results also freezing your project execution if used carelessly. And as you would realize most inbuilt methods and functions in unity are implemented usually as frame dependent methods.
Though if you need to play with concepts like tail recursion and Fibonacci which are ideal and specific to concepts of recursive calculations then you can always make custom classes and methods ... But just for the sake of using recursion it is not preferable to use a game engine like unity where object manipulation code is structured except if you have truly circumstantial reasons to use the calculations in a seperate class or to perform certain clearly defined code effects. Loops, frame updates and animations can do a lot more for you cheap than what recursive calculations can expensive as they are.
Red.
Also keep in $$anonymous$$d the Start, Awake, Update ... And various frame dependent methods are manipulatable to provide ways for patterns of execution to provide effects and effective calculations for desired effects.
Red
Answer by Jeff-Kesselman · Oct 28, 2014 at 03:22 PM
As others have said, the fundamental nature of Unity3D (and all client based games) is a game loop.
Having said that, recursion can be very useful for certain types of problems inside of that loop, such as tree evaluation, and there is no inherent reason not to use it. Call overhead is pretty minimal in modern compiled code and there is no damage you can do to yourself with recursion that you cant do equally easily with nested loops.
Additionally, if you want to see examples of recursion in Unity, I would start looking for projects that are very AI or path-planning heavy.
Second that. Recursion in Unity works great for the usual things recursion is good for. I use some recursive functions in a lovely maze generator. And in all my "do X to parent and all children" utility functions (the same as the old SetActiveRecursively
used to do.)
I think most game programs just tend to be too simple to need recursion, and most game programmers aren't comfortable with it (the same reason you rarely see inheritance.)
$$anonymous$$y answer was to emphasize on the point as I understood from your question , that there are no requirements for inbuilt recursive methods in unity or predefined unity functions ... Although if your implementation is justified you can customise your own functions to achieve specific recursive calls as side effects .
Red
Follow this Question
Related Questions
Distribute terrain in zones 3 Answers
Multiple Cars not working 1 Answer
Unity and Custom Defines 1 Answer
Ref modifier 1 Answer
A node in a childnode? 1 Answer