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.
Return not returning
- bartbes
- Sex machine
- Posts: 4946
- Joined: Fri Aug 29, 2008 10:35 am
- Location: The Netherlands
- Contact:
Re: Return not returning
How do you call this function?
Re: Return not returning
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 . 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.
Code: Select all
theResult = theFunction(thePlayerHand)
Thanks for your reply.
- Hexenhammer
- Party member
- Posts: 175
- Joined: Sun Feb 17, 2013 8:19 am
Re: Return not returning
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.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.
- bartbes
- Sex machine
- Posts: 4946
- Joined: Fri Aug 29, 2008 10:35 am
- Location: The Netherlands
- Contact:
Re: Return not returning
Which is why I asked for the call site, if that print statement works correctly, your problem is most likely in the call site.
- kikito
- Inner party member
- Posts: 3153
- Joined: Sat Oct 03, 2009 5:22 pm
- Location: Madrid, Spain
- Contact:
Re: Return not returning
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.
Splitting your code into smaller chunks like that instead of having to navigate that big wall of text should help you find the issue.
When I write def I mean function.
Re: Return not returning
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
My lovely code lives at GitHub: http://github.com/miko/Love2d-samples
Re: Return not returning
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.
'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.
- substitute541
- Party member
- Posts: 484
- Joined: Fri Aug 24, 2012 9:04 am
- Location: Southern Leyte, Visayas, Philippines
- Contact:
Re: Return not returning
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.
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.
Currently designing themes for WordPress.
Sometimes lurks around the forum.
Sometimes lurks around the forum.
Re: Return not returning
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.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.
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.
Who is online
Users browsing this forum: Ahrefs [Bot] and 3 guests