Is it possible to write a boolean to a file instead of a string?
#1
Posted 16 July 2013 - 07:17 PM
Anyways, I was wondering if it was possible to either write a boolean to a separate file, instead of writing it as a string[I know you can't do this(at least for what I know), but it's the only way I could think of that would work 100% properly]
I've been using Direwolf20's Button API (my button API didn't have as much functions and was a lot more buggy) and have been trying to a few different things with it
First of all, I've been trying to figure out for around 2 days how I can successfully keep a button toggled, even when I restart the computer or exit that menu
If you can't convert a string to a boolean(which I doubt you can), what is another way I could do this(and also simplify my code? It seems really messy and overly-complex currently)
Also, when I press the button, what it should do is turn a redstone bundled output on/off, but when I turn it on, then try to turn it off, it stays on until I reset it. How can I fix this from happening?
Button API: http://pastebin.com/2zVysjBE
My Code: http://pastebin.com/sFSq6G79
#2
Posted 16 July 2013 - 07:47 PM
toggled = toggledString == "true"?
#4
Posted 16 July 2013 - 09:29 PM
toggled = toggledString == "true"
The variable toggled will be set to the evaluated expression toggledString == "true" which is a comparison and therefore will be true or false.
#5
Posted 16 July 2013 - 09:47 PM
Grim Reaper, on 16 July 2013 - 09:29 PM, said:
toggled = toggledString == "true"
The variable toggled will be set to the evaluated expression toggledString == "true" which is a comparison and therefore will be true or false.
Can you put it into my code? 'Cause I'm not fully understanding, I've never used == for variables, only if statements
#6
Posted 16 July 2013 - 10:56 PM
n1ghtk1ng, on 16 July 2013 - 09:47 PM, said:
Grim Reaper, on 16 July 2013 - 09:29 PM, said:
toggled = toggledString == "true"
The variable toggled will be set to the evaluated expression toggledString == "true" which is a comparison and therefore will be true or false.
Can you put it into my code? 'Cause I'm not fully understanding, I've never used == for variables, only if statements
Ok the == just mean comparing two things. For example:
local x = "test1" local y = "test1" x == y -- this will return true because they are the same value --however local y = "test2" x == y --will return false because they are not the same.
#7
Posted 17 July 2013 - 04:36 AM
if toggledString == "true" then toggled = true else toggled = false end
#8
Posted 17 July 2013 - 06:08 AM
HurricaneCoder, on 16 July 2013 - 10:56 PM, said:
n1ghtk1ng, on 16 July 2013 - 09:47 PM, said:
Grim Reaper, on 16 July 2013 - 09:29 PM, said:
toggled = toggledString == "true"
The variable toggled will be set to the evaluated expression toggledString == "true" which is a comparison and therefore will be true or false.
Can you put it into my code? 'Cause I'm not fully understanding, I've never used == for variables, only if statements
Ok the == just mean comparing two things. For example:
local x = "test1" local y = "test1" x == y -- this will return true because they are the same value --however local y = "test2" x == y --will return false because they are not the same.
K, I understand it now. But say if I did
CowBool == ReadBool
Wouldn't it not work(That's where the error was happening, and I have already defined those two variables) CowBool is an actual boolean, but ReadBool just read the string in the file "buttonstates" and returns whether it says "true" or "false". Wouldn't it just be comparing a boolean and a string?
#9
Posted 17 July 2013 - 06:16 AM
You store the string in a varible named "loadedString" ...
your code to convert the value to true or false would be
varible = loadedString == "true"this sets "varible" to the boolean value of "loadedString" (wether it matches true or not) if the loadedString is not true it will always return false.
like immibis above said, the simple line of code is the same as the whole if statement. But much more efficient and shorter to type.
to save the boolean in the first place would be a case of saving a value with ...
convertedBoolean = tostring(unconvertedBoolean)
#10
Posted 17 July 2013 - 06:20 AM
n1ghtk1ng, on 17 July 2013 - 06:08 AM, said:
CowBool == ReadBool
Wouldn't it not work(That's where the error was happening, and I have already defined those two variables) CowBool is an actual boolean, but ReadBool just read the string in the file "buttonstates" and returns whether it says "true" or "false". Wouldn't it just be comparing a boolean and a string?
EDIT::
You have no way in your code there to catch what the true or false statement was...
catchBool = cowBool == readBool
catchBool receives the value of wether cowBool is the same as readBool
catchBool = tostring(cowBool) == tostring(readBool)
This will return a Boolean of wether the true statements match. (if you do not convert a boolean to string it will not match a string).
* as readBool is already a string you may not need to do the "tostring()" for that one.
#11
Posted 17 July 2013 - 09:47 AM
#12
Posted 17 July 2013 - 10:04 AM
n1ghtk1ng, on 17 July 2013 - 09:47 AM, said:
trueYour program (reading the file)
local h = fs.open("file1", "r") local doSomething = h.readLine() == "true" h.close() if doSomething then print("doSomething was true in file 1") else print("it was false") endYour program (writing the file)
local h = fs.open("file1", "w") h.write(tostring(doSomething)) h.close()
n1ghtk1ng, on 17 July 2013 - 09:47 AM, said:
#13
Posted 17 July 2013 - 02:31 PM
var = "true" is Var wants to contain "true"
where == is Var wants to know if it is the same as "true"
so Test = var == "true" means that Test wants to contain the result of var matching "true".
I only just noticed this at the bottom of your origional post
Quote
to turn off the bundled cables you have to get the value of the cables and then subtract the value for your cable and then send the new value to the cables...
rs.setBundledOutput("bottom", colors.white)Turns on only the white cable. so sending
rs.setBundledOutput("bottom", 0)would reset the cables to all off.
http://computercraft...etBundledOutput
I would suggest moving the LoadAPI to the top of the file (function calls first, the API is a set of functions.)
#14
Posted 17 July 2013 - 03:25 PM
theoriginalbit, on 17 July 2013 - 10:04 AM, said:
n1ghtk1ng, on 17 July 2013 - 09:47 AM, said:
trueYour program (reading the file)
local h = fs.open("file1", "r") local doSomething = h.readLine() == "true" h.close() if doSomething then print("doSomething was true in file 1") else print("it was false") endYour program (writing the file)
local h = fs.open("file1", "w") h.write(tostring(doSomething)) h.close()
n1ghtk1ng, on 17 July 2013 - 09:47 AM, said:
First of all, thank you for thoroughly explaining the code and giving a great example(at least one I easily understood) Second, how come you had to convert "dosomething" to a string when you wrote it in the separate file?
albrat, on 17 July 2013 - 02:31 PM, said:
var = "true" is Var wants to contain "true"
where == is Var wants to know if it is the same as "true"
so Test = var == "true" means that Test wants to contain the result of var matching "true".
I only just noticed this at the bottom of your origional post
Quote
to turn off the bundled cables you have to get the value of the cables and then subtract the value for your cable and then send the new value to the cables...
rs.setBundledOutput("bottom", colors.white)Turns on only the white cable. so sending
rs.setBundledOutput("bottom", 0)would reset the cables to all off.
http://computercraft...etBundledOutput
I would suggest moving the LoadAPI to the top of the file (function calls first, the API is a set of functions.)
I think I explained my question wrong/ you interpreted it wrong. What I was asking was when I press the button(on the program), it only turns on, and won't turn off when I try to. I realized I easily know how to turn the correct wires on when I reset it, so I don't need that answer. Thanks anyways
#15
Posted 17 July 2013 - 06:01 PM
I also found your problem... you are getting the error "button : 65 : attempt to index ?(a nil value)" this is because you wrote the boolean checker for Cow Farm button. You forgot to drop the button menu into the Farm menu and load the Cow Farm menu up, hence the program button crashed out... (the data was not loaded) You need to feed the program with the actions in the correct sequence otherwise the button API gets confused.
I loaded this up on a advanced computer, upon removing your file checks function from the execution list it works perfectly. * I played around a little and wrote the program a little different. -- Now the power to the white wire turns on and off as you click the button.
there is a problem with the Button API... it does not save sub table data. This means that every time you click for a new menu it wipes the previous values from the table. so even if your CowBool script changed the value, the button on every reload would default to off (dark) no matter what the output is set too. but clicking the button will change the output.
I have the button turning on and off... After a restart it loads its previous state and does not error out. But as I mentioned the button API by default can not reload states. It would require re-writing to be capable of handling your loaded states.
Here are my code re-writes.
The button API has not been altered, That would take a lot of work to make it work how you are hoping to make it work... (if I understand your intended use of the button API).
Hope this helps..
#16
Posted 17 July 2013 - 10:22 PM
n1ghtk1ng, on 17 July 2013 - 03:25 PM, said:
n1ghtk1ng, on 17 July 2013 - 03:25 PM, said:
local bool = true print( bool ) --# works fine; prints "true" print("State: "..tostring( bool )) --# works fine; prints "State: true" print("State: "..bool) --# error! "Attempt to concatenate string and boolean"
For that reason I have a habit of putting anything I'm concatenating or printing into a tostring, unless I know it is a string.
#17
Posted 18 July 2013 - 02:22 PM
I have taken a longer look at the button program and watched Direwolf20's video... This was never intended to be multiple menu's. or an API. (it just seems to work well as an API).
the button coding was supposed to be inside the button program... it seems you are trying to store more information but I think you need a way to save tables accross the different menu's before you move forwards. (possibly edit the button API to include a Save Table to new varible name before you wipe it and load new data. and a system to handle switching the table data around on each new menu. [a lot more programming and research needed] )
Edit :: I decided to play around with this code and the button API... I tried nesting Tables inside tables, inside tables... - this worked well and I managed to make a new layer for the buttons. BUT. the code to retreive the information is getting more and more complex, I have a few hundred lines of code with code commented out all over the place. Editing another persons code (like direwolf20's) which does not have comments makes figuring it out almost impossible.
I have spent the best part of a day just getting my head around how to setup the table to allow you to add more entries... unlike Direwolf20's way of adding the buttons I tried adding all buttons in one table. (this worked better than previous tables). The code is way too complex to be usefull, many sorting loops inside sorting loops... So it is nowhere near usefull as a button menu system. * Keeps working on it though.
Edited by albrat, 19 July 2013 - 11:52 AM.
1 user(s) are reading this topic
0 members, 1 guests, 0 anonymous users