Hello Guest it is April 28, 2024, 06:22:48 PM

Author Topic: Why is my function failing with a nil value, but works when I step through?  (Read 84 times)

0 Members and 1 Guest are viewing this topic.

I have made a module file 'PocketPositions.Lua' with a bunch of functions.

In the screen load script I added the following:
Code: [Select]
-- Pocket Positions
package.loaded.PocketPositions = nil
PocketPositions = require "PocketPositions"

From a tab On enter script I have:
Code: [Select]
local pocketNums = PocketPositions.GetUsedPocketNumbers()
if pocketNums[1] ~= nil then
wx.wxMessageBox("pocketNums[1] ="..tostring(pocketNums[1]))
table.sort(pocketNums)
local pocketStr = ""
for i=1, #pocketNums do
pocketStr = pocketStr .. pocketNums[i] .. "|"
end
wx.wxMessageBox("pocketStr="..pocketStr)
scr.SetProperty("GROUPBOX_CurrentToolPocket", "Strings", pocketStr)
else
wx.wxMessageBox("pocketStr=nil")
end

the function 'PocketPositions.GetUsedPocketNumbers()' looks like:
Code: [Select]
-- Gets a table of the pocket numbers that are configured
function PocketPositions.GetUsedPocketNumbers()
local PocketNums={}
local n=0

-- make sure the pockets have been loaded
if PocketDetails == nil or PocketDetails[0] == nil then
PocketPositions.ReadPocketData()
end

for k,v in pairs(PocketDetails) do
  n=n+1
  PocketNums[n]=k
end

return PocketNums
end

When it calls the function 'PocketPositions.ReadPocketData()' (which is also in the  'PocketPositions.Lua' file) I get an error that 'PocketPositions' is null.

If I step through 'PocketPositions.ReadPocketData()' in the script editor it works fine.

Any idea what's happening and how I fix it?