Return not returning

General discussion about LÖVE, Lua, game development, puns, and unicorns.
FrogsFriend
Prole
Posts: 6
Joined: Mon Aug 27, 2012 10:21 am

Re: Return not returning

Post 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?
User avatar
miko
Party member
Posts: 410
Joined: Fri Nov 26, 2010 2:25 pm
Location: PL

Re: Return not returning

Post 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)
My lovely code lives at GitHub: http://github.com/miko/Love2d-samples
tdc5013
Prole
Posts: 38
Joined: Wed Feb 08, 2012 4:24 pm

Re: Return not returning

Post 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.
tdc5013
Prole
Posts: 38
Joined: Wed Feb 08, 2012 4:24 pm

Re: Return not returning

Post 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.
Attachments
CardCoder.zip
(112.19 KiB) Downloaded 121 times
User avatar
substitute541
Party member
Posts: 484
Joined: Fri Aug 24, 2012 9:04 am
Location: Southern Leyte, Visayas, Philippines
Contact:

Re: Return not returning

Post by substitute541 »

You could atleast post the .love file. Isn't that really hard?
Currently designing themes for WordPress.

Sometimes lurks around the forum.
FrogsFriend
Prole
Posts: 6
Joined: Mon Aug 27, 2012 10:21 am

Re: Return not returning

Post 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 {}
tdc5013
Prole
Posts: 38
Joined: Wed Feb 08, 2012 4:24 pm

Re: Return not returning

Post 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.
FrogsFriend
Prole
Posts: 6
Joined: Mon Aug 27, 2012 10:21 am

Re: Return not returning

Post 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,)
tdc5013
Prole
Posts: 38
Joined: Wed Feb 08, 2012 4:24 pm

Re: Return not returning

Post by tdc5013 »

Cheers, that's really helpful. I check this and let you know how I get on.
Post Reply

Who is online

Users browsing this forum: Bing [Bot] and 2 guests