Skip to main content
gamedev.net
🔒 Locked

Lua table problem

Started by Stonicus Sep 1, 2005 at 12:19 PM 3 replies 800+ views
Original Post
Stonicus
Stonicus
I have a lua script that is maintaining information about merging objects: merge a glass with a can of coke, you'll get a glass of coke.

MergeData = {};

function AddMergePair(ob1, ob2, ob3, del1, del2)
	-- See if we have any entries for ob1 already...
	if(MergeData[ob1] == nil) then
		-- No entries for ob1, so let's make one and put the data in it
		MergeData[ob1] = {};
        end

        -- entry for ob1 is guarateed to exist, so put in data for ob2...
	MergeData[ob1][ob2] = {ob3, del1, del2};
end
ob1 = string - first object to merge ob2 = string - second object to merge ob3 = string - object to add to player when ob1 and ob2 are merged del1 = bool - do we delete ob1 when we merge del2 = bool - do we delete ob2 when we merge The problem, is that it nevers seems to find that an entry already exists... AddMergePair("chemical", "water", "healingpotion", true, true); this should say "when you merge water onto a chemical, make a healing potion". So after I make the first "chemical", it registers itself into the merge table, and all is well. But if I create a second one (each ob registeres itself on create), then I end up with 2 entries in the MergeData table with a key of "chemical", both with identical data. Is there a better way of checking if a key-value pair already exists? Actually, I don't need to value, just if a key already exists, then add to it's value (which will be another table) and not make a new entry with a duplicate key. I am new to LUA, so I apologize if this is a newbie or dumb question or if my terminology is off.
Sneftel
Sneftel
Quote:
Original post by Stonicus
But if I create a second one (each ob registeres itself on create), then I end up with 2 entries in the MergeData table with a key of "chemical", both with identical data.

That's not possible. Lua tables are utterly incapable of storing multiple key-value pairs with the same key. How are you inspecting to see whether this is the case?
Stonicus
Stonicus
AddMergePair("chemical", "water", "healingpotion", true, true);
AddMergePair("chemical", "water", "healingpotion", true, true);

If I run those functions back to back, then do 'table.foreach(MergeData, print);' the output is:

chemical table: 03A483C8
chemical table: 03AC12A0

The MergeData now has two keys with value "chemical", and if I further did table.foreach(t, print) on the tables of those keys, they have the same data in them.


Sneftel
Sneftel
Hm. I just tried the code you posted, and it worked fine. Have you done strange low-level memory things with the VM? Did you actually cut and paste the code you ran into this thread, or is that merely "representative" code?

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.