This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
n1ghtk1ng's profile picture

Is it possible to write a boolean to a file instead of a string?

Started by n1ghtk1ng, 16 July 2013 - 06:17 PM
n1ghtk1ng #1
Posted 16 July 2013 - 08:17 PM
Sorry 'bout the title, couldn't think of anything else to name it
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'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
zero_cool #2
Posted 16 July 2013 - 08:47 PM
For the string-to-boolean thing, why not something like

toggled = toggledString == "true"
?
n1ghtk1ng #3
Posted 16 July 2013 - 10:13 PM
For the string-to-boolean thing, why not something like

toggled = toggledString == "true"
?

Can you please explain more?
Grim Reaper #4
Posted 16 July 2013 - 10: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.
n1ghtk1ng #5
Posted 16 July 2013 - 10:47 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.

Can you put it into my code? 'Cause I'm not fully understanding, I've never used == for variables, only if statements
HurricaneCoder #6
Posted 16 July 2013 - 11:56 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.

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.
immibis #7
Posted 17 July 2013 - 05:36 AM
The long version of what Grim Reaper said is this:

if toggledString == "true" then
  toggled = true
else
  toggled = false
end
n1ghtk1ng #8
Posted 17 July 2013 - 07:08 AM

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?
albrat #9
Posted 17 July 2013 - 07:16 AM
A longer explanation is that if you read a value from a file as a string… for example you read "true" from your file…

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)
albrat #10
Posted 17 July 2013 - 07:20 AM
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?

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 ( the true statement is the same as a string that says true ) That was bad information !!!

 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.
