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

[Help] Computer rebooting some time after i run the code

Started by Liraal, 22 March 2012 - 05:32 PM
Liraal #1
Posted 22 March 2012 - 06:32 PM
okay, here is the code, i am trying to use runTerm()

local mode="node"
local nodes={}
local terms={}
local side="right"
local mem
local netp="123"
function Smode(a)
mode=a
end
function con(a)
for i=1, #terms, 1 do
if terms[i]==a then return true end
end
return false
end
function add(a,:(/>/>
if b==netp then table.insert(nodes, a) end
if b=="term" then table.insert(terms, a) end
end
function ping()
if mode=="term" then rednet.broadcast("ping") else rednet.broadcast(netp) end
os.startTimer(3)
local a,b,c=os.pullEvent()
while a~="timer" do
if a=="rednet_message" and (c==netp or c=="term") then add(b,c) end
end
end
function pong(b,c)
if mode=="node" and c==netp then
rednet.send(b, netp)
else
rednet.send(b,"term")
end
return true
end
function getID(message)
local id=""
local text=""
id=string.sub(message,3,4)
text=string.sub(message,5)
return tonumber(id), text
end
function route(a, r)
for i=1,#nodes,1 do
if nodes[i]~=r then
rednet.send(nodes[i],a)
end
end
return false
end
function relay(message, r)
local id, a=getID(message)
if not con(id) then
if message==mem then return false end
route(message, r)
else rednet.send(id, a)
end
end
function runNode()
rednet.open(side)
Smode("node")
ping()
while true do
a,b,c=os.pullEvent("rednet_message")
if c==netp or c=="ping" then pong(b,c)
else relay(c,:)/>/>
end
end
end
function runTerm()
rednet.open(side)
Smode("term")
ping()
while true do
a,b,c=os.pullEvent()
if a=="rednet_message" then
if c=="ping" then pong() else print(b," ",c) end
end
if a=="key" and b~=1 then local id=tonumber(read()) local message="00"..id..read() route(message, os.computerID()) end
end
end
Espen #2
Posted 22 March 2012 - 06:47 PM

while a~="timer" do
  if a=="rednet_message" and (c==netp or c=="term") then add(b,c) end
end
This loop will never end, because 'a' is never changed during the loop.
Change it to this to prevent an infinite loop:

if a~="timer" then
  if a=="rednet_message" and (c==netp or c=="term") then add(b,c) end
end
Liraal #3
Posted 22 March 2012 - 07:44 PM
i failed :(/>/>

while a~="timer" do
  if a=="rednet_message" and (c==netp or c=="term") then add(b,c) end
a,b,c=os.pullEvent()
end
is all it will take. Much thanks, Espen!