Page 1 of 2
Return not returning
Posted: Thu Mar 07, 2013 6:19 pm
by tdc5013
Before I start here's my code:
http://codepad.org/XDL86qAx
So, this is a pretty complex piece of code to explain - so if I don't do a good job forgive me (though the code has comments, and for reference a 2 and a reset card are the same thing, and a "Go Lower" card is a 7).
Basically, this is working out from a supplied table of objects (cards) which particular card to play. The code given is specifically for whether or not the AI has a reset card (hasReset == true) and from there what to do with the supplied hand. The main errors occurs at lines 22 and 80, the returns. But I'm guessing if there are errors with those, there are probably similar errors scattered about that will be harder to dig up. With the returns, I have printed everything going in and out of this particular piece of code. All of which is going in is correct, it's what coming out that isn't. The hand[x] returns do not seem to work; despite the fact that I am able to actually print out these objects right before the return, as you can see from some of the code right at the end (around line 80). The error is that they return nothing. This has me stumped, I have no idea why something would work on one line and then the next seemingly not. If anyone can help I'd be very grateful.
Re: Return not returning
Posted: Thu Mar 07, 2013 6:31 pm
by bartbes
How do you call this function?
Re: Return not returning
Posted: Thu Mar 07, 2013 6:39 pm
by tdc5013
The function this code belongs in? I've set up an empty variable at the start and whenever it's needed (after the hand has been pruned of unplayable cards) it is used basically as
Code: Select all
theResult = theFunction(thePlayerHand)
. As I said though, I've checked the inputs at multiple times in this code and the table of objects coming in is exactly as I would expect. The error occurs when trying to execute the returns.
Thanks for your reply.
Re: Return not returning
Posted: Thu Mar 07, 2013 8:46 pm
by Hexenhammer
The hand[x] returns do not seem to work; despite the fact that I am able to actually print out these objects right before the return, as you can see from some of the code right at the end (around line 80). The error is that they return nothing.
Very strange. So in line 22 "print hand[higherCard]" shows the expected value but "return hand[higherCard]" returns nil? If that happens I have no explanation for it. I don't see how this can happen.
Re: Return not returning
Posted: Thu Mar 07, 2013 9:42 pm
by bartbes
Which is why I asked for the call site, if that print statement works correctly, your problem is most likely in the call site.
Re: Return not returning
Posted: Thu Mar 07, 2013 9:45 pm
by kikito
Try putting every 2 indentation levels inside a function with a descriptive name, and call those functions with the appropriate parameters.
Splitting your code into smaller chunks like that instead of having to navigate that big wall of text should help you find the issue.
Re: Return not returning
Posted: Thu Mar 07, 2013 9:49 pm
by miko
If you were giving a trimmed-down *.love which shows the bug, it could be solved instantly.
Anyways, the one obvious error I see is:
Code: Select all
if #hand>1 then
...
else
return hand[1]
end
So you are assuming that if #hand is not greater than 1 then it equals 1. What if it equals 0?
Re: Return not returning
Posted: Fri Mar 08, 2013 7:36 am
by tdc5013
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. 'cardTable' is a local table. The call comes at line 6, tacticalTable (another local table). Basically what happens is that the hand is pruned of unplayable cards (before this point in code), that works fine, the remaining cards are then fed into Opponent_AI.MultipleCheck to find if there are 'multiples' of the same rank. If there are then the game should return there, if not then it returns another table. This table is the one I've checked time and again at the start of the original piece of code I uploaded and has all the values I would expect, and it is fed into Opponent_AI.LastPickupCheck where there are some more tactical checks. Most important is that the table being passed into Opponent_AI.LastPickupCheck as an argument will never be nil, because if it is then the function isn't called.
Edit: I say this everytime, but excuse the semicolons. I use multiple languages so it's a habit I've picked up. Also if anyone wants the code in it's entirety (as producing a version to specifically test this error I think would be fairly complex) feel free to ask, the code is heavily commented.
Re: Return not returning
Posted: Fri Mar 08, 2013 8:40 am
by substitute541
Is hand[index] a table? Not hand itself but hand[index]. If it is actually a table, is the contents of hand[index] empty?
Edit: Miko is also right. If #hand is not greater than one, you assume it is 1. Instead, try elseif #hand == 1 and add in the else statement, a "false" value, and check if the returned value is false before running it.
Re: Return not returning
Posted: Fri Mar 08, 2013 9:11 am
by tdc5013
substitute541 wrote:Is hand[index] a table? Not hand itself but hand[index]. If it is actually a table, is the contents of hand[index] empty?
Edit: Miko is also right. If #hand is not greater than one, you assume it is 1. Instead, try elseif #hand == 1 and add in the else statement, a "false" value, and check if the returned value is false before running it.
Hand is a table of tables. Each entry is a card. The cards have their own fields; suit, number, etc. So, if you mean hand[index] as the last entry in hand, then yeah that will be a table.
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.