n1ghtk1ng #11
Posted 17 July 2013 - 10:47 AM
Not trying to sound ungrateful, which I'm not(I did learn something new and I bet it will help me in the future) but everyone is telling me about comparing things with ==, but how would that help me in this situation/with my problem? Once I get home I will most likely create a new topic with my new problem(I made CowBool = "true" instead of CowBool = true) and tweaked some other things, but it returns in an error which I am unfamiliar with(I understand where the error is and know what it means, but I don't know how to fix it) Thanks anyways guys, if you can still help, please do. It's most likely my fault 'cause of my shit explaining.
theoriginalbit #12
Posted 17 July 2013 - 11:04 AM
Not trying to sound ungrateful, which I'm not(I did learn something new and I bet it will help me in the future) but everyone is telling me about comparing things with ==, but how would that help me in this situation/with my problem?
File 1 contents (the one that you're writing the boolean to)

true
Your 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")
end
Your program (writing the file)

local h = fs.open("file1", "w")
h.write(tostring(doSomething))
h.close()

Once I get home I will most likely create a new topic with my new problem(I made CowBool = "true" instead of CowBool = true) and tweaked some other things, but it returns in an error which I am unfamiliar with(I understand where the error is and know what it means, but I don't know how to fix it) Thanks anyways guys, if you can still help, please do. It's most likely my fault 'cause of my shit explaining.
Keep all errors relating to the one piece of code in the same thread, it makes it hard for us to keep track of progress when it is spread over multiple threads.
albrat #13
Posted 17 July 2013 - 03:31 PM
I found the best way to understand == and = is to think

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
"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?"

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.)
n1ghtk1ng #14
Posted 17 July 2013 - 04:25 PM
Not trying to sound ungrateful, which I'm not(I did learn something new and I bet it will help me in the future) but everyone is telling me about comparing things with ==, but how would that help me in this situation/with my problem?
File 1 contents (the one that you're writing the boolean to)

true
Your 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")
end
Your program (writing the file)

local h = fs.open("file1", "w")
h.write(tostring(doSomething))
h.close()

Once I get home I will most likely create a new topic with my new problem(I made CowBool = "true" instead of CowBool = true) and tweaked some other things, but it returns in an error which I am unfamiliar with(I understand where the error is and know what it means, but I don't know how to fix it) Thanks anyways guys, if you can still help, please do. It's most likely my fault 'cause of my shit explaining.
Keep all errors relating to the one piece of code in the same thread, it makes it hard for us to keep track of progress when it is spread over multiple threads.

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?

I found the best way to understand == and = is to think

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
"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?"

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 :P/>
albrat #15
Posted 17 July 2013 - 07:01 PM
Ahhh, I understand now… you want to click the button the first time and turn on the signal, then press again and turn off the signal… that would require some programming changes.

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.

Spoiler

	--[[ VARIABLES ]]--
	
	local mon = peripheral.wrap("top")
	local CowBool = false
	
	--[[ FUNCTIONS ]]--
	
	os.loadAPI("button")  -- Bunch of functions.  Gave errors because it was not loaded first
	
	function MainButtons()
			button.setTable("Farm Menu", FarmMenu, 10, 20, 3, 5)
			button.setTable("Test2", mainb, 22, 32, 3, 5)
			button.setTable("Test3", mainc, 10, 20, 8, 10)
			button.setTable("Test4", maind, 22, 32, 8, 10)
			button.screen()
	end
	
	function getClick()
			event, side, x, y = os.pullEvent("monitor_touch")
			button.checkxy(x,y)
	end
	
	function FarmButtons()
			button.setTable("Cow Farm", CowFarm, 10, 20, 3, 5)
			button.setTable("Blaze Farm", BlazeFarm, 22, 32, 3, 5)
			button.setTable("Back", back, 10, 17, 15, 17)
			button.screen()
	end
	
	function FarmMenu() --Clears the past(main) menu and runs/displays the "Farms" menu
			button.flash("Farm Menu")
			sleep(.1)
			button.clearTable()
			FarmButtons()
			button.heading("Farms")
	end
	
	function mainb()
  sleep(1)
			--Do Stuff
	end
	
	function mainc()
  sleep(1)
			--Do Stuff
	end
	
	function maind()
  sleep(1)
			--Do stuff
	end
	
	function back()
			button.flash("Back")
			button.clearTable()
			MainButtons()
			button.heading("N1ght Network")
	end
	
	function CowFarm()
   CowBool = not CowBool
			local filew = fs.open("buttonstates", "w")
			filew.writeLine(tostring(CowBool))
			filew.close()
		  
			button.toggleButton("Cow Farm")
  
			if CowBool then
   rs.setBundledOutput("bottom", colors.white)
   else
   rs.setBundledOutput("bottom", 0)
   end
	end
	function CowFarmL() -- loading file test
			local filew = fs.open("buttonstates", "w")
			filew.writeLine(tostring(CowBool))
			filew.close()
		  
			button.toggleButton("Cow Farm")
  
			if CowBool then
   rs.setBundledOutput("bottom", colors.white)
   else
   rs.setBundledOutput("bottom", 0)
   end
	end
	
	function BlazeFarm()
			--Toggle the BlazeFarm
	end

	
	function CheckCowBool()
--Checks the file "buttonstates" for the boolean of the CowFarm, and tries to set CowBool to the current, correct state, defined in the file "buttonstates".
  if fs.exists("buttonstates") then
			local filer = fs.open("buttonstates", "r")
			local CowBoole = filer.readLine() == "true"
			filer.close()
   CowBool = CowBoole
		end
  sleep(1)
  if CowBool then
   rs.setBundledOutput("bottom", colors.white)
   mon.clear()
   MainButtons()
   button.heading("N1ght Network")
   FarmMenu()
   sleep(1)
   CowFarmL()
   button.screen()
   sleep(1)
   back()
  else
   rs.setBundledOutput("bottom", 0)
  end
	end
	
	
	--[[ MAIN ]]--


CheckCowBool() -- Our new check function
  
mon.clear()
	MainButtons()
	button.heading("N1ght Network")

	while true do
			getClick()
	end


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..
theoriginalbit #16
Posted 17 July 2013 - 11:22 PM
First of all, thank you for thoroughly explaining the code and giving a great example(at least one I easily understood)
No problems.

Second, how come you had to convert "dosomething" to a string when you wrote it in the separate file?
Ahh thats just a habit I've got myself into…


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.
albrat #17
Posted 18 July 2013 - 03:22 PM
https://en.wikipedia...i/Concatenation << just incase. :)/> joining of strings

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 on 19 July 2013 - 10:52 AM