FG Spreadshirt Swag
  1. #1

    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
    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!

  2. #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.

  3. #3
    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
    Thanks!

  4. #4
    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" }
    If you need more, let me know and I'll straighten things up and include the file.

    Thanks!

  5. #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.

  6. #6
    I agree. It just seems odd. I've tried worse looking attempts as well to no avail.

  7. #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.

  8. #8
    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!

  9. #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.

  10. #10
    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
    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!

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
5E Product Walkthrough Playlist

Log in

Log in