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

program ends when long distance?

Started by mikesaa309, 17 June 2013 - 12:31 PM
mikesaa309 #1
Posted 17 June 2013 - 02:31 PM
so i have a program which works fine when i tested it when the computers where with in like a 10 block radius. The program tells me when a train is at a station as a way to help me control trains in my mc station i'm making. So the station is a fair distance from the control room and so i edited the config file to allow rednet to travel further how ever the program on the sending computer ends when ever i go back to the control room. I'm using teleporters from powercraft to go back and forth so i though maybe thats whats doing it so i flew back but still the same problem. I also tried using world anchors but no avail. also the program will say train has left station but not say its at station but in the test world the program works fine no problem. Heres the code for the sending computer:


–Variables
local stationName = ""
local id = 0
local modemSide = someSide
local frequence = someFrequence
local rsSide = someSide

–Initialisation
rednet.open("left")

–getting the station name
term.write("station name: ")
stationName = read()

–getting the id
term.write("Enter computer ID: ")
id = read()
while not tonumber(id) do
print("Not a number")
id = read()
end
id = tonumber(id)

term.write("Change name press n, press i to change ID")
–Functions
local function changeName()
print("Enter station name: ")
stationName = read()
end

local function changeId()
print("enter computer ID: ")
id = read()
while not tonumber(id) do
print("Not a number")
id = read()
end
id = tonumber(id)
end

local function waitForKeypress()
while true do
local sEvent, sKey = os.pullEvent()
if sEvent == "key" then
if sKey == keys.i then
changeId()
elseif sKey == keys.n then
changeName()
end
end
end
end

local function waitForRs()
os.pullEvent("redstone")
while true do
if rs.getInput("back") then
rednet.send(id,"train is at "..stationName)
os.pullEvent("redstone")
else rednet.send(id, "train has left "..stationName)
os.pullEvent("redstone")
end
sleep(1)
end
end




–Main function
local function main()
parallel.waitForAny(waitForKeypress, waitForRs)
end

–BSoD
local _,err = pcall(main)
if err then
print("Some bad error has occured D:\n\n")
print(" " .. tostring(err) .. " \n\n")
print("press any key to exit…")
while true do
local evt = os.pullEvent()
if evt == "key" then
break
end
end
end

and this is the code for the receiving computer (note it prints to a monitor so i can always see where trains are:


local side = ""
print("side of monitor: ")
side = read()
rednet.open("back")
mon = peripheral.wrap(side)
term.clear()
term.setCursorPos(1, 1)

local function changeSide()
print("Enter monitor side: ")
side = read()
end

local function waitForKeypress()
while true do
local event, param1 = os.pullEvent("char")
if param1 == "n" then
changeSide()
end
end
end


local pos = 1

while true do
local id, message, distance = rednet.receive()
mon.setCursorPos(1, pos)
pos = pos + 1
mon.write(message)
end

local function main()
parallel.waitForAny(waitForKeypress)
end

Also the program for the receiving computer remains running even if i leave the control room but its the program on the sending computer that seems to end without error when ever i leave the station. Why is it doing it?
Engineer #2
Posted 17 June 2013 - 03:20 PM
This problem has not to due with your code, but the problem is that the chunk is unloading and because that happens the computer shuts down.
You might want to consider placing chunkloaders
diegodan1893 #3
Posted 17 June 2013 - 03:21 PM
Computers shutdown when the chunk is unloaded. Did you put a world anchor next to each computer?

EDIT: BTW, did you read your old post?
mikesaa309 #4
Posted 17 June 2013 - 03:37 PM
yeah i put a world anchor next to both of them but still the same issue :/
mikesaa309 #5
Posted 17 June 2013 - 03:48 PM
never mind i'm such a dumb ass -.- I never really looked at a world anchor before so i though just placing it down worked. I now realize it needs a ender pearl to work *face palm -.\
also the reason the program never said "train is at station" was because the program is still trying to find a rs input from the back but this time its from top of the computer. Now i feel an idiot :/ All problems sorted now thanks for your help guys although is there not a way it could work without a world anchor because it may seriously lag my game if i have one at every station.
Sammich Lord #6
Posted 17 June 2013 - 03:51 PM
If you have Chicken Chunks you can get a Chunk Loader and only load the chunk it's in. So if you have 10 stations only 10 chunks will be loaded which isn't much at all.
diegodan1893 #7
Posted 17 June 2013 - 05:10 PM
Maybe it will be less laggy if you put a world anchor on the receiving computer and attach a world anchor to the train instead of placing it on each station, so only the chunks where the train is will be loaded. But for that you need to make sure your program will be loaded on startup, for example if you need to write the name of the station every time you start the program it won't work, but if you save the name to a file and then load it everytime the program starts, it will work perfectly.
apemanzilla #8
Posted 19 June 2013 - 04:58 PM
Also the stations may be too far apart for rednet to reach.