Register forum user name Search FAQ

Gammon Forum

Notice: Any messages purporting to come from this site telling you that your password has expired, or that you need to verify your details, confirm your email, resolve issues, making threats, or asking for money, are spam. We do not email users with any such messages. If you have lost your password you can obtain a new one by using the password reset link.

Due to spam on this forum, all posts now need moderator approval.

 Entire forum ➜ MUSHclient ➜ Lua ➜ Problems with tables

Problems with tables

It is now over 60 days since the last post. This thread is closed.     Refresh page


Posted by Katherina   (19 posts)  Bio
Date Wed 30 Jul 2008 04:37 PM (UTC)
Message
Hi,
I have read what I can find in the forum, gone to lua.org, gone to other lua forums and I can't figure out what I'm doing wrong or how to do what I want to.
I'm rewriting my combat system for aetolia, what I'm trying to do is have an alias that allows you to add certain data to a table. In this case it's keeping track of weapons. The fields are character name, weapon type, which hand and weapon number. I can enter these into the table fine but if I go to add a second weapon it's not appending to the table but overwriting it. I tried working with insert and get all kinds of errors, what is working is with the table name being weapons.
weapons.name = charname
weapons.type = temptype
weapons.hand = weaponhand
weapons.number = tempnumber
that adds it fine but it's overwriting the other records. Now in my script file I have weapons = { } to initialize the table, is it erasing the contents of the table? If so how do I keep the data in the table and load it when I start mushclient?

The second issue is, if the data is being over written my buffer tables are no use to me and I just spent three days coding for nothing. I have afflictions, defenses and so forth going into a table to temporarily store the variable, then on the prompt set the variables and clear the table, however there may be up to four afflictions before a new prompt so if it's over writing i'm only going to be storing the last affliction I need all of them.

Please help I'm very frustrated and having a hard time finding examples I can understand or anything that really relates.
Top

Posted by David Haley   USA  (3,881 posts)  Bio
Date Reply #1 on Wed 30 Jul 2008 05:38 PM (UTC)
Message
What you actually want is a table of tables, not just one table. The outer table is a list of tables, each of which is a weapon record.

So, you want something like this:


-- create the list of weapons
weapons = {}

-- create one weapon record
weapon = {}

-- add it to the list
table.insert(weapons, weapon)

-- populate its values
weapon.field1 = val1
weapon.field2 = val2
weapon.field3 = val3
-- etc.


Every time you run "weapons = {}" you are clearing the list of weapons, which is probably not what you want to do.

As for saving and loading the data, Nick has some serialization functions that do the job very nicely. I don't remember the link off-hand, but there's a page in the forums that gives several examples on how to save/load data from/into Lua.

David Haley aka Ksilyan
Head Programmer,
Legends of the Darkstone

http://david.the-haleys.org
Top

Posted by Nick Gammon   Australia  (23,133 posts)  Bio   Forum Administrator
Date Reply #2 on Wed 30 Jul 2008 08:48 PM (UTC)
Message
See this post about serializing Lua tables into a string:

http://www.gammon.com.au/forum/?id=4960

It only take a few lines of code, and a huge table can be saved and reloaded next time.

- Nick Gammon

www.gammon.com.au, www.mushclient.com
Top

The dates and times for posts above are shown in Universal Co-ordinated Time (UTC).

To show them in your local time you can join the forum, and then set the 'time correction' field in your profile to the number of hours difference between your location and UTC time.


13,187 views.

It is now over 60 days since the last post. This thread is closed.     Refresh page

Go to topic:           Search the forum


[Go to top] top

Information and images on this site are licensed under the Creative Commons Attribution 3.0 Australia License unless stated otherwise.