Scripts / Table Questsions

Author
Message
MChapman
9
Years of Service
User Offline
Joined: 19th May 2015
Location: USA
Posted: 19th Oct 2016 04:58
What I was wondering is how do you access A table in a table? What I'm trying to do is make A table that will hold all the data of the villagers something like.



I can't simply put villagers_npcs[1][1] to access the first key of village_npcs and the first key of npc_one... for npc_one's name... I seen a good example of this on a post by AmenMoses but I have no idea where I saw it at. It was some of his move object script I think, I've looked where I though I seen it but to no prevail.
Markchapman10 is my Skype let's have some dev talk.
AmenMoses
GameGuru Master
8
Years of Service
User Offline
Joined: 20th Feb 2016
Location: Portsmouth, England
Posted: 19th Oct 2016 19:56 Edited at: 19th Oct 2016 19:59


You can search the list something like this:

Been there, done that, got all the T-Shirts!
PM
MChapman
9
Years of Service
User Offline
Joined: 19th May 2015
Location: USA
Posted: 20th Oct 2016 01:41 Edited at: 20th Oct 2016 02:02
even better! thanks AmenMoses this should help to others working on tables as well. Your a life saver.


edit: when you use the return function instead of the villager name it will have "not found" instead?
Markchapman10 is my Skype let's have some dev talk.
AmenMoses
GameGuru Master
8
Years of Service
User Offline
Joined: 20th Feb 2016
Location: Portsmouth, England
Posted: 20th Oct 2016 09:15
Yes, basically if the for loop completes then it didn't find a match so it will fall into the return statement and return "not found".

If you want to see some heavy table usage check out my ezplatform.lua thread, now that script is becoming a beast!

Been there, done that, got all the T-Shirts!
PM
MChapman
9
Years of Service
User Offline
Joined: 19th May 2015
Location: USA
Posted: 22nd Oct 2016 03:23
sorry another question you put the npc_one as a string, so strings can be variables? Is this also how the states work in the AI scripts?
Markchapman10 is my Skype let's have some dev talk.
AmenMoses
GameGuru Master
8
Years of Service
User Offline
Joined: 20th Feb 2016
Location: Portsmouth, England
Posted: 22nd Oct 2016 11:12 Edited at: 22nd Oct 2016 11:13
Yes, keys can be anything and you can put anything in a list.

DaysOfWeek = {"Mon","Tue","Wed","Thu","Fri","Sat","Sun"}

This is a list with no keys specified so Lua simply allocates keys starting at 1 so DaysOfWeek[1] = Mon.

This looks like an array but it isn't, it is the same as:

DaysOfWeek = {[1] = "Mon", [2] = "Tue" etc}

Note that the keys can be anything, e.g. DaysOfWeek['Sunday'] = "Sun" is quite valid and mucks up the 'array' because it isn't added as DaysOfWeek[8] like you may imagine, in fact there is no [8] key in the list.

The proper way to iterate lists is to use the functions 'pairs' and 'ipairs', pairs(list) iterates in the order the list is created whereas ipairs(list) iterates in order of the keys. Both return the key and value, if you don't care about one or the other put '_' instead, for example:

local days_string = ""

for _, Day in ipairs(DaysOfWeek) do
days_string = days_string .. Day
end

Would result in days_string containing "MonTueWedThuFriSatSun"
Been there, done that, got all the T-Shirts!
PM

Login to post a reply

Server time is: 2024-12-22 22:17:33
Your offset time is: 2024-12-22 22:17:33