-
January 3rd, 2011, 13:20 #1
- Join Date
- Oct 2009
- Location
- GMT -6 hrs
- Posts
- 503
Help with Tables in a Table (like a Dictionary)
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]).
Code: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
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!
-
January 3rd, 2011, 14:13 #2
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.- Trying to find a community Ruleset, Extension or Information on how to complete a process? Try FGRepository.
- PM Myself if you have new or updated community Rulesets, Extensions or Information that you'd like added to FGRepository.
-
January 3rd, 2011, 14:23 #3
- Join Date
- Oct 2009
- Location
- GMT -6 hrs
- Posts
- 503
Not the most elegant, but here it is:
Code: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
-
January 3rd, 2011, 14:55 #4
- Join Date
- Oct 2009
- Location
- GMT -6 hrs
- Posts
- 503
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.
Code:cards = { ['A'] = "Ace", ['10'] = "Ten", ['K'] = "King", ['Q'] = "Queen", ['J'] = "Jack", ['9'] = "Nine" }
Thanks!
-
January 4th, 2011, 14:57 #5
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.
- Trying to find a community Ruleset, Extension or Information on how to complete a process? Try FGRepository.
- PM Myself if you have new or updated community Rulesets, Extensions or Information that you'd like added to FGRepository.
-
January 4th, 2011, 21:26 #6
- Join Date
- Oct 2009
- Location
- GMT -6 hrs
- Posts
- 503
I agree. It just seems odd. I've tried worse looking attempts as well to no avail.
-
January 5th, 2011, 00:51 #7
Have your tried, instead of using the object oriented form, doing something like:
table.insert(players[i][hand], hands[i * CurrCount]);- Trying to find a community Ruleset, Extension or Information on how to complete a process? Try FGRepository.
- PM Myself if you have new or updated community Rulesets, Extensions or Information that you'd like added to FGRepository.
-
January 5th, 2011, 01:21 #8
- Join Date
- Oct 2009
- Location
- GMT -6 hrs
- Posts
- 503
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!
-
January 5th, 2011, 02:13 #9
Glad I could help!
- Trying to find a community Ruleset, Extension or Information on how to complete a process? Try FGRepository.
- PM Myself if you have new or updated community Rulesets, Extensions or Information that you'd like added to FGRepository.
-
January 5th, 2011, 15:24 #10
- Join Date
- Oct 2009
- Location
- GMT -6 hrs
- Posts
- 503
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.
Code: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
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!
Thread Information
Users Browsing this Thread
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks