EDIT: Got sniped by MysticT. He helped me a lot with writing my program so listen to him because he knows what he's talking about. And thanks Mystic!
First things first, please properly format your code. This could be the reason nobody's responded to this after almost 24 hours. I'll show you how I format my code
Spoiler
term.clear()
print("Console " .. os.computerID())
rednet.open("right")
x = 1
while x == 1 do
action, senderID, test = os.pullEvent()
if action == "rednet_message" then
print(senderID)
print(text)
end
if action == "key" and senderID == 16 then
x = 0
end
end
Okay now that's out of the way. The problem you were having with
text = rednet.receive()
print(text)
only printing the senderID is because the command rednet.receive() returns two strings, one is the senderID and the second is their text. You should have written the code like this
senderID, text = rednet.receive()
print(senderID .. " " .. text)
I'll make a list going from the top to the bottom to explain all of the red marks.
1. This command outputs "console". The double periods is called string concatenation, which is just adding the strings together (using + or - on strings will return an error in lua). Last, the os.computerID() function will return the ID of the computer. Computer ID's are pretty simple, the first computer you make has an ID of 0 and each one you make and place after that will increment by 1. So ID 0, ID 1, ID 2. Note: os.computerID() will return a number representing the ID of your computer, not "ID 0", just "0".
2. These are the first 3 parameters that are returned from the function os.pullEvent().
"action" is representing the event that is returned. For a complete list of events, type "help events" in your computer. You should rename this to "event" or "evt" in larger projects to avoid confusion.
"senderID" is the second parameter of os.pullEvent(). In this case, it's okay to use "senderID" as the string name but for larger projects just name it "p2" or "param2".
"text", this is the message that was sent. This will only have something in it if the operating system detects a "rednet_receive" event. You should also rename this to "p3" or "param3" in larger projects.
3, 4, 5. Reading #2 should explain this in detail.
6. "if action == "key" and senderID == 16" this is why I said you should rename those parameters returned from os.pullEvent() to "evt", "p1", "p2".
evt, p1, p2 = os.pullEvent()
In the "key" event, the ASCII value of a key is returned in p2, which is called senderID in this code. ASCII 16 is the code for the letter "q" so that code could also be written like
if action == "char" and senderID == "q" then
It's fine the way it is, but this should make it easier to understand…or not.
7. "local" is a variable declaration. Typically, it's a good thing to declare your variables in the first lines of code before using them. Another type of declaration is "global" but if you don't know what that's for, you don't need to use it.
8. As for the GPS program, I don't know how to use it; but after you learn the syntax in lua and learn more about programming, you should be able to open the GPS program in notepad++ (which you should be using rather than the in-game editor) and understand how it works, and what parameters you can use and parameters that are returned.
For an EXTREMELY basic and simple IM program to learn from, try this, I wrote it myself and it's completely functional. (make sure you edit the code if your wireless modem isn't on the right side, I had the hardest time bug testing because one of my computers has it's modem on the top for aesthetic purposes)
Spoiler
local cursorY = 17
local username = tostring(os.getComputerLabel)
local cMessage = ""
local hMessage = ""
local charNum = 1
local message = {}
local pastCount = 1
local pastMess = {}
local capOn = false
rednet.open("right")
for i = 1, 48 do
message[i] = ""
end
for i = 1, 12 do
pastMess[i] = ""
end
function clearScreen()
term.clear()
term.setCursorPos(1,1)
end
function resetMessage()
for i = 1, 48 do
message[i] = ""
end
charNum = 1
end
function updatePast(newMess)
for i = 1, 11 do
pastMess[i] = pastMess[i + 1]
end
pastMess[12] = newMess
updateScreen()
end
function updateScreen()
for i = 4, 15 do
term.setCursorPos(1, i)
term.clearLine()
end
for i = 4, 15 do
term.setCursorPos(1, i)
term.write("|")
term.setCursorPos(50, i)
term.write("|")
end
for i = 12, 1, -1 do
term.setCursorPos(2, i + 3)
term.write(pastMess[i])
end
end
function Border()
clearScreen()
for i = 2, 49 do
term.setCursorPos(i, 1)
term.write("-")
term.setCursorPos(i, 3)
term.write("-")
term.setCursorPos(i, 16)
term.write("-")
term.setCursorPos(i, 18)
term.write("-")
end
for i = 1, 18 do
term.setCursorPos(1, i)
term.write("|")
term.setCursorPos(50, i)
term.write("|")
end
term.setCursorPos(2, 2)
term.write("BossChat v.005")
end
function handleBackspace()
if charNum + 1 >= 3 then
charNum = charNum - 1
message[charNum] = " "
term.setCursorPos(charNum + 1, cursorY)
term.write(" ")
term.setCursorPos(charNum + 1, cursorY)
elseif charNum <= 1 then
charNum = 1
end
end
function handleSend(message)
for i = 1, charNum do
hMessage = hMessage .. message[i]
end
term.setCursorPos(1, 17)
term.clearLine()
term.setCursorPos(1, 17)
term.write("|")
term.setCursorPos(50, 17)
term.write("|")
resetMessage()
if string.find(hMessage, "/") == 1 then
handleCommand(hMessage)
elseif hMessage ~= "" then
updatePast(hMessage)
messageSend(hMessage)
end
return hMessage
end
function handleCaps(capOn)
if capOn == false then
term.setCursorPos(20, 2)
term.write("CAPS ON")
capOn = true
elseif capOn == true then
term.setCursorPos(20, 2)
term.write(" ")
capOn = false
end
end
function handleCommand(command)
command = string.lower(command)
if command == "/exit" then
term.setCursorPos(2, 17)
term.write("Thank you for using. Rebooting")
for i = 1, 5 do
term.write(".")
sleep(0.30)
end
hMessage = ""
os.reboot()
end
end
function writeChar(char)
term.setCursorPos(charNum + 1, cursorY)
if charNum <= 48 then
term.write(char)
message[charNum] = char
charNum = charNum + 1
end
end
function handleKey(key)
if key == 58 then
handleCaps(capOn)
elseif key == 14 then
handleBackspace()
elseif key == 28 then
hMessage = handleSend(message)
hMessage = ""
end
end
function messageReceive(id, message)
cMessage = message
updatePast(cMessage)
end
function messageSend(hMessage)
rednet.broadcast(hMessage)
end
function handleEvent()
evt, p1, p2 = os.pullEvent()
if evt == "char" then
writeChar(p1)
elseif evt == "key" then
handleKey(p1)
elseif evt == "rednet_message" then
messageReceive(p1, p2)
end
end
clearScreen()
Border()
term.setCursorPos(2, 17)
while true do
handleEvent()
end
EDIT #2: Sorry about no comments to explain things. I don't usually work with other people on projects and I wrote all of the code so I know what it does. Just PM any questions like it says below.
If you have any questions feel free to PM me, I think this forum has a PM system.