我正在尝试使用computercraft在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()
时,它会冻结并等待传入的消息。
我想让它在后台运行,并且只在收到消息时执行。
我试着查看了文档和帮助站点。我想把这个问题带到这里,因为Lua编码器的范围很广。
发布于 2014-01-13 14:24:10
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)
发布于 2013-05-10 16:13:32
命令rednet.receive()可以在其参数中有一个参数,即“超时”。
这是以秒为单位的。它也是一个浮点数(十进制),例如4.50,1.23。等。
这意味着它将在该时间段内接收。
实现您想要的功能的一个好方法是让另一台计算机不断地接收消息,然后向您想要进行模块化接收和写入的计算机发出redstone信号。
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”的参数中指定的时间量内收到消息。
希望这能对你有所帮助
--EwilDawe
发布于 2014-12-19 23:42:13
我所做的是我有一台运行os.pullEvent的电脑,还有一大堆其他的电脑收集信息并通过rednet发送信息,这使得只有一台电脑像这样被卡住了,尽管其他的电脑非常不常用,当然,如果你像我一样做,你可以让它检测按键和rednet_message等等。
https://stackoverflow.com/questions/15673312
复制相似问题