lua-users home
lua-l archive

Re: Using full userdata as table index?

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


>> Could you possibly be more specific about the problem?
Yes--just didn't want to flood a bunch of code onto the list before I
knew it should work. :)
-- prints contents of a table for debugging purposes
function printtable( aTable )
	print( "@@@@@@@@@@@@@@@@" )
	for i, v in pairs( aTable ) do
		print( i, v )
	end
	print( "@@@@@@@@@@@@@@@@" )
end
-------------------------------------------
Basically, I want to use a userdata object as the index for a lookup
table of strings. 
-- Setup the lookup table.
function SetupTable()
	tObject2String = {}
	for count, stringID in pairs( tStrings ) do
		-- return a numeric id to the actor object
		local obj = Utility_GetActor( stringID ) 
		-- associate it with a string
		tObject2String[obj] = stringID 
		-- this is how I want to use the table later,
		-- and for now, this outputs the right information
		print( '--> Adding ' .. tObject2String[obj] .. '\n' )
	end
	
	-- this prints out the contents of the table, and is also
correct	
	printtable( tObject2String )
end
OUTPUT
--> Adding targetVehicle1
--> Adding targetVehicle2
--> Adding targetVehicle3
--> Adding targetVehicle4
--> Adding targetVehicle5
@@@@@@@@@@@@@@@@
guid: 0x679676de targetVehicle1
guid: 0x44cfa3c6 targetVehicle3
guid: 0x33678cd6 targetVehicle5
guid: 0xf015e098 targetVehicle2
guid: 0x4f580514 targetVehicle4
@@@@@@@@@@@@@@@@
-------------------------------------------
However, when I try to lookup a value in the table, it returns nil.
I know that the table is unchanged since its creation and that the aObj
argument exists in the table. If I do a linear search of the
tObject2String table using aObj, I find the object. I also tried using
rawget and rawset to read/write values from the lookup table because I
thought the index metamethod might have been confused, but that did not
work either.
--look up the string and use it ...
function Lookup( aObj )
	printtable( tObject2String )
	
	local stringID = tObject2String[aObj]
	print(aObj, stringID)
	...
End
OUTPUT
@@@@@@@@@@@@@@@@
guid: 0x679676de targetVehicle1
guid: 0x44cfa3c6 targetVehicle3
guid: 0x33678cd6 targetVehicle5
guid: 0xf015e098 targetVehicle2
guid: 0x4f580514 targetVehicle4
@@@@@@@@@@@@@@@@
guid: 0x679676de nil

AltStyle によって変換されたページ (->オリジナル) /