Watch window cannot find variables which aren't used by the current function environment

The watch window fails to read variables which aren’t used by the current function. Here’s a very simple example:

As you can see, the variable beta exists and is set to true. The function doesn’t use it, and therefore the watch window fails to get the value. alpha IS used, and the watch window is able to read it. Here’s a more detailed example:

--[[
	Inside of functions, the watch window can only search for upvalues.
	To my knowledge, upvalues are variables that a function needs to work as expected.
	They are local values that appear up above the function, hence the name.
]]

local alpha = true
local beta = true

local function test()
	print(alpha) -- If you watch for 'beta' here, it CAN NOT find the variable
	--print(beta)
end

test() -- If you watch for 'beta' here, it CAN find the variable

if alpha then
	print("Hello world!") -- If you watch for 'beta' here, it CAN find the variable
end

This behavior happens in all my games.

Something important to note is that this behavior is NOT occurring with native code generation. Using this feature, the bug disappears, and the watch window works as expected!


OS: Windows 11
VERSION: 0.627.0.6270453
FILE: Reproduction.rbxl (52.8 KB)

Expected behavior

I expect the watch window to be able to read every variable that has been currently assigned, in the current environment.
I also expect the variables tab to show all the declared variables. Currently, it doesn’t show ones it cannot find.

3 Likes

Discovered something odd…
Including getfenv() somehow makes native code generation experience the bug.

Code
--!native
local alpha = true
local beta = true

local function test()
	print("Hello world!") --> beta is 'true' now
	--print(getfenv()) --> UNCOMMENTED: beta is 'nil' now
	--print(getfenv().beta) --> UNCOMMENTED: beta is 'nil' now
end

test()
Images

1 Like

Hi,

The interpreter optimizes the code so unused variables don’t exist in running scripts to reduce memory (and CPU) use. This means watches and Logpoints won’t work for unused variables.

The solution would be to add a Setting to disable this optimization. This would be a feature request though, as it would potentially bloat memory use and maybe slow execution.

Alternatively you can add “noop” lines to include these variables in the execution.

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.