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?
Return not returning
Re: Return not returning
So is that output from the real code? If so, the OppHand is nil, not a table: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.
Code: Select all
line 1: attempt to get length of global 'OppHand' (a nil value)
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: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.
Code: Select all
assert(type(hand)=="table")
assert(#hand>0)
My lovely code lives at GitHub: http://github.com/miko/Love2d-samples
Re: Return not returning
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: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.
Code: Select all
assert(type(hand)=="table")
assert(#hand>0)
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
Re: Return not returning
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
- substitute541
- Party member
- Posts: 484
- Joined: Fri Aug 24, 2012 9:04 am
- Location: Southern Leyte, Visayas, Philippines
- Contact:
Re: Return not returning
You could atleast post the .love file. Isn't that really hard?
Currently designing themes for WordPress.
Sometimes lurks around the forum.
Sometimes lurks around the forum.
-
- Prole
- Posts: 6
- Joined: Mon Aug 27, 2012 10:21 am
Re: Return not returning
Looking at this section:
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:
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 {}
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
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]};
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
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 wrote:Looking at this section:
It's returning a table that looks like this (one card's fields inside a table):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
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:EDITCode: Select all
return {hand[handNum]};
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 {}
-
- Prole
- Posts: 6
- Joined: Mon Aug 27, 2012 10:21 am
Re: Return not returning
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):
... and use it like this:
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
Code: Select all
dump_table("some_table", some_table,)
Re: Return not returning
Cheers, that's really helpful. I check this and let you know how I get on.
Who is online
Users browsing this forum: Bing [Bot] and 2 guests