A table inside a table.... inside a table.

Hello, I’m trying to make a dice game and it requires that I have:
1) A table of all quests (The case which is going on in the game)

2) A table of results (Depends on what the dice rolled)

3) A table of awards (Like adding tools, remembering values, names to another table)

Right now it looks like this:

local questBank = { {    "Quest info";    "Rolled 1-3";     "Rolled 4-6";     "Rolled 7-9";     "Rolled 10";     AftermathFor13 = {name = "Eric", dad = "Gone"};     AftermathFor46 = {name = "Joseph"; dad = "Kind"};     AftermathFor79 = {name = "Albert"}; dad = "Millionaire";     AftermathFor10 = {name = "Kim", dad = "Korean"} }; "someotherquestsWithTables" }

So what I need to do is to get the TABLE itself and insert it into another table.

Let’s assume I rolled 7

--Text changes to: box.text = questBank[1][4] -- First quest, fourth string library = {} table.insert(library, questBank[1][8]) --  adding the AftermathFor79 table into library

The thing is, it may not only be name. So I need both name = “Albert” and dad’s status to be inserted.

It gives me nil all the time. Any help?

I haven’t understand everything.

If you want a table inside a table you have to do:

table={} table.table2={} table.table2["table3"]={} table.table2["table3"][1]={}

like this you have table2 inside table. Table3 is inside table2 and 1 is inside table3.

if you want many table inside a table:

table={} for i=0,100 do table[i]={} for j=8,1000 do table[i][j]={} end end

You’re AftermathFor79 entry is not located at an integer index but at the string key AftermathFor79

So you have to access your questBank like so

[lua]

table.insert(library, questBank[1][“AftermathFor79”]) –  adding the AftermathFor79 table into library

or

table.insert(library, questBank[1].AftermathFor79) –  adding the AftermathFor79 table into library

[/lua]

Thanks, it made me do some real progress.

Although there is a problem. I can’t make it check for which one of the third table keys is being transferred, so it’s better to, somehow, get the table not with questBank[1].AftermathFor79, but questBank[1][6], (where 6 is the # for AftermathFor79) because there might be 4 different keys there.

library[1]=questBank[1][8] like this you know the index of the table.

There is a probleme in your table.

at this line:  AftermathFor79 = {name = “Albert”}; dad = “Millionaire”;

it’s missing “}” at the end and the “}” is at the wrong place

sometime you use “;” and other “,” that s very strange

Then you have to change the way you’ve built your questBank as, like it is now, there is no integer index for any of your Aftermath* lines as the string itself is the index.

I.e. your table actually looks like

[lua]

questBank[1][1] = “Quest info”

questBank[1][2] = “Rollded 1-3”

questBank[1][3] = “Rolled 4-6”

questBank[1][4] = “Rolled 7-9”

questBank[1][5] = “Rolled 10”

questBank[1][“AftermathFor13”] = {name = “Eric”, dad = “Gone”};

questBank[1][“AftermathFor46”] = {name = “Joseph”; dad = “Kind”};

questBank[1][“AftermathFor79”] = {name = “Albert”; dad = “Millionaire”};

questBank[1][“AftermathFor10”] = {name = “Kim”, dad = “Korean”}

[/lua]

So if you want to access the Aftermath tables by an index you’d have to change your questBank and then you’ll be able to access everything by an integer as you originally did.

[lua]

local questBank = {

{

    “Quest info”;

    “Rolled 1-3”;

    “Rolled 4-6”;

    “Rolled 7-9”;

    “Rolled 10”;

    {name = “Eric”, dad = “Gone”};

    {name = “Joseph”; dad = “Kind”};

    {name = “Albert”; dad = “Millionaire”};

    {name = “Kim”, dad = “Korean”}

};

[/lua]

I don’t know you exact needs but a probably better way to structure your data is maybe something like

[lua]

local questBank = {

{

    “Quest info”;

    { text = “Rolled 1-3”, aftermath = {name = “Eric”, dad = “Gone”} },

    { text = “Rolled 4-6”, aftermath = {name = “Joseph”, dad = “Kind”} },

    { text = “Rolled 7-9”, aftermath = {name = “Albert”, dad = “Millionaire”} },

    { text = “Rolled 10”, aftermath = {name = “Kim”, dad = “Korean”} },

};

box.text = questBank[2].text

library = {}

table.insert( library, questBank[2].aftermath )

[/lua]

I haven’t understand everything.

If you want a table inside a table you have to do:

table={} table.table2={} table.table2["table3"]={} table.table2["table3"][1]={}

like this you have table2 inside table. Table3 is inside table2 and 1 is inside table3.

if you want many table inside a table:

table={} for i=0,100 do table[i]={} for j=8,1000 do table[i][j]={} end end

You’re AftermathFor79 entry is not located at an integer index but at the string key AftermathFor79

So you have to access your questBank like so

[lua]

table.insert(library, questBank[1][“AftermathFor79”]) –  adding the AftermathFor79 table into library

or

table.insert(library, questBank[1].AftermathFor79) –  adding the AftermathFor79 table into library

[/lua]

Thanks, it made me do some real progress.

Although there is a problem. I can’t make it check for which one of the third table keys is being transferred, so it’s better to, somehow, get the table not with questBank[1].AftermathFor79, but questBank[1][6], (where 6 is the # for AftermathFor79) because there might be 4 different keys there.

library[1]=questBank[1][8] like this you know the index of the table.

There is a probleme in your table.

at this line:  AftermathFor79 = {name = “Albert”}; dad = “Millionaire”;

it’s missing “}” at the end and the “}” is at the wrong place

sometime you use “;” and other “,” that s very strange

Then you have to change the way you’ve built your questBank as, like it is now, there is no integer index for any of your Aftermath* lines as the string itself is the index.

I.e. your table actually looks like

[lua]

questBank[1][1] = “Quest info”

questBank[1][2] = “Rollded 1-3”

questBank[1][3] = “Rolled 4-6”

questBank[1][4] = “Rolled 7-9”

questBank[1][5] = “Rolled 10”

questBank[1][“AftermathFor13”] = {name = “Eric”, dad = “Gone”};

questBank[1][“AftermathFor46”] = {name = “Joseph”; dad = “Kind”};

questBank[1][“AftermathFor79”] = {name = “Albert”; dad = “Millionaire”};

questBank[1][“AftermathFor10”] = {name = “Kim”, dad = “Korean”}

[/lua]

So if you want to access the Aftermath tables by an index you’d have to change your questBank and then you’ll be able to access everything by an integer as you originally did.

[lua]

local questBank = {

{

    “Quest info”;

    “Rolled 1-3”;

    “Rolled 4-6”;

    “Rolled 7-9”;

    “Rolled 10”;

    {name = “Eric”, dad = “Gone”};

    {name = “Joseph”; dad = “Kind”};

    {name = “Albert”; dad = “Millionaire”};

    {name = “Kim”, dad = “Korean”}

};

[/lua]

I don’t know you exact needs but a probably better way to structure your data is maybe something like

[lua]

local questBank = {

{

    “Quest info”;

    { text = “Rolled 1-3”, aftermath = {name = “Eric”, dad = “Gone”} },

    { text = “Rolled 4-6”, aftermath = {name = “Joseph”, dad = “Kind”} },

    { text = “Rolled 7-9”, aftermath = {name = “Albert”, dad = “Millionaire”} },

    { text = “Rolled 10”, aftermath = {name = “Kim”, dad = “Korean”} },

};

box.text = questBank[2].text

library = {}

table.insert( library, questBank[2].aftermath )

[/lua]