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

Is it more efficient to declare a function within the global scope or a local scope of a script?

Asked by 5 years ago
Edited 5 years ago

Quick question. I wanted to go about calling a function while yielding another in order to shorten up code, like so:

--NOTE: This is just an example, and has no real practical uses
--f represents function #1
function example(conditional)
    local var = 0
    if conditional then
        var = f(var)
    else
        var = f(var+1)
    end
    return var
end

Though I didn't know if declaring function f listed above in the scope of the function examplewould be more efficient than in the global scope.

0
I don't understand the question. You never declared "function f". Sir_Melio 221 — 5y
0
Read the script comment. "f represents function #1". Just imagine f is declared somewhere. What I'm asking is what scope I should define it in. LateralLace 297 — 5y

1 answer

Log in to vote
0
Answered by
Sir_Melio 221 Moderation Voter
5 years ago
Edited 5 years ago

Well if you're only using it in that function, then don't define a second function at all. Write the second function's code directly into that function instead, making it only 1 function. That’s more efficient. But if you don't want to do that, then define it locally right above it, not globally.

However, even if efficiency might differ in multiples, it's pretty much negligible if you're considering game resources.

Ad

Answer this question