简体   繁体   中英

Having trouble with some computercraft/lua code

Hi I want my lua code in Computercraft to allow the user to turn the redstone signal on/off by right clicking on a monitor on top, but I can't get it to work.

monitor = peripheral.wrap("top")
monitor.clear()
monitor.setTextColor(colors.red)
monitor.setCursorPos(1, 1)
monitor.setTextScale(1)
monitor.write("Hello")

function rubber()
    monitor.setCursorPos(1, 2)
    monitor.clearLine()

    if rs.getOutput("right", true) then
        monitor.write("Rubber farm is on")
    elseif rs.getOutput("right", false) then
        monitor.write("Rubber farm is off")
    end

    local event = { os.pullEvent() }

    if event == "monitor_touch" then
        if rs.getOutput("right") == true then
            rs.setOutput("right", false)
        else
            rs.setOutput("right", true)
        end
    else
        write("test")
    end

    rubber()
end

Right now all it displays is 'hello' and I don't know how to fix it, anyone know how? Also I'm a beginner at Lua so I've probably made some pretty simple mistakes. Thanks

local event = { os.pullEvent() }
if event == "monitor_touch" then

os.pullEvent returns a tuple. In your code, you're packing this tuple into a table. That's fine, but you then compare that table to a string. Tables can't be equal to strings - they're a table. Either don't pack the tuple into a table, and keep the first return value (the type):

local event = os.pullEvent()
if event == "monitor_touch" then

Or extract the first element when comparing

local event = { os.pullEvent() }
if event[1] == "monitor_touch" then

The problem is you wanted to have that function infinitly looping, but you have not called your function outside your function.... also you should look into using while loops

while true do
 //stuff here
end

just add

rubber()

to the last line after your last end tag.

You have to call the function. rubber()

You need to close your function

function rubber()

  monitor.setCursorPos(1,1)

  monitor.clearLine()
end

The end is it you need to make this little word

这是一个简单的修复程序,只需在完成功能橡胶后添加rubber(),因为在创建功能橡胶时,您尚未要求它启动。

The "monitor_touch" event is what you should be using. Also, make sure the monitor you are using is an advanced monitor (the one with the yellow border).

If you need help in understanding the event, check out this page: http://computercraft.info/wiki/Monitor_touch_(event)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM