Debugger doesn't show some local variables
Closed this issue · 1 comments
This is mobdebug issue I guess. Consider the following code fragment:
local var1 = 666
function func()
local var2 = 10
print("Hello world") -- breakpoint here
end
func()
When stopped at breakpoint Stack Window correctly shows both variables var1
and var2
. However other tools (Watch Window, Local console, tooltip) wrongly evaluate var1
as nil :(
But it's only half the story. When I make use of var1
in function func
, e.g.
local var1 = 666
function func()
local var2 = 10
print("Hello world", var1) -- breakpoint here
end
func()
then everything works fine. Making var1
global also fixes the issue.
This is not a bug, it's a feature. The Stack view analyzes all stack frames and finds all local variables that have been defined in the current or any of the outer frames, and as the result shows var1.
However, there is no var1
inside the function (in the first example). Lua frame that was created for the function doesn't have var1
variable there. Neither the watch window, not the console can access it. If you try to set var1
variable from the console while in the debugger, it will actually set a global variable var1
, and not the local one you have defined earlier.
The reason why it works when you do reference var1
is because in this case it becomes an upvalue and you do indeed see the correct var1
value and can modify it. The global variables are always visible, so there is no issue with them.
Essentially, the watch view, the console, and the tooltip can only see variables that are local or upvalues to the function blocks. This is only an issue with functions and variables defined in the outer scope.