Jump to content
  • Advertisement
Sign in to follow this  
Stonicus

Lua table problem

This topic is 4821 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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.

Share this post


Link to post
Share on other sites
Advertisement
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?

Share this post


Link to post
Share on other sites
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.


Share this post


Link to post
Share on other sites
Guest Anonymous Poster
I would store merge indices with the string in a separate table.
The object types would exist in their own table, such as:

object_types = { "chemical", "water", "coke", "glass" }
object_instances = {}

and the object instances would be stored in a separate table.
The the merge indexes are stored such that merging table value 3 and table value 4 would be a "glass of coke".



each element of the MergeData table would look something like:-

merge_elem = { "glass of coke", 3, 4 }

so to merge you just index the two object types. The problem is obviously with searching over the merge data table to find how to merge, as the algorithms would be computationally expensive. It would be possible to store all mergeing items in each object by index too.
hope this helps

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!