Access local variable outside an if

Asked

Viewed 664 times

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 am working on a Lua file similar to this scheme:

    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)

Behold functioning on the ideone and in 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 the if and be able to access it externally without having to create a scope?

I don’t know if you’re doing what you want, but it’s the solution. I think initialize the variable already in the statement and only change its value within the if would be better, but it depends on the outcome you expect.

1 answer

3


Browser other questions tagged

You are not signed in. Login or sign up in order to post.