7 posts
Posted 12 April 2013 - 11:57 AM
I have a little problem with my code. I need to run a loop in a background but I dont know how! I got the program to work but i need to type when the loop is going.
I need to run this :
while true do
event, id, text = os.pullEvent()
if event == "rednet_message" then
print(id .. "> " ..text)
end
end
in this code:
local comp = 0
local a
local mail
local mainA
local conId
local conText
parallel.waitForAny(test, function() end)
rednet.open("right")
function main()
shell.run("clear")
term.setCursorPos(15, 19)
print("'m' = new message 'o' = open mail")
term.setCursorPos(1, 1)
write("What do you want?")
mainA = read()
if mainA == "m" then
Cid()
message()
end
if mainA == "o" then
Tead()
end
if mainA == "c" then
convert()
end
if mainA == "close" then
os.reboot()
end
end
function Cid()
print("Whats your computer id you want to send to?")
comp = tonumber(read())
end
function message()
print("Type in what you want to say to them")
mail = read()
rednet.send(comp, mail)
end
function Tead()
print("Do you want to open email?")
a = read()
if a == "yes" then
shell.run("print")
sleep(60)
else
shell.run("clear")
shell.run("email")
end
end
function convert()
print("Who do you want to talk to?")
conId =tonumber(read())
convert2()
mailCheck()
end
function convert2()
print("What do you want to say?")
conText = read()
rednet.send(conId, conText)
print("Want to send another message?")
k = read()
if k == "yes" then
shell.run("clear")
convert2()
mailCheck()
end
if k == "no" then
shell.run("clear")
main()
end
end
function test()
while true do
event, id, text = os.pullEvent()
if event == "rednet_message" then
print(id.. ">" ..text)
end
end
end
function mailCheck()
shell.run("print")
end
while true do
shell.run("print")
end
main()
Thanks
1190 posts
Location
RHIT
Posted 12 April 2013 - 12:07 PM
I feel like using the parallel API is a bit unnecessary here. Have you considered writing a custom read function that will accept rednet at the same time? A bit like this:
local function customRead()
local str = ""
while true do
local e = {os.pullEvent()}
if e[1] == "rednet_message" then
--Do something upon receiving a message
elseif e[1] == "char" then
str = str..e[2]
elseif e[1] == "key" then
if e[2] == keys.enter then
return str
end
end
end
end
Obviously it doesn't have backspace and such, but it's not that hard to do.
7 posts
Posted 12 April 2013 - 12:17 PM
I feel like using the parallel API is a bit unnecessary here. Have you considered writing a custom read function that will accept rednet at the same time? A bit like this:
local function customRead()
local str = ""
while true do
local e = {os.pullEvent()}
if e[1] == "rednet_message" then
--Do something upon receiving a message
elseif e[1] == "char" then
str = str..e[2]
elseif e[1] == "key" then
if e[2] == keys.enter then
return str
end
end
end
end
Obviously it doesn't have backspace and such, but it's not that hard to do.
Can you put this in my code because I don't understand it.
while true doevent, id, text = os.pullEvent()if event == "rednet_message" thenprint(id.. ">" ..text)endendThis prints it out the mail
1190 posts
Location
RHIT
Posted 12 April 2013 - 12:26 PM
Well I won't do all the work for you, but here's the jist of what my code is doing.
local function customRead()
local str = "" --This variable will keep the user input
while true do
local e = {os.pullEvent()} --Pull an event. An event can be anything: rednet_message, key press, char (the actual value of the pressed key), etc.
if e[1] == "rednet_message" then --If you receive a message then react to it however you want. You could call another function if you want.
--Do something upon receiving a message
elseif e[1] == "char" then --If they press a key and it is an actual character, this event will be triggered. Add it to the str variable.
str = str..e[2]
elseif e[1] == "key" then --If they press a key then...
if e[2] == keys.enter then --Check to see if it is the enter key. If so, return the value of the str variable
return str
end
end
end
end
If I'm still not making sense, tell me more specifically what part and I'll try to help you out.
7 posts
Posted 12 April 2013 - 01:03 PM
Well I won't do all the work for you, but here's the jist of what my code is doing.
local function customRead()
local str = "" --This variable will keep the user input
while true do
local e = {os.pullEvent()} --Pull an event. An event can be anything: rednet_message, key press, char (the actual value of the pressed key), etc.
if e[1] == "rednet_message" then --If you receive a message then react to it however you want. You could call another function if you want.
--Do something upon receiving a message
elseif e[1] == "char" then --If they press a key and it is an actual character, this event will be triggered. Add it to the str variable.
str = str..e[2]
elseif e[1] == "key" then --If they press a key then...
if e[2] == keys.enter then --Check to see if it is the enter key. If so, return the value of the str variable
return str
end
end
end
end
If I'm still not making sense, tell me more specifically what part and I'll try to help you out.
Its still not working it just running the loop!
1190 posts
Location
RHIT
Posted 12 April 2013 - 01:04 PM
Well I won't do all the work for you, but here's the jist of what my code is doing.
local function customRead()
local str = "" --This variable will keep the user input
while true do
local e = {os.pullEvent()} --Pull an event. An event can be anything: rednet_message, key press, char (the actual value of the pressed key), etc.
if e[1] == "rednet_message" then --If you receive a message then react to it however you want. You could call another function if you want.
--Do something upon receiving a message
elseif e[1] == "char" then --If they press a key and it is an actual character, this event will be triggered. Add it to the str variable.
str = str..e[2]
elseif e[1] == "key" then --If they press a key then...
if e[2] == keys.enter then --Check to see if it is the enter key. If so, return the value of the str variable
return str
end
end
end
end
If I'm still not making sense, tell me more specifically what part and I'll try to help you out.
Its still not working it just running the loop!
Ah sorry. Forgot to make it actually display the text. Just added a simple term.write(e[2]) under the str = str..e[2]
local function customRead()
local str = "" --This variable will keep the user input
while true do
local e = {os.pullEvent()} --Pull an event. An event can be anything: rednet_message, key press, char (the actual value of the pressed key), etc.
if e[1] == "rednet_message" then --If you receive a message then react to it however you want. You could call another function if you want.
--Do something upon receiving a message
elseif e[1] == "char" then --If they press a key and it is an actual character, this event will be triggered. Add it to the str variable.
str = str..e[2]
term.write(e[2])
elseif e[1] == "key" then --If they press a key then...
if e[2] == keys.enter then --Check to see if it is the enter key. If so, return the value of the str variable
return str
end
end
end
end
7 posts
Posted 12 April 2013 - 01:14 PM
The text wont come up!
7 posts
Posted 12 April 2013 - 01:25 PM
Well I won't do all the work for you, but here's the jist of what my code is doing.
local function customRead()
local str = "" --This variable will keep the user input
while true do
local e = {os.pullEvent()} --Pull an event. An event can be anything: rednet_message, key press, char (the actual value of the pressed key), etc.
if e[1] == "rednet_message" then --If you receive a message then react to it however you want. You could call another function if you want.
--Do something upon receiving a message
elseif e[1] == "char" then --If they press a key and it is an actual character, this event will be triggered. Add it to the str variable.
str = str..e[2]
elseif e[1] == "key" then --If they press a key then...
if e[2] == keys.enter then --Check to see if it is the enter key. If so, return the value of the str variable
return str
end
end
end
end
If I'm still not making sense, tell me more specifically what part and I'll try to help you out.
Its still not working it just running the loop!
Ah sorry. Forgot to make it actually display the text. Just added a simple term.write(e[2]) under the str = str..e[2]
local function customRead()
local str = "" --This variable will keep the user input
while true do
local e = {os.pullEvent()} --Pull an event. An event can be anything: rednet_message, key press, char (the actual value of the pressed key), etc.
if e[1] == "rednet_message" then --If you receive a message then react to it however you want. You could call another function if you want.
--Do something upon receiving a message
elseif e[1] == "char" then --If they press a key and it is an actual character, this event will be triggered. Add it to the str variable.
str = str..e[2]
term.write(e[2])
elseif e[1] == "key" then --If they press a key then...
if e[2] == keys.enter then --Check to see if it is the enter key. If so, return the value of the str variable
return str
end
end
end
end
The text wont come up!
1190 posts
Location
RHIT
Posted 12 April 2013 - 01:34 PM
Well I won't do all the work for you, but here's the jist of what my code is doing.
local function customRead()
local str = "" --This variable will keep the user input
while true do
local e = {os.pullEvent()} --Pull an event. An event can be anything: rednet_message, key press, char (the actual value of the pressed key), etc.
if e[1] == "rednet_message" then --If you receive a message then react to it however you want. You could call another function if you want.
--Do something upon receiving a message
elseif e[1] == "char" then --If they press a key and it is an actual character, this event will be triggered. Add it to the str variable.
str = str..e[2]
elseif e[1] == "key" then --If they press a key then...
if e[2] == keys.enter then --Check to see if it is the enter key. If so, return the value of the str variable
return str
end
end
end
end
If I'm still not making sense, tell me more specifically what part and I'll try to help you out.
Its still not working it just running the loop!
Ah sorry. Forgot to make it actually display the text. Just added a simple term.write(e[2]) under the str = str..e[2]
local function customRead()
local str = "" --This variable will keep the user input
while true do
local e = {os.pullEvent()} --Pull an event. An event can be anything: rednet_message, key press, char (the actual value of the pressed key), etc.
if e[1] == "rednet_message" then --If you receive a message then react to it however you want. You could call another function if you want.
--Do something upon receiving a message
elseif e[1] == "char" then --If they press a key and it is an actual character, this event will be triggered. Add it to the str variable.
str = str..e[2]
term.write(e[2])
elseif e[1] == "key" then --If they press a key then...
if e[2] == keys.enter then --Check to see if it is the enter key. If so, return the value of the str variable
return str
end
end
end
end
The text wont come up!
Are you calling the function?
7 posts
Posted 13 April 2013 - 01:56 AM
Well I won't do all the work for you, but here's the jist of what my code is doing.
local function customRead()
local str = "" --This variable will keep the user input
while true do
local e = {os.pullEvent()} --Pull an event. An event can be anything: rednet_message, key press, char (the actual value of the pressed key), etc.
if e[1] == "rednet_message" then --If you receive a message then react to it however you want. You could call another function if you want.
--Do something upon receiving a message
elseif e[1] == "char" then --If they press a key and it is an actual character, this event will be triggered. Add it to the str variable.
str = str..e[2]
elseif e[1] == "key" then --If they press a key then...
if e[2] == keys.enter then --Check to see if it is the enter key. If so, return the value of the str variable
return str
end
end
end
end
If I'm still not making sense, tell me more specifically what part and I'll try to help you out.
Its still not working it just running the loop!
Ah sorry. Forgot to make it actually display the text. Just added a simple term.write(e[2]) under the str = str..e[2]
local function customRead()
local str = "" --This variable will keep the user input
while true do
local e = {os.pullEvent()} --Pull an event. An event can be anything: rednet_message, key press, char (the actual value of the pressed key), etc.
if e[1] == "rednet_message" then --If you receive a message then react to it however you want. You could call another function if you want.
--Do something upon receiving a message
elseif e[1] == "char" then --If they press a key and it is an actual character, this event will be triggered. Add it to the str variable.
str = str..e[2]
term.write(e[2])
elseif e[1] == "key" then --If they press a key then...
if e[2] == keys.enter then --Check to see if it is the enter key. If so, return the value of the str variable
return str
end
end
end
end
The text wont come up!
Are you calling the function?
Yes I am
local comp = 0
local a
local mail
local mainA
local conId
local conText
parallel.waitForAny(test, function() end)
rednet.open("right")
function main()
customRead()
shell.run("clear")
term.setCursorPos(15, 19)
print("'m' = new message 'o' = open mail")
term.setCursorPos(1, 1)
write("What do you want?")
mainA = read()
if mainA == "m" then
Cid()
message()
end
if mainA == "o" then
Tead()
end
if mainA == "c" then
convert()
end
if mainA == "close" then
os.reboot()
end
end
function Cid()
print("Whats your computer id you want to send to?")
comp = tonumber(read())
end
function message()
print("Type in what you want to say to them")
mail = read()
rednet.send(comp, mail)
end
function Tead()
print("Do you want to open email?")
a = read()
if a == "yes" then
shell.run("print")
sleep(60)
else
shell.run("clear")
shell.run("email")
end
end
function convert()
print("Who do you want to talk to?")
conId =tonumber(read())
convert2()
mailCheck()
end
function convert2()
print("What do you want to say?")
conText = read()
rednet.send(conId, conText)
print("Want to send another message?")
k = read()
if k == "yes" then
shell.run("clear")
convert2()
mailCheck()
end
if k == "no" then
shell.run("clear")
main()
end
end
--function test()
--while true do
--event, id, text = os.pullEvent()
--event == "rednet_message" then
--print(id.. ">" ..text)
--end
--end
function mailCheck()
shell.run("print")
end
local function customRead()
local str = ""
while true do
-- test()
local e = {os.pullEvent()}
if e[1] == "char" then
str = str..e[2]
term.write(e[2])
elseif e[1] == "key" then
if e[2] == keys.enter then
return str
end
end
end
end
main()
--rednet.send(id, mail)
1190 posts
Location
RHIT
Posted 13 April 2013 - 12:11 PM
Okay… well you're still using parallels, which is exactly the thing my function was designed to avoid.
It was designed to be used a bit like this:
local function handleMessage(message_table)
--This function would handle messages. So maybe you could add them to a table.
end
local function customRead()
term.setCursorBlink(true)
local str = ""
while true do
local e = {os.pullEvent()}
if e[1] == "key" then
if e[2] == keys.enter then
term.setCursorBlink(false)
return str
end
elseif e[1] == "char" then
str = str..e[2]
term.write(e[2])
elseif e[1] == "rednet_message" then
handleMessage(e) --Call the message handler
end
end
end
term.write("Message: ")
local yourMessage = customRead()
print("Sending: "..yourMessage.."to some computer.") --Handle however you want