Results 1 to 2 of 2

Thread: Why doesn't this code get the last card ? )

  1. #1

    Why doesn't this code get the last card ? )

    I've got a script zone which I iterate for objects and if I find a deck I do processing on the cards in that deck.
    The processing of the cards in the deck object puts the GUIDS of cards I am interested in into a table called cards_to_cash_in
    The GUIDS may or may not be all the GUIDS of the cards in the deck ( but most of the time they ARE all the GUIDS of the cards in the deck ).

    I then do the processing in the code box below which is to go through the deck and move some cards ( n=total_gold ) to a "gold basket" area and the remainder to the discard pile...
    I iterate through the GUIDS stored in the table I made earlier cards_to_cash_in and use that as a parameter to takeObject() on the deck ( called object )
    Initially I did this for all the GUIDS stored in the table but if I ended up emptying the deck I got the problem with the 2nd to last card take making the deck disappear so the last take failed
    so I limited the loop to stop just before the last GUID and put in some final special processing to get the last card by getObjectFromGUID() specifically.
    But even this card_to_move = getObjectFromGUID( cards_to_cash_in[#cards_to_cash_in] ) fails and the last card_to_move is set to nil. Why?

    All my debug print statements show computed guids tally with actual card guids on the table.

    Code:
    -- if we have valid beans in the beanfield then get the gold worth
    			
    			if ( ( bean_total > 0 ) and ( bean_type ~= '' ) and ( beanfield_is_valid ) ) then
    			
    				local total_gold = calculate_gold( bean_type, bean_total )
    				print( bean_total .. ' ' .. bean_type .. ' = ' .. total_gold .. ' gold' )
    				
    				-- now move ( n=total_gold ) many cards into the gold basket and the rest onto the discard pile
    				
    				local card_to_move = nil
    				
    				for i, card_guid in ipairs( cards_to_cash_in ) do
    				
    					if ( i < #cards_to_cash_in ) then
    					
    						print( '--------------------------------------------------------------' )
    						print( i .. ' of ' .. #cards_to_cash_in .. '  guid:' .. card_guid )
    						
    						takeParams = { guid = card_guid, }
    						card_to_move = object.takeObject( takeParams )
    						
    						print( 'Got hold of guid:' .. card_to_move.guid .. ' Its tag is:' .. card_to_move.tag .. ' Its name is:' .. card_to_move.name )
    												
    						if total_gold > 0 then
    					
    							send_card_to_gold_basket( card_to_move, basket_coordinates )
    							total_gold = total_gold - 1
    						
    						else
    					
    							send_card_to_discard( card_to_move )
    					
    						end
    					
    					end
    					
    				end
    				
    				-- do the "awkward last" card of the deck
    				
    				print( '--------------------------------------------------------------' )
    				print( #cards_to_cash_in .. ' of ' .. #cards_to_cash_in .. '  guid:' .. cards_to_cash_in[#cards_to_cash_in] )
    				
    				card_to_move = getObjectFromGUID( cards_to_cash_in[#cards_to_cash_in] )
    				
    				if ( card_to_move == nil ) then
    					print( 'that get above FAILS' )
    				end
    				
    				-- So... ON EXECUTION OF print statement BELOW I GET ERROR OF:
    				-- Error in Script(Global) function <cash in beans>:chunk_3:(1239,9-131): attempt to index a nil value
    				print( 'Got hold of guid:' .. card_to_move.guid .. ' Its tag is:' .. card_to_move.tag .. ' Its name is:' .. card_to_move.name )
    				
    				print( '--------------------------------------------------------------' )
    				
    			end
    Last edited by groovyclam; 12-03-2020 at 07:22 AM.

  2. #2
    Just discovered the newly implemented deck attribute ".remainder"

    See this link:
    https://forums.tabletopsimulator.com/showthread.php?8596-Last-Card-of-Deck&p=28491#post28491

    Which works for processing my last card in the code above.

    But I still don't understand why my code didn't work as was. Can anyone explain why the

    card_to_move = getObjectFromGUID( cards_to_cash_in[#cards_to_cash_in] )

    fails ?

Similar Threads

  1. BB Code list?
    By TheRaven81 in forum General Discussion
    Replies: 4
    Last Post: 09-17-2018, 09:18 AM
  2. Why doesn't my code correctly count deck cards?
    By AgentElrond in forum Scripting
    Replies: 1
    Last Post: 01-08-2018, 02:15 PM
  3. takeObject() doesn't take last card from deck
    By Banbury in forum Scripting
    Replies: 31
    Last Post: 11-07-2017, 04:33 PM
  4. Replies: 1
    Last Post: 03-22-2017, 10:28 AM
  5. Replies: 2
    Last Post: 07-09-2016, 11:42 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •