Page 2 of 2

Re: Return not returning

Posted: Fri Mar 08, 2013 10:21 pm
by FrogsFriend
Just to clarify, what is actually returned? zero, empty table, nill?

What do you see if you print the entire 'hand' table before calling the function, immediately the function starts and just before you return?

Re: Return not returning

Posted: Sat Mar 09, 2013 12:36 am
by miko
tdc5013 wrote:Ok, so I've uploaded the code for where the function is called http://codepad.org/ErgyencK

'OppHand' is a local variable referencing the table holding the AI's hand.
So is that output from the real code? If so, the OppHand is nil, not a table:

Code: Select all

line 1: attempt to get length of global 'OppHand' (a nil value)
tdc5013 wrote:As for the elseif, I'll add that. Though, as I said the function shouldn't be called unless that 'hand' argument being passed in is greater than 1; though I suppose it's always a good idea to have handling for exceptional errors.
Another wrong assumption here: what makes you think that if #hand>1 then it is a table? It could be a string. So add this:

Code: Select all

assert(type(hand)=="table")
assert(#hand>0)

Re: Return not returning

Posted: Sat Mar 09, 2013 2:02 am
by tdc5013
tdc5013 wrote:As for the elseif, I'll add that. Though, as I said the function shouldn't be called unless that 'hand' argument being passed in is greater than 1; though I suppose it's always a good idea to have handling for exceptional errors.
Another wrong assumption here: what makes you think that if #hand>1 then it is a table? It could be a string. So add this:

Code: Select all

assert(type(hand)=="table")
assert(#hand>0)
[/quote]

Code: Select all

cardTable = Opponent_AI.MultipleCheck(OppHand); 				
				if( #cardTable ~= 0 ) then
					print("Playing Cards");
	
					tacticalCardTable = Opponent_AI.LastPickupCheck(cardTable);
                                 else
                                        Rulebook.Pickup(GLOBAL_VARS.Player2Store)
                                 end
This is the code that means that 'tacticalCardTable' will never be a nil, as if the argument i nil then we don't run the function. The hand will never be anything else than a table of tables. If it is not that, then we do not run the function; instead we say "the AI can't play a legal card, so it has to pick up the played card pile.

Re: Return not returning

Posted: Sun Mar 10, 2013 2:11 am
by tdc5013
Maybe it's easier if I just upload my code. If you open it you should get a console window with print outs. The error is specifically happening whenever it prints out "TACTICAL TABLE:0". This should never happen. But you guys know this better than me: please, please tell me what I've done wrong. Any help will be very greatly appreciated.

Re: Return not returning

Posted: Sun Mar 10, 2013 3:07 am
by substitute541
You could atleast post the .love file. Isn't that really hard?

Re: Return not returning

Posted: Sun Mar 10, 2013 10:13 am
by FrogsFriend
Looking at this section:

Code: Select all

	print("PLAYING A CARD NON AGRESSIVELY OR BASED ON RESETS");
--Resetting Variables
	Opponent_AI.isMultiples = false;
--A little error catch to stop it returning no cards
	local handNum = 0;
	for i = 1, #hand do
		if( hand[i].number >= cardPile[#cardPile].number ) then
			handNum = i;
			print("RETURNING A CARD EQUAL OR HIGHER TO THE LAST PLAYED");
			print("i = "..i);
			print("handNum "..handNum);
			print("hand[i].Picture: "..hand[i].picture);
			print("hand[handNum].Picture: "..hand[handNum].picture);
			return hand[handNum];
		end
	end

It's returning a table that looks like this (one card's fields inside a table):
tacticalCardTable table: 02A76400
number 6
Attribute function: 02A25EF0
name Seven
picture 7S.png
drawable Image
isSelected false
suit Spades
colour Black

When it should be like this(a table containing a table for just one card):
tacticalCardTable table: 02A76400
1 table: 02A38F88
number 6
Attribute function: 02A25EF0
name Seven
picture 7S.png
drawable Image
isSelected false
suit Spades
colour Black

Try changing the return line to this:

Code: Select all

return {hand[handNum]};
EDIT
Just to clarify, a returned table of cards should be { {}, {}, {}, }
A returned table with just one card in it should be { {}, }
What you were returning was just the contents of one card as {}

Re: Return not returning

Posted: Sun Mar 10, 2013 1:11 pm
by tdc5013
FrogsFriend wrote:Looking at this section:

Code: Select all

	print("PLAYING A CARD NON AGRESSIVELY OR BASED ON RESETS");
--Resetting Variables
	Opponent_AI.isMultiples = false;
--A little error catch to stop it returning no cards
	local handNum = 0;
	for i = 1, #hand do
		if( hand[i].number >= cardPile[#cardPile].number ) then
			handNum = i;
			print("RETURNING A CARD EQUAL OR HIGHER TO THE LAST PLAYED");
			print("i = "..i);
			print("handNum "..handNum);
			print("hand[i].Picture: "..hand[i].picture);
			print("hand[handNum].Picture: "..hand[handNum].picture);
			return hand[handNum];
		end
	end

It's returning a table that looks like this (one card's fields inside a table):
tacticalCardTable table: 02A76400
number 6
Attribute function: 02A25EF0
name Seven
picture 7S.png
drawable Image
isSelected false
suit Spades
colour Black

When it should be like this(a table containing a table for just one card):
tacticalCardTable table: 02A76400
1 table: 02A38F88
number 6
Attribute function: 02A25EF0
name Seven
picture 7S.png
drawable Image
isSelected false
suit Spades
colour Black

Try changing the return line to this:

Code: Select all

return {hand[handNum]};
EDIT
Just to clarify, a returned table of cards should be { {}, {}, {}, }
A returned table with just one card in it should be { {}, }
What you were returning was just the contents of one card as {}
Thanks for the advice, just to clarify though, are you saying I should use this bracketing format with every hand[specificCard] return, or with every hand return in general - bearing in mind that hand should itself be a table of tables? Again, thanks.

Re: Return not returning

Posted: Sun Mar 10, 2013 1:34 pm
by FrogsFriend
If you return the whole 'hand' (e.g. return hand ) it should be ok as it is. It's already in the format { {}, {}, {}, } that the code it's returning to is expecting.

You only need to enclose the returned value in a table (e.g. return {hand[specific_card]} ) where you're just returning a single card taken from the 'hand' table. Effectively you need to create a new hand containing the single specific card and return that

It looks like your recieving code is always expecting a hand of cards to be returned to it, even if the hand only has a single card in it. { {}, }

Hope that makes sense.

EDIT

I use this bit of code below to print tables when I need to check if the contents or structure are what I'm expecting (especially nested tables):

Code: Select all

function DumpValue(key, value, prefix, func, logged)
	func(prefix .. key .. "\t" .. tostring(value));
	if type(value) == "table" then
		local newprefix = prefix .. "\t";
		if not logged[value] then
			logged[value] = true;
			for k, v in pairs(value) do
				DumpValue(k, v, newprefix, func, logged);
			end
		end
	end
end

function dump_table(key, value, prefix)
	DumpValue(key, value, prefix or "", print, { });
end
... and use it like this:

Code: Select all

dump_table("some_table", some_table,)

Re: Return not returning

Posted: Sun Mar 10, 2013 2:37 pm
by tdc5013
Cheers, that's really helpful. I check this and let you know how I get on.