Access local variable outside of an if

4

How to create / modify a variable within a if and be able to access it without having to set it in global mode / scope. I'm working on a Lua file similar to this schema:

    if verificacao then
        local variavelDoArquivo = 123
    end

    -- preciso acessar a variável que foi criada pelo if verificação fora do if mas no mesmo arquivo. Ex:
print(variavelDoArquivo)

See working on ideone and on CodingGround .

In this situation returned blank on the console, but in some cases returned nil in other situations. How can I create / set a variable within if and be able to access it externally without having to create a scope?

I do not know if you're doing what you want, but it's the solution. I think initializing the variable already in the statement and just changing its value inside the if would be better, but it depends on the result you expect.

    
asked by anonymous 16.11.2016 / 07:44

1 answer

3

Declare it out of if to assign it within:

local variavelDoArquivo
if true then
    variavelDoArquivo = 123
end
print(variavelDoArquivo)

Read more about What's the difference between declaration and definition? and #.

    
16.11.2016 / 10:05