luacomputercraft

computercraft variable in if statment


I have a little problem I´m new to the Lua and ComputerCraft. so my problem I think is that I´m too stupid to code a variable change that's in an if statement I will post my code down hope that you can help me with that problem thx

mon = peripheral.wrap("top")
modem = peripheral.wrap("right")
mon.clear()
mon.setCursorPos(1,2)
mon.blit("Pulverizer","0000000000","dddddddddd")
redstone.setOutput("right", true)
Pulverizer = true
mon.setCursorPos(1,4)
mon.write("Furnice","0000000","ddddddd")
redstone.setOutput("left", true)
local Furnice = true
mon.setCursorPos(1,6)
mon.write("Injection Chamber","00000000000000000","ddddddddddddddddd")
local Injection_Chamber = true
redstone.setOutput("top",true)
mon.setCursorPos(1,8)
mon.write("Metalurgig InFuser","000000000000000000","dddddddddddddddddd")
local Metalurgig_InFuser = true
redstone.setOutput("bottom", true)
while true do
  event,side,x,y = os.pullEvent("monitor_touch")


  if x > 1 and x < 11 and y == 2 and Pulverizer == true then
    mon.setCursorPos(1,2)
    mon.clearLine()
    mon.blit("Pulverizer","0000000000","eeeeeeeeee")
    redstone.setOutput("right", false)
    Pulverizer = false

 elseif x > 1 and x < 11 and y == 2 and Pulverizer == false then
   mon.setCoursorPos(1,2)
   mon.clearLine()
   mon.blit("Pulverizer","0000000000","dddddddddd")
   redstone.setOutput("right", true)
   Pulverizer = true
  end
end

the link for the Pastebin : link to code

so the problem is in this part of the code

if x > 1 and x < 11 and y == 2 and Pulverizer == true then
mon.setCursorPos(1,2)
mon.clearLine()
mon.blit("Pulverizer","0000000000","eeeeeeeeee")
redstone.setOutput("right", false)
Pulverizer = false

the Pulverizer variable don't want to change from true to false so this code cant get activated right now

   elseif x > 1 and x < 11 and y == 2 and Pulverizer == false then
   mon.setCoursorPos(1,2)
   mon.clearLine()
   mon.blit("Pulverizer","0000000000","dddddddddd")
   redstone.setOutput("right", true)
   Pulverizer = true

and I don't know why also here is a video link from in-game footage so as you can see in the video it turns red but doesn't turn green anymore

https://youtu.be/R4dpN-egnwY


Solution

  • Not sure if this will solve your problem but it's worth a try to debug this. Just for good practice I'd recommend that when you define Pulverizer on line 7, set it to a local by using local Pulverizer = true. Probably won't make a difference but it's good practice. In my experience it's also good practice to avoid using variables with capitalized first letters.

    Secondly, try re-working your if block into 2 blocks like this:

    if x > 1 and x < 11 and y == 2 then
      --Put some output message here or something so that you can see that it's entered this block of code and detected the cursor properly
        mon.setCursorPos(1,2)
        mon.clearLine()
      if Pulverizer == true then
        --Put another output message here so that you can see that Pulverizer is being evaluated as true
        mon.blit("Pulverizer","0000000000","eeeeeeeeee")
      elseif Pulverizer == false then
        --Put one last output message here so that if Pulverizer is being evaluated as false you'll know
        mon.blit("Pulverizer","0000000000","dddddddddd")
      end
    Pulverizer = not Pulverizer
    redstone.setOutput("right", Pulverizer)
    end
    

    If you insert a debug message in the places where I've put comments (the greyed out lines) then it should help you narrow down on the problem because you can see which if blocks are being entered and which are not. Another good idea would be to put in some code immediately after you do Pulverizer = true or Pulverizer = false that will show the value of Pulverizer.

    I'm not super familiar with Computercraft's specific syntax so I'm not sure if you could display a message in chat or just on the monitor for debugging purposes, but you get the idea. Whenever you have a problem you can't quite narrow down, try putting debug messages around in your code so you can see exactly what's happening and work from there. If you find out any more information, feel free to leave a comment and I'll check back and see if I can help any more.

    Hopefully you find this helpful, have a great weekend!