Log in

View Full Version : Same table has two different readings of it's properties?



Mastyrial
March 10th, 2022, 14:00
You can ignore this thread.

I have a set of errors which seem very strange to me.


I have "ItemRepresentation" windows, which are entries in a windowlist. The windows have following script file attached to them.
Please note the three Debug statements, they will be important in my error description later on.
I am working with a configurable callback table: The table indicates a "source" and a "function" to be called on that source.


local tCallbackData; -- table with tCallbackData["source"] = windowinstance and tCallbackData["function"] = string to indicate the function to call on the source
local visibility = true;

function setUp(tCallbackDataIn)
tCallbackData = tCallbackDataIn; -- save it for later
end

function foo()
Debug.chat(tCallbackData); -- Debug 2

-- some code unrelated to tCallbackData

if(tCallbackData ~= nil) then
Debug.chat(tCallbackData); -- Debug 1
local callbackSource = tCallbackData["source"]; --source here: the windowinstance of a specific instance of a ItemRepresentation window (basically "self")
callbackSource[tCallbackData["function"]](); -- function to call: "hideInWindowList" (see below)
end
end

function bar()
-- do something
Debug.chat(tCallbackData); -- Debug 3
foo();
end

function hideInWindowList()
visibility = false;
windowlist.applyFilter();
end

-- follwoing function is used for the filter of the windowlist; non-visible entries are filtered out and therefore "hidden"
function isVisible()
return visibility;
end



I have following problem:
Debug 3 gives me following:
{ s'source' = windowinstance = { class = ItemRepresentation, node = charsheet.id-00001.items.onbody.weapons.hammer1h-25, x,y,w,h = 300,0,300,290 }, s'function' = s'hideInWindowList' }
Debug 1 gives me:
{ s'source' = userdata, s'function' = s'hideInWindowList' }

So the windowinstance is no longer recognised as a windowinstance, but is now some generic user data?

... and Debug 2 (in the same function as Debug 1, without any changes to tCallbackData in between) gives me:
{ s'source' = windowinstance = { class = ItemRepresentation, node = charsheet.id-00001.items.onbody.weapons.hammer1h-25, x,y,w,h = 300,0,300,290 }, s'function' = s'hideInWindowList' }

Why is tCallbackData["source"] a windowinstance in bar() and in first part of foo(), but an userdata in second part of foo()?
I get an error because of this: "Attempt to index local 'callbackSource' (a user data value)"

Second problem:
During debugging, I first wrote "Debug 1" (and no other lines with Debug). Fantasy Ground gave me the error that Debug is nil. I could not believe that my function was somehow out of scope of the fantasy grounds framework? Then I wrote Debug 2 in addition to Debug 1. I did not change anything else. When I run this, both Debugs were working...
What is happening? Did lua or fantasy ground break?

Third problem:
While there is an error with tCallbackData's user data in foo(), it is still executed. In the current use case, the tCallbackData calls the function "hideInWindowList()". Basically it sets the attribute visibility to false and let the windowlist filter on that attribute -> if the tCallbackData part is executed, the window is hidden from the user.
While I get the error "Attempt to index local 'callbackSource' (a user data value)", the window gets filtered out.

I do not know why the table is read differently? (windowinstance in one case, user data in another case)
I do not know why Debug was not found in the first place, then found after I just wrote another line with it? (first time happening to me)
I do no know why my code is executed while having an error (also a first timer; usually an error means aborting the rest of the function)

Mastyrial
March 10th, 2022, 18:04
I fixed it by moving one line of unrelated (!) code from the middle of foo() at the end of foo() ...
I don't know how this fixed it, but it did.

Moon Wizard
March 10th, 2022, 20:40
The difference between getting userdata and an FG object has to do with timing, if you are creating or deleting objects.

Regards,
JPG

Mastyrial
March 11th, 2022, 09:16
That is nice to know. Thank you :)