Weird question about creating vars

I’ve seen some people create vars like this:



local var

function test()
     var = 1
end

test()


What’s the actual point of creating the var at the top if the code inside the function creates the var and sets it in the same line?
Is it done that way to improve performance?

now var is set to 1 :slight_smile:

why not just do:
local var = 1

inside the func.

your code, your variables

your choice

It’s so you can make the variable “global” in your file. It’s still local, of course, but now you can access the variable outside the function.

Run these two scripts and look at the output.
[lua]local var

function test()
var = 1
end

test()
print(var)[/lua]
[lua]function test()
local var = 1
end

test()
print(var)[/lua]

I can’t think of a good reason to do this specific thing.

There are very good reasons to do similar things.

Do you have a more concrete example from actual code?