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

Is A Variable Not Changing? I'm Confused!

Started by Guilty_Spark_117, 17 November 2013 - 02:33 PM
Guilty_Spark_117 #1
Posted 17 November 2013 - 03:33 PM
So I have a monitor on the back side of the computer. It displays if users can enter my house or not. There is a button to the right of the computer, and I want it to toggle whether the monitor says if you can enter or not. No matter what I set x to, the monitor always prints the nopass function. Also, is there a way I could save and load the variable to the computer so the chunk will not have to be loaded all the time? I'm noobish sorry.Thanks in advance!

local m = peripheral.wrap("back")

local function pass()
m.clear()
m.setBackgroundColor(8192)
m.setTextScale(1)
m.setCursorPos(1,1)
m.write("You may")
m.setCursorPos(1,2)
m.write(" enter ")
m.setCursorPos(1,3)
m.setTextColor(32)
m.write("WITHOUT ")
m.setTextColor(1)
m.setCursorPos(1,4)
m.write(" perms! ")
m.setCursorPos(1,5)
m.write(" ")
x = 2
end

local function nopass()
m.clear()
m.setBackgroundColor(16384)
m.setTextScale(1)
m.setCursorPos(1,1)
m.write("You may")
m.setCursorPos(1,2)
m.write(" enter ")
m.setCursorPos(1,3)
m.setTextColor(32)
m.write("WITH ")
m.setTextColor(1)
m.setCursorPos(1,4)
m.write(" perms! ")
m.setCursorPos(1,5)
m.write(" ")
sleep(5)
x = 1
end
x = 1
while true do
while not redstone.getInput("right",true) do
sleep(.5)
end
if x == 1 then
pass()

else
nopass()
end
end
jag #2
Posted 17 November 2013 - 05:16 PM
I don't really see the problem, but try using a boolean instead of number for the state wich you can pass or not.
Example:

canpass = true
while true do
    while not rs.getInput("right") do
        sleep(.5)
    end
    canpass = not canPass
    if canpass then pass()
    else nopass() end
end

Could you explain more how the code didnt work? What is wrong with the program?
jag #3
Posted 17 November 2013 - 05:25 PM
Maybe when you press the button it flickers pass/nopass so fast and its a chance it goes back to its original state.
By fixing this you can either simply add a sleep(2) or something or you compare the previous state and see if it just got turned on.


while true do
    oldstate = state
    state = rs.getInput("right")
    if oldstate == false and state == true then
        canpass = not canpass
    end

    if canpass then pass()
    else nopass() end

    sleep(0.5)
end
^ that should work. Now my method might bot be optimal concidering it redraws each 0.5 seconds, but I think it works at least (not tested).
Guilty_Spark_117 #4
Posted 17 November 2013 - 08:43 PM
Well, I got it working but I'm still confused of how the code on the previous post isn't working.


local m = peripheral.wrap("back")
local function pass()
m.clear()
m.setBackgroundColor(8192)
m.setTextScale(1)
m.setCursorPos(1,1)
m.write("You may")
m.setCursorPos(1,2)
m.write(" enter ")
m.setCursorPos(1,3)
m.setTextColor(32)
m.write("WITHOUT ")
m.setTextColor(1)
m.setCursorPos(1,4)
m.write(" perms!  ")
m.setCursorPos(1,5)
m.write("	   ")
sleep(2)
end
local function nopass()
m.clear()
m.setBackgroundColor(16384)
m.setTextScale(1)
m.setCursorPos(1,1)
m.write("You may")
m.setCursorPos(1,2)
m.write(" enter ")
m.setCursorPos(1,3)
m.setTextColor(32)
m.write("  ONLY    ")
m.setCursorPos(1,4)
m.write("  WITH	 ")
m.setTextColor(1)
m.setCursorPos(1,5)
m.write(" perms!  ")
m.setCursorPos(1,6)
m.write("	   ")
sleep(2)
end
x = 1
while true do
while not redstone.getInput("right",true) do
sleep(.2)
end
if x <= 1.5 and true then
x=2
nopass()
else
x=1
pass()
end
end
Zeoic #5
Posted 18 November 2013 - 10:06 AM
if x == 1 then
pass()

else
nopass()
end

It was this if right here. essentially, before you fixed it, you had it always make it swap x
since pass() sets x to 2 and nopass() set x to 1 it was doing:

if x == 1 then
  x = 2
else
  x = 1
end
It not being in the loop, made it do it only once. So your script just waited for an input, then swapped the x from 1 to 2 or 2 to 1 and ended the program.

A better way to check for that button press would be:

while true do
  if redstone.getInput("right",true) then
	pass()
	sleep(3) -- Set 3 to how ever long you would like it to stay open
	nopass()
  else
	nopass()
  end
  sleep(0.1)
end
Edited on 18 November 2013 - 09:07 AM
jag #6
Posted 18 November 2013 - 03:39 PM
–snip–
I am not sure what he want this for, but if he wants an admin button on the inside to toggle the state my example code does it plenty well.