lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


Okay, so can anyone tell me where I am going wrong here:

-- 'key' is a userdata which is created in my C++ code.
function set_visible(key, visible)
	if visible then 
		all_visible[key] = true
	else 
		all_visible[key] = nil
	end

	-- Show some output for debugging
	for k,v in pairs(all_visible) do
		print(k,v)
	end
end

-- Objects is a table that contains other tables of which the field
'key' is the same sort of userdata
function show_all_visible()
	-- Show some output for debugging.  Verify that the all_visible
table is consistant between f() calls
	for k,v in pairs(all_visible) do
		print(k,v)
	end

	for i, v in ipairs(objects) do
		print(v.key, all_visible[v.key])
		if all_visible[v.key] == true then
			show_stuff(v)
		end
	end
end

local key = 'some userdata'

set_visible(key, true)
show_all_visible()

What I was expecting to happen is the all_visible[key] value to be set
to true, and the show_stuff() call to be made for the object with that
key.  However, this never happened.  The debug output showed that
all_visible[key] was set to true in set_visible, and was still true when
outputting that table at the start of show_all_visible(), however the
output in the ipairs loop in show_all_visible was showing
all_visible[v.key] to be nil.

So what special rules are there for using userdata as a key in a table?
Is it actually allowed?  Am I being a complete numpty?

(I got round the problem, by calling tostring(key) everywhere I used it
as a key, but I don't like that as a solution)


 
---------------------------------------------------------------------------- 
         DISCLAIMER 
This email is sent by The Creative Assembly Limited company No. 03425917, registered in England &Wales registered office 27 Great West Road, Middlesex, TW8 9BW, England. The contents of this e-mail and any attachments are confidential to the intended recipient and may also be legally privileged. Unless you are the named addressee (or authorised to receive for the addressee) of this email you may not copy, disclose or distribute it to anyone else. If you have received this email in error, please notify us immediately by e-mail on postmaster@creative-assembly.co.uk and then delete the email and any copies. The Creative Assembly Limited have made all reasonable efforts to ensure that this e-mail and any attached documents or software are free from software viruses, but it is the recipient's responsibility to confirm this.