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

Code not working. Cant seem to find the problem. Please help

Started by Pieterv24, 16 January 2015 - 04:31 PM
Pieterv24 #1
Posted 16 January 2015 - 05:31 PM
Hello
I am currently working on a system to swap blocks in an bloodmagic altar.
It works by using redstone signals of variating colors to trigger the switch.
the only thing it has to do first is to swap the default block back to its position by applying a signal to the last used switcher.
The only thing is i can't seem to get my code working.
I was hoping someone can see what i am doing wrong.
Thanks in advance.

recieveComp = 8
switchNr = 6
rednet.open("top")
ToFile = fs.open("TempFile", "w")
FromFile = fs.open("TempFile", "r")
function EmptyFile()
  ReadFile()
  if CurrentBlock == "" then
	writeToFile("Home")
  end
end
function check()
  ReadFile()
  switch(CurrentBlock)
	case "Red": pulse(colors.red)
	case "Green": pulse(colors.green)
	case "Brown": pulse(colors.brown)
	case "Blue": pulse(colors.blue)
	case "Purple": pulse(colors.purple)
	case "Cyan": pulse(colors.cyan)
	case "Light Gray": pulse(colors.lightGray)
	case "Gray": pulse(colors.gray)
	case "Pink": pulse(colors.pink)
	case "Home": print("alreadySomething There")
  end
end
function pulse(pulseColor)
  rs.setBundledOutput("back", pulseColor)
  sleep(0,5)
  rs.setBundledOutput("back", colors.white)
end
function writeToFile(Blank)
  ToFile.write(Blank)
  ToFile.close()
end
function ReadFile()
  CurrentBlock = FromFile.readLine()
  FromFile.close()
end
function swapAugCap()
  if CurrentBlock == "Red" then
	break
  else
	check()
	writeToFile("Red")
	check()
  end
end
function swapDisLoc()
  if CurrentBlock == "Green" then
	break
  else
	check()
	writeToFile("Green")
	check()
  end
end
function swapOrb()
  if CurrentBlock == "Brown" then
	break
  else
	check()
	writeToFile("Brown")
	check()
  end
end
function swapSupCap()
  if CurrentBlock == "Blue" then
	break
  else
	check()
	writeToFile("Blue")
		check()
  end
end
function swapAcc()
  if CurrentBlock == "Purple" then
	break
  else
	check()
	writeToFile("Purple")
	check()
  end
end
function swapSpe()
  if CurrentBlock == "Cyan" then
	break
  else
	check()
	writeToFile("Cyan")
	check()
  end
end
function swapEff()
  if CurrentBlock == "Light Gray" then
	break
  else
	check()
	writeToFile("Light Gray")
	check()
  end
end
function swapSac()
  if CurrentBlock == "Gray" then
	break
  else
	check()
	writeToFile("Gray")
	check()
  end
end
function swapSelSac()
  if CurrentBlock == "Pink" then
	break
  else
	check()
	writeToFile("Pink")
	check()
  end
end
while true do
  EmptyFile()
  event = os.pullEvent("rednet_message")
  senderID, message = rednet.recieve(switchNr, 5)
  if senderID == recieveComp then
	switch(message)
	  case 1: swapAugCap()
	  case 2: swapDisLoc()
	  case 3: swapOrb()
	  case 4: swapSupCap()
	  case 5: swapAcc()
	  case 6: swapSpe()
	  case 7: swapEff()
	  case 8: swapSac()
	  case 9: swapSelSac()
	end
  end
end
Edited on 16 January 2015 - 04:35 PM
Dragon53535 #2
Posted 16 January 2015 - 05:52 PM
Lua has no switch statement. And as such your switch statements need to be converted to if/elseif's
Bomb Bloke #3
Posted 16 January 2015 - 11:29 PM
Though if/thens would be rather more verbose than using tables. For eg:

local cases = {swapAugCap,swapDisLoc,swapOrb,swapSupCap,swapAcc,wapSpe,swapEff,swapSac,swapSelSac}

while true do
  EmptyFile()
  local senderID, message = rednet.receive(switchNr, 5)  -- rednet.recEIve() pulls the "rednet_message" event for you. Don't pull it manually unless you intend to unpack it manually.
  if senderID == recieveComp and type(message) == "number" and message > 0 and message <= #cases then cases[message]() end
end

You also can't open the same file in two different modes at once. If you wish to change modes, you need to close the handle (if it was open), then re-open it with the one you wish to use at the time.

And you've got nine separate functions to do the exact same thing!! …and you've got a comma in your sleep() call!!! …Here, consider this re-write. It follows the same flow as your original, but packs it down a fair bit:

local receiveComp, switchNr, fileName, CurrentBlock = 8, 6, "TempFile"
rednet.open("top")

local colList = {"red","green","brown","blue","purple","cyan","lightGrey","grey","pink"}  -- Valid names from the "colours" table, in the order you had them

-- We only need to read from the file ONCE for every time the script runs:
if fs.exists(fileName) then
	local myFile = fs.open(fileName, "r")
	CurrentBlock = myFile.readLine()
	myFile.close()
end

local function check()
	if CurrentBlock then  -- if CurrentBlock is anything other than nil or false, then...
		rs.setBundledOutput("back", colours[CurrentBlock])  -- Eg, if CurrentBlock is "lightGrey", then colours[CurrentBlock] is the same as colours.lightGrey
		sleep(0.5)
		rs.setBundledOutput("back", colours.white)
	else print("alreadySomething There") end
end

while true do
	local senderID, message = rednet.receive(switchNr, 5)
	if senderID == receiveComp and type(message) == "number" and colList[message] and CurrentBlock ~= colList[message] then
		check()

		CurrentBlock = colList[message]  -- Eg, if "message" is 3 then "colList[message]" will be "brown"

		local myFile = fs.open(fileName, "w")
		myFile.write(CurrentBlock)
		myFile.close()

		check()
	end
end
Edited on 16 January 2015 - 10:35 PM