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

fs API usage

Started by Pebbe19, 02 July 2013 - 02:18 PM
Pebbe19 #1
Posted 02 July 2013 - 04:18 PM
Okay, so I'm making a little program for controlling a few mob spawners. I'm using Direwolf20's "Button API", to make fancy buttons. I've successfully implemented buttons for 6 spawners, and the functions these buttons trigger. I want to be able to save the state of each spawner to a file, so that whenever the server I'm on restarts, the computer will remember the state of each spawner. I've tried to implement this into /one/ of my button functions, with no success. The computer sets the state of the spawner to false each time I press the button, even though it should toggle it. I think this is because I'm doing something wrong with the fs API.

Code:

os.unloadAPI("button")
os.loadAPI("ButtonAPI")
m = peripheral.wrap("right")
m.clear()
whiteActive = false
orangeActive = false
magentaActive = false
lightBlueActive = false
yellowActive = false
limeActive = false
rs.setBundledOutput("left", 0)
function fillTable()
  ButtonAPI.setTable("Enderman #1", enderman1, 4,20,3,5)
  ButtonAPI.setTable("Enderman #2", enderman2,4,20,7,9)
  ButtonAPI.setTable("Enderman #3", enderman3,4,20,11,13)
  ButtonAPI.setTable("Enderman #4", enderman4,4,20,15,17)
  ButtonAPI.setTable("Blaze #1", blaze1, 22,38,3,5)
  ButtonAPI.setTable("Blaze #2", blaze2, 22,38,7,9)
  ButtonAPI.screen()
end
function getClick()
  event, side, x, y = os.pullEvent("monitor_touch")
  ButtonAPI.checkxy(x,y)
end
function enderman1()
  ButtonAPI.toggleButton("Enderman #1")
   E1R = fs.open("F1", "r")
   whiteState = E1R.readLine()
   E1R.close()
   if state == "false" then
	colourNum = rs.getBundledOutput("left")
	newColourNum = colourNum + 1
	rs.setBundledOutput("left", newColourNum)
	whiteActiveSet = true
   else
	colourNum = rs.getBundledOutput("left")
	newColourNum = colourNum - 1
	rs.setBundledOutput("left", newColourNum)
	whiteActiveSet = false
   end
  E1W = fs.open("F1", "w")
  E1W.write(whiteActiveSet)
  E1W.close()
  print("Enderman Spawner #1 was toggled to: " ..tostring(whiteActiveSet))
end
function enderman2()
   ButtonAPI.toggleButton("Enderman #2")
   if orangeActive == false then
	colourNum = rs.getBundledOutput("left")
	newColourNum = colourNum + 2
	rs.setBundledOutput("left", newColourNum)
	orangeActiveSet = true
   else
	colourNum = rs.getBundledOutput("left")
	newColourNum = colourNum - 2
	rs.setBundledOutput("left", newColourNum)
	orangeActiveSet = false
   end
  orangeActive = orangeActiveSet
  print("Enderman Spawner #2 was toggled to: " ..tostring(orangeActive))
end
function enderman3()
   ButtonAPI.toggleButton("Enderman #3")
   if magentaActive == false then
	colourNum = rs.getBundledOutput("left")
	newColourNum = colourNum + 4
	rs.setBundledOutput("left", newColourNum)
	magentaActiveSet = true
   else
	colourNum = rs.getBundledOutput("left")
	newColourNum = colourNum - 4
	rs.setBundledOutput("left", newColourNum)
	magentaActiveSet = false
   end
  magentaActive = magentaActiveSet
  print("Enderman Spawner #3 was toggled to: " ..tostring(magentaActive))
end
function enderman4()
   ButtonAPI.toggleButton("Enderman #4")
   if lightBlueActive == false then
	colourNum = rs.getBundledOutput("left")
	newColourNum = colourNum + 8
	rs.setBundledOutput("left", newColourNum)
	lightBlueActiveSet = true
   else
	colourNum = rs.getBundledOutput("left")
	newColourNum = colourNum - 8
	rs.setBundledOutput("left", newColourNum)
	lightBlueActiveSet = false
   end
  lightBlueActive = lightBlueActiveSet
  print("Enderman Spawner #4 was toggled to: " ..tostring(lightBlueActive))
end
function blaze1()
  ButtonAPI.toggleButton("Blaze #1")
   if yellowActive == false then
	colourNum = rs.getBundledOutput("left")
	newColourNum = colourNum + 16
	rs.setBundledOutput("left", newColourNum)
	yellowActiveSet = true
   else
	colourNum = rs.getBundledOutput("left")
	newColourNum = colourNum - 16
	rs.setBundledOutput("left", newColourNum)
	yellowActiveSet = false
   end
  yellowActive = yellowActiveSet
  print("Blaze Spawner #1 was toggled to: " ..tostring(whiteActive))
end
function blaze2()
   ButtonAPI.toggleButton("Blaze #2")
   if limeActive == false then
	colourNum = rs.getBundledOutput("left")
	newColourNum = colourNum + 32
	rs.setBundledOutput("left", newColourNum)
	limeActiveSet = true
   else
	colourNum = rs.getBundledOutput("left")
	newColourNum = colourNum - 32
	rs.setBundledOutput("left", newColourNum)
	limeActiveSet = false
   end
  limeActive = limeActiveSet
  print("Blaze Spawner #2 was toggled to: " ..tostring(whiteActive))
end
fillTable()
ButtonAPI.heading("Spawner Control")
while true do
  getClick()
end
Lyqyd #2
Posted 02 July 2013 - 05:29 PM
You read the string into whiteState, then compare state to "false". You might consider using one of the two variables in both places.
Pebbe19 #3
Posted 02 July 2013 - 05:37 PM
Aha, thanks, I knew I had fucked up somewhere. ;D
westbound #4
Posted 25 February 2014 - 06:48 AM
I don't know if this topic is dead or not, but I want my computer to do the same thing.

Can you explain the code a bit more?
I implemented your code in mine: (the print thing is just for testing purposes, it will be a rednet signal in the end)

function scrap()
   button.toggleButton("Scrap")
   E1R = fs.open("F1", "r")
   scrapState = E1R.readLine()
   E1R.close()
	   if state == "false"then
		 print("Test1true")
		 scrapActiveSet = true
	   else
		 print("test1false")
		 scrapActiveSet = false
	   end
   E1W = fs.open("F1", "w")
   E1W.write(scrapActiveSet)
   E1W.close()
   print("Recyclers state is: "..tostring(scrapActiveSet))
end

But now:
How do i fix it with the false thingy? I'm not that good of a coder so I don't know what's wrong :S

Please help me.

Greetings,

WestBound
Lyqyd #5
Posted 25 February 2014 - 10:18 AM
Again, like OP, you're using two variables, scrapState and state. Pick one. Use it both places.