33 posts
Posted 14 June 2013 - 05:18 PM
Hi,
In my ever failing attempts to create a computer craft controlled train station I mad a short test track and program which when a cart went over a detector rail it sent a redstone signal to the computer which then sent it through rednet to one computer which read out what the station was called. This program works fine as someone else on this forum helped me out with it. I now want to make it so I can change the stations name which i've managed to do myself. When you run the program it asks you for the stations name and then continues the rest with the station name chosen. I want to be able to change the stations name whilst the program is running. So by pressing a key on the keyboard it will again ask you to enter a station name and therefore change the variable data heres my attempt but as I'm rubbish at coding it didn't work. It does however bring up the enter station name thing it just doesn't send to the other computer.
while true do
local event, param1 = os.pullEvent("char")
if param1 == "n" then
rednet.open("left")
print("enter station name")
local st = read()
break
end
end
while true do
os.pullEvent("redstone")
if rs.getInput("back") then
rednet.send(18, st)
end
end
And this is the original code without my attempt at the key press thing.
rednet.open("left")
print("enter station name")
local st = read()
while true do
os.pullEvent("redstone")
if rs.getInput("back") then
rednet.send(18, st)
end
end
1583 posts
Location
Germany
Posted 14 June 2013 - 06:07 PM
--Variables
local stationName = ""
local modemSide = someSide
local rsSide = someSide
local frequence = someFrequence
--Initialisation
rednet.open(modemSide)
--Functions
local function waitForKeypress()
while true do
local sEvent, sKey = os.pullEvent()
if sEvent == "key" and sKey == keys.enter then
term.write("New Name:")
stationName = read()
end
end
end
local function waitForRs()
while true do
if rs.getInput(rsSide) then
rednet.send(frequence, stationName)
end
sleep(0)
end
end
--Main function
local function main()
parallel.waitForAny(waitForKeypress, waitForRs)
end
--BSoD
local _,err = pcall(main)
if err then
term.clear()
term.setCursorPos(1,3)
print("Some bad error has occured D:\n\n")
print(" " .. tostring(err) .. " ")
print("\n\npress any key to exit...")
while true do
local sEvent = os.pullEvent()
if sEvent == "key" then
term.clear()
term.setCursorPos(1,1)
print(os.version() .. "\n\n")
end
end
end
this should work…
7508 posts
Location
Australia
Posted 14 June 2013 - 10:21 PM
-badly indented code-
Indentation man… please use it…
33 posts
Posted 15 June 2013 - 06:44 AM
Thanks Freack100 it worked. How would you do it so it asks you to press enter to enter a station name first? Also is there a way to enter a computer id by using the read() function? Would you also be able to comment some of the lines of the code to just so i understand it better I learn best from things like that some code i can work out my self like rs.getInput is obviously going to look for a redstone input but things like the code in the main function and the BSoD section i don't quite understand. thanks again.
199 posts
Location
Switzerland
Posted 15 June 2013 - 09:02 AM
Hi Mike,
Hope the comments help somehow. Explenation for Pcall maybe is not so good for better information have a look
herenot sure if i got the question
How would you do it so it asks you to press enter to enter a station name first?
but i added a print that should do what you asked for
--Variables
local stationName = ""
local modemSide = someSide
local rsSide = someSide
local frequence = someFrequence
--Initialisation
rednet.open(modemSide)
--Functions
local function waitForKeypress()
while true do
local sEvent, sKey = os.pullEvent() -- waits for an event
if sEvent == "key" and sKey == keys.enter then --if event is key and the key is Enter do sth
term.write("New Name:") --- asks for a new name
stationName = read() -- reads the input and saves it to variable
end
end
end
local function waitForRs()
while true do -- endless loop
if rs.getInput(rsSide) then -- wait for rsinput on given side
rednet.send(frequence, stationName) -- sends rednetmessage on given frequency with stationname as text
end
sleep(0)
end
end
--Main function
local function main()
print("press enter to change Station name")
parallel.waitForAny(waitForKeypress, waitForRs) --runs both functions simultanly and waits that one of them returns
end
--BSoD
local _,err = pcall(main) --pcall is used to see if the function produced any errors insteadt of running on normal error first variable _ will result in false and err will contain error details. Makes it possible to generate custom error Screen (BSoD)
if err then -- if there is an error message do sth.
term.clear()
term.setCursorPos(1,3)
print("Some bad error has occured D:\n\n")
print(" " .. tostring(err) .. " ") -- print the error
print("\n\npress any key to exit...")
while true do -- endless loop
local sEvent = os.pullEvent() -- as above wait for event
if sEvent == "key" then -- this time just wait for any key event no matter what key
term.clear()
term.setCursorPos(1,1)
print(os.version() .. "\n\n") -- print version of the os
end
end
end
7508 posts
Location
Australia
Posted 15 June 2013 - 09:04 AM
the BSoD section i don't quite understand.
Have a read of my
BSoD tutorial.
33 posts
Posted 15 June 2013 - 11:04 AM
so how would you code it so that it asks for the computers ID first? I've tried using the read() in a variable but it expects a number on the rednet.send() bit. Thanks for the comments though they helped just want to be able to tell it what computer ID to send to without having to edit the program.
7508 posts
Location
Australia
Posted 15 June 2013 - 11:11 AM
there is a function called tonumber which converts strings into numbers.
local function readNumber()
write("Input an ID: ")
local input = read()
while not tonumber(input) do
print("not a number")
write("Input an ID: ")
input = read()
end
return input
end
892 posts
Location
Where you'd least expect it.
Posted 15 June 2013 - 12:34 PM
there is a function called tonumber which converts strings into numbers.
local function readNumber()
write("Input an ID: ")
local input = read()
while not tonumber(input) do
print("not a number")
write("Input an ID: ")
input = read()
end
return input
end
You missed return tonumber(input), you just had return input.
33 posts
Posted 15 June 2013 - 04:37 PM
how would I add that function to this code (i've already attempted it but failed…again..)
–Variables
local stationName = ""
local modemSide = someSide
local rsSide = someSide
local frequence = someFrequence
–Initialisation
rednet.open("left")
–Functions
local function id()
write("Input an ID: ")
local input = read()
while not tonumber(input) do
print("not a number")
write("Input an ID: ")
input = read()
end
return input
end
print("press enter to enter/change station name")
local function waitForKeypress()
while true do
local sEvent, sKey = os.pullEvent()
if sEvent == "key" and sKey == keys.enter then
term.write("Station Name: ")
stationName = read()
end
end
end
local function waitForRs()
while true do
os.pullEvent("redstone")
if rs.getInput("back") then
rednet.send(id, "A train is at "..stationName)
else rednet.send(id, "train has left "..stationName)
end
end
sleep(0)
end
–Main function
local function main()
parallel.waitForAny(waitForKeypress, waitForRs)
end
–BSoD
local _,err = pcall(main)
if err then
term.clear()
term.setCursorPos(1,3)
print("Some bad error has occured D:\n\n")
print(" " .. tostring(err) .. " ")
print("\n\npress any key to exit…")
while true do
local sEvent = os.pullEvent()
if sEvent == "key" then
term.clear()
term.setCursorPos(1,1)
print(os.version() .. "\n\n")
end
end
end
7083 posts
Location
Tasmania (AU)
Posted 15 June 2013 - 07:28 PM
It's not working because your id() function isn't a returning a number. As Mudkip points out, the "return" statement at the end of it needs to be changed from:
return input
… to:
return tonumber(input)
Furthermore, your rednet.send statements are trying to send to "id". "id" represents your function - you want to
run that function and send to the
result, which is done by referring to id() instead:
rednet.send(id(), "A train is at "..stationName)
Or maybe just run the function once as the program starts up, and store the result in a variable somewhere so the user doesn't need to re-enter it over and over:
targetID = id()
.
.
.
rednet.send(targetID, "A train is at "..stationName)
1583 posts
Location
Germany
Posted 15 June 2013 - 07:45 PM
Okay, you want to set the station name and the id seperate?
--Variables
local stationName = ""
local id = 0
local modemSide = someSide
local frequence = someFrequence
local rsSide = someSide
--Initialisation
rednet.open(modemSide)
--getting the station name
stationName = read()
--getting the id
id = read()
while not tonumber(id) do
print("Not a number")
id = read()
end
id = tonumber(id)
--Functions
local function changeName()
stationName = read()
end
local function changeId()
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 fuction waitForRs()
while true do
if rs.getInput(rsSide) then
--Your message sending here
end
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
press i to edit the ID, and n to edit the station name
33 posts
Posted 16 June 2013 - 09:23 AM
i tried that code but when i place a cart on the detector track it gives an error:
parallel:22: st:50: attempt to call number
Also how would i make it so it prints on the screen "enter ID" and "enter station name" ?
33 posts
Posted 16 June 2013 - 09:27 AM
its ok i've managed to figure it out thanks for the help. :)/>