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