Jump to content




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


16 replies to this topic

#1 n1ghtk1ng

  • Members
  • 58 posts

Posted 16 July 2013 - 07: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 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 zero_cool

  • Members
  • 14 posts
  • LocationGermany

Posted 16 July 2013 - 07:47 PM

For the string-to-boolean thing, why not something like
toggled = toggledString == "true"
?

#3 n1ghtk1ng

  • Members
  • 58 posts

Posted 16 July 2013 - 09:13 PM

View Postzero_cool, on 16 July 2013 - 07:47 PM, said:

For the string-to-boolean thing, why not something like
toggled = toggledString == "true"
?

Can you please explain more?

#4 Grim Reaper

  • Members
  • 503 posts
  • LocationSeattle, WA

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 n1ghtk1ng

  • Members
  • 58 posts

Posted 16 July 2013 - 09:47 PM

View PostGrim 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 HurricaneCoder

  • Members
  • 52 posts

Posted 16 July 2013 - 10:56 PM

View Postn1ghtk1ng, on 16 July 2013 - 09:47 PM, said:

View PostGrim 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 immibis

    Lua God

  • Members
  • 1,033 posts
  • LocationWellington, New Zealand

Posted 17 July 2013 - 04:36 AM

The long version of what Grim Reaper said is this:
if toggledString == "true" then
  toggled = true
else
  toggled = false
end


#8 n1ghtk1ng

  • Members
  • 58 posts

Posted 17 July 2013 - 06:08 AM

View PostHurricaneCoder, on 16 July 2013 - 10:56 PM, said:

View Postn1ghtk1ng, on 16 July 2013 - 09:47 PM, said:

View PostGrim 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 albrat

  • Members
  • 162 posts
  • LocationA Chair

Posted 17 July 2013 - 06: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)


#10 albrat

  • Members
  • 162 posts
  • LocationA Chair

Posted 17 July 2013 - 06:20 AM

View Postn1ghtk1ng, on 17 July 2013 - 06:08 AM, said:

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.

#11 n1ghtk1ng

  • Members
  • 58 posts

Posted 17 July 2013 - 09: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[I would post the error but I'm on my phone and don't remember it]) Thanks anyways guys, if you can still help, please do. It's most likely my fault 'cause of my shit explaining.

#12 theoriginalbit

    Semi-Professional ComputerCrafter

  • Moderators
  • 7,332 posts
  • LocationAustralia

Posted 17 July 2013 - 10:04 AM

View Postn1ghtk1ng, on 17 July 2013 - 09:47 AM, said:

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()

View Postn1ghtk1ng, on 17 July 2013 - 09:47 AM, said:

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[I would post the error but I'm on my phone and don't remember 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.

#13 albrat

  • Members
  • 162 posts
  • LocationA Chair

Posted 17 July 2013 - 02: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

Quote

"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.)

#14 n1ghtk1ng

  • Members
  • 58 posts

Posted 17 July 2013 - 03:25 PM

View Posttheoriginalbit, on 17 July 2013 - 10:04 AM, said:

View Postn1ghtk1ng, on 17 July 2013 - 09:47 AM, said:

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()

View Postn1ghtk1ng, on 17 July 2013 - 09:47 AM, said:

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[I would post the error but I'm on my phone and don't remember 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?

View Postalbrat, on 17 July 2013 - 02:31 PM, said:

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

Quote

"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

#15 albrat

  • Members
  • 162 posts
  • LocationA Chair

Posted 17 July 2013 - 06: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

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 theoriginalbit

    Semi-Professional ComputerCrafter

  • Moderators
  • 7,332 posts
  • LocationAustralia

Posted 17 July 2013 - 10:22 PM

View Postn1ghtk1ng, on 17 July 2013 - 03:25 PM, said:

First of all, thank you for thoroughly explaining the code and giving a great example(at least one I easily understood)
No problems.

View Postn1ghtk1ng, on 17 July 2013 - 03:25 PM, said:

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.

#17 albrat

  • Members
  • 162 posts
  • LocationA Chair

Posted 18 July 2013 - 02: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 by albrat, 19 July 2013 - 11:52 AM.






1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users