202 posts
Posted 29 January 2014 - 12:51 AM
In my tunnel program there is a part where I check to see if a settings files already exists and if it does then I ask the user if they want to load it. This used to work fine but it doesn't work right now. It is on lines 351-372. Instead of working how it is supposed to, it tells me that when I type 'y' it is invalid. While this shouldn't happen it then tells me that it is invalid again. After this it just goes on with the program as if it was valid. I have no idea what is going on.
Code:
http://pastebin.com/VbzaRCbfHelp is appreciated.
Thanks! :D/>
Edited on 29 January 2014 - 06:04 PM
110 posts
Posted 29 January 2014 - 07:37 AM
Dunno. Your code to read the input looks fine. Throw a debug in there and print the length of load. Maybe there's a character at the end like CR or LF.
Better, pull that snippet of code out into its own program, just for testing. Try to isolate the issue.
I would recommend cleaning up your conditional. You don't need "if 'y' then this elseif 'n' then that else a third thing." Just test against the 'y' and throw the rest in the else. I was concerned that useSettings was not set to a value but then I saw you initialize it at 341. A binary conditional can re-set that value just for clarity.
Edited on 29 January 2014 - 06:38 AM
286 posts
Location
United States
Posted 29 January 2014 - 08:11 AM
I agree with Buho – print out the value of
load before the test to see what you are getting from the file.
Also, the test in your
until statement is unnecessary. The variable continue is either
true or
false by that point in the code. It can be simplified.
Here is your code with a debug statement after load is converted to lower and the simplified
until statement:
repeat
term.clear()
term.setCursorPos(1,1)
print("A settings file was found.")
print("Would you like to load it?(Y/N): ")
load = read()
load = string.lower(load)
print("-->"..load.."<--") -- formatted to see exactly what load is
if load == "y" then
useSettings = true
continue = true
elseif load == "n" then
useSettings = false
continue = true
else
continue = false
invalid()
end
until continue
Edit: I ran the above code on a computer. I used your
repeat … until clause as well as the modified one above. Both work fine.
Edited on 29 January 2014 - 07:19 AM
202 posts
Posted 29 January 2014 - 02:14 PM
I tried both of your suggestions but it didn't reveal what problem is. When I add some printing to debug and get the value of load, it outputs
-->y<--
This just adds to the confusion because this shows no extra characters.
I also noticed that the turtle does not properly load the settings at all. Like if I keep going in the program it won't run how it should with the settings. Like place torches etc.
Edited on 29 January 2014 - 01:15 PM
202 posts
Posted 29 January 2014 - 06:46 PM
Ok, so after doing some testing I found out the source of the problem. The problem is somehow related to the values of useChests, useEnder, and useTorches. For each one of these variables that is true or false, it will print Invalid another time. If they are set to something besides true or false, the program works. Using this information I could technically fix my problem by changing what it checks for in these variables but I don't want to do this. I want to find out what is causing the actual problem. My guess is that it is somehow running through the loops that decide if you want to use all of those things but it shouldn't because you already chose to use settings from a file.
Edited on 29 January 2014 - 05:54 PM
286 posts
Location
United States
Posted 29 January 2014 - 10:03 PM
So, you have just ruled out the section of code that you thought was broken. The only thing that section of code does is test if a file exists, if it does it asks if you want to use it (y or n). If you fails to type in y or n, it prints "\nInvalid" and repeats the question. It sets useSettings to true or false depending on whether you type y or n. That's it. Printing the value of useSettings at the end of this portion of code should prove this to you.
So, your problem lies in a different section. I recommend you place debug prints at each stage of your code and make sure the values you think should be present are in fact what you think they ought to be.