Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

Is there a way to avoid recursion?

Asked by 6 years ago
Edited 6 years ago

Why do I have a recursive function?

In my script, I have this function that checks if there is a value called "Status".

NOTE: THERE IS CODE IN BETWEEN THE LOOPS AND IF STATEMENTS THAT I DIDN'T SHOW

local function recursive(model)
    for _,child in pairs(model:GetChildren()) do
        for _,descendant in pairs(child:GetChildren()) do
            if descendant:IsA("StringValue") and descendant.Name:lower() =="status" then
                if string.sub(descendant.Value,1,4):lower() == "jump" then
                    --code
                elseif string.sub(descendant.Value,1,5):lower() == "speed" then
                    --code
                elseif string.sub(descendant.Value,1,4):lower() == "kill" then
                    --code
                end
            else
                recursive(descendant)
            end 
        end
    end
end

recursive(newMap) -- newMap is declared before in the actual script

What is newMap?

newMap is a map that is being cloned into Workspace. I have declared this variable before so you don't have to worry about that.

Status

Status is supposed be in a model, not added.

As you can see, I am using recursion. However, I want to avoid using recursion as much as possible for good performance in my game. Is there an alternative to recursion?

0
This isn't recursive. The `recursive` function never calls itself. Are you somehow cloning the script itself into its target to recurse? fredfishy 833 — 6y
0
No, actually, you can call a function inside of itself. saSlol2436 716 — 6y
0
Oh, I get it. I had insertScript(). I changed the name to recursive() because it would be more understandable. In the actual script, it would clone the scripts to parent of Status. Sorry about the confusion. saSlol2436 716 — 6y
0
Rather than calling the function on descendant, maybe instead push descendant to a stack, and have a `while` loop running until the stack is empty? Each loop would pop an element from the stack, and run what recursive currently does on it. fredfishy 833 — 6y
0
What do you mean by, "... push descendant to a stack ..." and why would using a while loop be faster than using recursion(Not as a negative thing. I just want to know if it is faster. I would like some evidence)? saSlol2436 716 — 6y

1 answer

Log in to vote
-1
Answered by 6 years ago

This is the most confusing question. You named the function recursive and you want to know how to stop recursion? Maybe edit the names you grabbed from the freemodel to show more of what you'd like to do.

0
First of all, I did not get this from a free model. This is a function from my Round Script I made on my own. You can't assume I got this from a free model. Second of all, I want to avoid recursion, but, I can't find a solution. saSlol2436 716 — 6y
0
I also show what I would like to do. I want to find the value Status. I would check if it's a StringValue with IsA(). I would use if-statements to check if the Status is a certain value. saSlol2436 716 — 6y
0
But here is the thing, I want to find an alternative of recursion. Is there any alternative? PS: This should've been a comment instead of an answer saSlol2436 716 — 6y
0
I understand why you assume this is a free model. The reason why is I forgot to change insertScript() (the original name of the function) to recursive(). saSlol2436 716 — 6y
Ad

Answer this question