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

rednet + Lua help and GPS program help

Started by jadelade, 20 May 2012 - 01:52 AM
jadelade #1
Posted 20 May 2012 - 03:52 AM
Hay ppl i have be working on CC for a wile now and i know a (under my standards) a lot about lua API stuff like print(), write(), read(), if, then, repeat, until, =, ==, +, /, *, -, end, and a lil more but there are one or two things i need to know how it works.
1. I have found a bit of red net code that i was going to use in my IM program (I was going to use text = rednet.receive() print(text) but it only printed the senderID) but first i need to know a few things about it i will type it and make the parts that i want to know red.

term.clear()
print ("console "..os.computerID())
rednet.open("right")
x=1
while x == 1 do
action, senderID, text = os.pullEvent()
if action == "rednet_message" then
print( senderID )
print( text )
end
if action=="key" and senderID == 16 then
x=0
end
end

I know its a big request.

2. And last but not least local i seen a lot of local's in code and I think it mite be something i can use. pleas explain.

and what dues the GPS program do and how can it be used.

I hope this is not a donating task for anyone and thanks so much. :P/>/>
MysticT #2
Posted 21 May 2012 - 12:20 AM
1.
print ("console "..os.computerID())
this just prints the id of the computer, os.computerID() returns that, and it concatenates that to the string "console " (with the .. operator). So if your computer's id is 1 it would print "console 1".

action, senderID, text = os.pullEvent()
os.pullEvent() gets the next event in the queue (each time an event occurs it is stored in a queue), it returns the name of the event (a string that represents what it is) and any arguments it has. The arguments for the "rednet_message" event are the id of the sender and the received message.

if action == "rednet_message" then
check if the event is a rednet message.

print( senderID )
print( text )
prints the arguments of the "rednet_message" event (the sender id and the message).

if action=="key" and senderID == 16 then
checks if the event is a key press ("key" event) and the key pressed (the argument of the "key" event) is 16 (the letter q). This could be also done with a "char" event like: if action == "char" and senderID == "q" then

2. local makes your variables local to the block it's on (a block could be a function, a loop, an if statement, the whole program, etc.), so when it ends the variable is "erased". It's good practice to make your variables local, it helps avoid problems between programs or the program itself.

The gps program is used to host a gps tower (a computer that sends its location to anyone who asks for it) or to get your own location (the one of the computer, not the player).
Bossman201 #3
Posted 21 May 2012 - 12:33 AM
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.
MysticT #4
Posted 21 May 2012 - 12:47 AM

only printing the senderID is because the command rednet.receive() returns two strings, one is the senderID and the second is their text.
the id received is a number, not a string.


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
the key codes are not ASCII, the ASCII value for q is not 16. You can check the key codes here (CC uses the same as minecraft).

Just wanted to point out those things, don't want to confuse anyone. Your explanations are more detailed, it's good to see you learned that much :P/>/>.
jadelade #5
Posted 21 May 2012 - 12:47 AM
Thanks mystic and boss that help a lot and boss sorry i was in a rush.
Bossman201 #6
Posted 21 May 2012 - 01:20 AM
Just one more thing, if you're planning on creating an IM program, avoid io.read() at all costs. It halts your program at that line until you press enter. Meaning that if someone sends a message and you're still stuck on io.read() you won't receive it.

Pay special attention to the functions:
handleEvent() - "sorts" events and "sends" the program them to the proper place.
writeChar() - handles writing, saving each character in an array, virtually the same as io.read() but without halting your program; allowing you to keep receiving messages while typing.
handleSend() - "decodes" the array back into a string and sends it to messageSend() for sending.

If there's something inside the parentheses, that is the variable passed to the function unless it's during the function definition, which then means that those are the parameters the function will accept. Mystic did a really good job of explaining it on my topic, as I sort of slept through that lecture in high school computer science class.
jadelade #7
Posted 21 May 2012 - 01:23 AM
i have CodeRunner and Lua on my computer and i use CodeRunner to edit the scripts but wen i use stuff like rednet and other inputs I usually use the in game editor and i have my own style if formatting code


print("Hello PASSWORD PLEAS")

io.write("=( ")

pass = io.read("*")

if pass == ("jadelade") then
		   print("hello jadelade")
elseif pass == ("bossman201") then
		   print("hello boss")
else
           print("go home")
end

print("lol")
print(" ")
print("what")

I recommend making a youtube vid to teach people this stuff with voice do to the fact that people like me are illiterate XD and seeing is one of the best ways to learn .
jadelade #8
Posted 21 May 2012 - 01:44 AM
thanks more lol