Log in

View Full Version : Help with Tables in a Table (like a Dictionary)



tdewitt274
January 3rd, 2011, 13:20
I was wondering if someone could help me with a problem with adding a table to another table. I've seen something similar in the Lua tutorials as t[2] = { "apple", "pear", "banana" }.

In a nutshell, the below code generates a listing of the cards for each player around the table. Then, the values are to be put into the "players" table within the "hands" (in essence, players[1 to n].hand[MaxCards]).


function packDeal(params)
MaxSeats = packTable.MaxPlayers;
if #players < packTable.MaxPlayers then MaxSeats = #players end
local hands = {} ;
for p = 1, MaxSeats*tonumber(params) do
table.insert(hands,cards[1]);
table.remove(cards,1);
end
for i = 1, #hands do
CurrCount = 0;
while CurrCount <= MaxSeats do
CurrCount = CurrCount + 1;
-- XXXXX
table.insert(players[i].hand,{ hands[i * CurrCount] });
end
end
end

I receive the following error on the line indicated with the --XXXXX
Script Error: [string "scripts/chatmanager.lua"]:542: attempt to index field '?' (a nil value)

I've tried the code a few different ways, but I can't seem to get it to work. Any suggestions?

Thanks in advance!

drahkar
January 3rd, 2011, 14:13
I'm positive that the problem is caused by the way the reference is handled here: players[i].hand,{ hands[i * CurrCount] }

Could you paste a snippet of the table format you are going for? How that line needs to be written is entirely dependent on that.

tdewitt274
January 3rd, 2011, 14:23
Not the most elegant, but here it is:



function packBuyIn(params)
table.insert(players,packTable.CurrPlayers + 1, {
name = "player"..#players+1,
chips = params,
value = value,
xcoord = 0,
ycoord = 0,
hand = {}
});
local msg = {};
msg.text = "Player "..players[#players].name.." has joined the game.";
deliverMessage(msg);
end

Thanks!

tdewitt274
January 3rd, 2011, 14:55
I guess it would help if you had the cards. I don't have the code to a decent point, but here's kind of what I'm using.


cards = { ['A'] = "Ace", ['10'] = "Ten", ['K'] = "King", ['Q'] = "Queen", ['J'] = "Jack", ['9'] = "Nine" }

If you need more, let me know and I'll straighten things up and include the file.

Thanks!

drahkar
January 4th, 2011, 14:57
I have a similar problem in my own code that I'm working through. I'll post when I find a workable solution. I'm still certain it has to do with the formatting of that line.

tdewitt274
January 4th, 2011, 21:26
I agree. It just seems odd. I've tried worse looking attempts as well to no avail.

drahkar
January 5th, 2011, 00:51
Have your tried, instead of using the object oriented form, doing something like:

table.insert(players[i][hand], hands[i * CurrCount]);

tdewitt274
January 5th, 2011, 01:21
That seems to be the issue! I thought I tried that. Must have been when I was screwing up some other piece ;)

Thanks a lot!

drahkar
January 5th, 2011, 02:13
Glad I could help! :)

tdewitt274
January 5th, 2011, 15:24
Just wanted to say thanks again! And to post what I did to make it work. Hope this helps anyone that's trying to work with dictionaries.


function packDeal(params)
MaxSeats = packTable.MaxPlayers;
if #players < packTable.MaxPlayers then MaxSeats = #players end
local hands = {} ;
CurrSeat = 1
for p = 1, MaxSeats do
hands[p] = {};
end
for p = 1, MaxSeats*tonumber(params) do
table.insert(hands[CurrSeat],cards[1]);
table.remove(cards,1);
CurrSeat = CurrSeat + 1;
if CurrSeat > MaxSeats then CurrSeat = 1 end;
end
for i = 1, #hands do
players[i]['hand'] = hands[i];
print(players[i]['hand'][1].name);
end
end

Looks to be that my problem was when I was diving into the "subtable". I can iterate through the "players" table using an index, but if you have another table within, you need to use the named reference for the index in order to get the information within the table.

I'm sure I can clean up the code now to just insert into the original table, but why mess with a good thing ;) Besides, I need to see if it can handle multiple players with ease.

Again, thanks!