Log In  

Basically what asked in the title. Is it possible to define a variable name in Lua without defining a value just yet?

For example, would

local tick

work as a way to define, in a function, a local variable named "tick" as a "nil" value automatically?

Sorry for the dumb question

P#143816 2024-03-19 09:39 ( Edited 2024-03-19 09:39)

Yes, that's correct.

P#143820 2024-03-19 10:17

Yep, looks like you have the right idea. To test it you can do something like this:

x = 5
function f()
  print(x)  -- uses global x
  local x
  print(x)  -- uses local x 
end
print(x)  -- uses global x

Notice how when the function first calls for x, it uses the global var when a locally defined var isn't found. Just something to keep in mind while you're coding :)

P#143821 2024-03-19 10:20

[Please log in to post a comment]