0

我正在尝试使用计算机技术在 Tekkit 上制作计算机系统/网络。

Computecraft 是一个基于 Lua 的修改,在 Minecraft/Tekkit 和其他修改包中运行。

基本上,我目前正在使用无线路由器来执行此操作:

rednet.open('top') -- Open Connection to the wireless router
print ("test")
input = read()
rednet.receive()  -- Listen for messages send/broadcasted 

if message then
 print (message)
end

我正在尝试对我的所有系统进行更改,如下所示:

rednet.open ('top')
 -- Do all normal stuff

rednet.receive()
if message == "Lock202" then 
 os.pullEvent = os.pullEventRaw
 term.clear()
 term.setCursorPos(1,1)
 print ("Alert In Progress, Terminal Locked")
end

通过做所有正常的事情,我希望用户能够导航和使用计算机的功能。但是当调用rednet.receive()它时,它会冻结并等待传入​​消息。

我希望它在后台运行,并且仅在收到消息时才采取行动。

我试过查看文档,帮助站点。并考虑将这个问题带到 SO,因为由于可用的 Lua 编码器的范围。

4

3 回答 3

4

http://computercraft.info/wiki/Parallel.waitForAll

这是一个基本上可以多线程你的程序的函数......所以你可以......

function listen()
 while true do
  id, msg, distance = rednet.receive()
  FUNCTION_THAT_RUNS_STUFF(id, msg, distance)
  sleep(1)
 end
end

function main_loop()
 while true do
  --do your other stuff in here
 end
end

--end of file run everything
parrallel.waitForAll(listen, main_loop)
于 2014-01-13T06:24:09.507 回答
0

我所做的是我有 1 台电脑运行 os.pullEvent,还有很多其他的电脑收集信息并通过 rednet 发送,这使得只有一台电脑像这样卡住了,尽管其他电脑几乎无法使用,当然,如果你做了我所做的,你可以让它检测密钥和 rednet_message 等等。

于 2014-12-19T15:42:12.740 回答
0

命令 rednet.receive() 的参数中可以有一个参数,即“超时”。

这是以秒为单位的。它也是一个浮点数(十进制),例如 4.50、1.23。等等

这意味着它将在该时间段内收到。

完成您想要的事情的一个好方法是让另一台计算机不断接收消息,然后向您想要进行模块化接收和写入的计算机发出红石信号

function Check()
  If rs.getInput("back") then
     local id, message = rednet.receive(5)
     print("Receiving Message")
  end
end

另一台计算机会这样做:

computerid = 50

id, message = rednet.receive()
  rs.setOutput("back",true)
  sleep(1)
  rednet.send(computerid, message)
  rs.setOutput("back",false)

computerid 将等于您要运行的原始计算机的 ID。您还必须在运行代码时定期使用 Check() 函数,除非收到消息,否则它不会影响计算机,在这种情况下,它会在“rednet.receive”中指定的时间内接收论据。

希望这对您有所帮助

——艾威尔道

于 2013-05-10T08:13:32.120 回答