Scripts / Lua help: Empty tables inside empty tables?

Author
Message
Moshroom
9
Years of Service
User Offline
Joined: 18th Jul 2015
Location: Finland
Posted: 24th Jan 2017 14:50
I'm trying to create a complex database that can be filled from TXT files. I have a bit similar type of idea in my old set of RPG scripts, but there I used several different tables and many if-elses to fill each separately. This time I'm trying to save rows of code by filling the database with as few if-elses as possible.

First of all, I know how to create a table inside a table:

However this doesn't seem to work:

The game thinks the table entry doesn't exist.

And if I do this:

It gives no error message, but the game just freezes.

Is there something fundamentally wrong with my idea or do I just need to go through my code to look for typos? (The actual script is a bit more complicated than the one I typed here. Just want to make sure I'm doing the basic idea correctly before intense typo-search of the actual code...)
smallg
Community Leader
19
Years of Service
User Offline
Joined: 8th Dec 2005
Location:
Posted: 24th Jan 2017 15:00
I always do it like this
Table = {}
Table[e] = {}

Table[e][1] = "string1"
lua guide for GG
https://steamcommunity.com/sharedfiles/filedetails/?id=398177770
windows 10
i5 @4ghz, 8gb ram, AMD R9 200 series , directx 11
Moshroom
9
Years of Service
User Offline
Joined: 18th Jul 2015
Location: Finland
Posted: 24th Jan 2017 15:01 Edited at: 24th Jan 2017 15:06
Ok, so my idea should be kind of right, and I need to look for solution later in my script. Thanks.

Edit: Just for the record: My problem was that I had accidentally made the code try to read entry "0" instead of "1". Since "0" is nil, the game froze.
AmenMoses
GameGuru Master
8
Years of Service
User Offline
Joined: 20th Feb 2016
Location: Portsmouth, England
Posted: 25th Jan 2017 20:15
Lua lists are absolutely awesome but it does take a while to get your head around them, just remember that you must tell Lua you want a list before you try to use it as one with ={} or you are going to have problems.

btw '0' is perfectly acceptable as a key, but 0 is not, by this I mean that under the covers a list with no defined key values is given values 1 .. n so they act like arrays but the important thing to keep in mind is that they are not arrays.
Been there, done that, got all the T-Shirts!
PM
Moshroom
9
Years of Service
User Offline
Joined: 18th Jul 2015
Location: Finland
Posted: 25th Jan 2017 21:19
Yes I know. I have some tables with data behind negative keys as well as positive ones. It requires manual addition of the keys, but offers an easy way to divide the entries into distinct categories that will be handled differently by the functions that read them. In case I need to add more entries later, this saves me from needing to move the positions of existing data in the table or creating any execeptions to a reading rule.

Login to post a reply

Server time is: 2024-12-23 03:28:01
Your offset time is: 2024-12-23 03:28:01