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

Rednet Message Problem

Started by Hepcat2299, 05 September 2012 - 02:59 AM
Hepcat2299 #1
Posted 05 September 2012 - 04:59 AM
Hey, guys. I am very new to Lua and coding in general, but I believe I am slowly grasping the concept. My question deserves a bit of a run down on the program I am trying to make. It is a simple Mineshaft digging program that sends stats such as: how far the turtle has gone, how far he has left, how many torches does he have left, and so on. I then wrote a program for the computer in my main base to redirect all that info onto a monitor (4x3). I created a loop that says
Spoilerfunction Repeat()

for i=0,11
do
local event,p1,p2 = os.pullEvent()
if event == "rednet_message" then
print(p2)
end

end

term.clear()
Repeat()
end


The loop repeats 11 times because there are 11 things that the turtle sends each time he sends data, this includes a line of "*" on the top and bottom.

Well….this does'nt work! The only way I was able to make it work was to actually have the turtle send 12 things instead of 11 (it adds an extra line of no text). That would work for a while and then the monitor would return a "java.lang.arrayIndexOutofBoundsException" error, then works again after rebooting the receiving Terminal.

My main goal is to have the monitor receive 11 lines through rednet, input them on the screen then right before it receives the next set of 11 lines it clears the previous and then writes the new set… I really hope that makes sense, if not let me know and I will try to explain a bit better!

Any help would be greatly appreciated!


edit: Just to give a bit more information. This is what the turtle is sending to the home Computer:

Spoilerrednet.send(0, "") —–Notice the extra line of no Information
rednet.send(0, " Currently Mining")
rednet.send(0, "*************************************")
rednet.send(0, "Blocks from Starting Position: " ..z)
rednet.send(0, "Total Blocks Moved: " ..x)
rednet.send(0, "Layers Left: " ..i)
rednet.send(0, "Current Fuel Level: " ..turtle.getFuelLevel())
rednet.send(0, turtle.getItemCount(1).. " CobbleStone Left")
rednet.send(0, turtle.getItemCount(2).. " Torches Left")
rednet.send(0, "Coal Left in Inventory: " ..turtle.getItemCount(16))
rednet.send(0, "Blocks Moved Until Torch: "..5-q)
rednet.send(0, "*************************************")


Edit 2: I suppos what I'm ultimatley after is the ability to send all 11 messages in the form of one message, but still include all the variables. Kind of like the "print[[" command or others like it. That way I can just use the "term.clear()" at the beginning of each loop. But I'm not sure if this is possible.

Edit 3: Upon further debugging I have found that the recursion loop that I used in a big "No No" and that is why I received the Java error. But I only started using that loop because the tried and true "for i=0,11" loop did not function correctly. Also it seems that the reason I needed the blank message at the begining of the other Rednet messages is because the terminal was having trouble receiving the first message everytime it rebooted. Is there a way around this?
Edited on 05 September 2012 - 05:47 AM
Grim Reaper #2
Posted 05 September 2012 - 07:55 AM
If you're trying to send a block of information as one string, I might suggest string concatenation.

Concatenation, from my understanding, is basically saying: I have this string and another string, but I want the as one, so I'll just sick em' together.
LUA handles string concatenation with a special operator:

..

Here is an example:


s = "This is "
s1 = "a test!"

sFinalString = s .. s1 -- Concatenate the strings into one with the .. operator.
print( sFinalString )
-- OR
print( s .. s1 )  -- You save a variable this way.

--[[OUTPUT:
This is a test!
This is a test!
--]]

Similarly, and more organized and pretty :D/>/> you could create a string that was packed with that information and send it off via rednet:
EDIT: For some reason, the HTML encoding screwed up the string I posted. Here's a pastebin copy.
Hepcat2299 #3
Posted 05 September 2012 - 08:28 AM
Hmm, I put your code into a test turtle next to the Monitor set up, and while using the same code on the receiving end that I used before but with the Recursion loop and the term.clear() removed.
Spoilerfunction Repeat()

for i=0,11
do
local event,p1,p2 = os.pullEvent()
if event == "rednet_message" then
print(p2)
end

end

end
The Turtle complies without any error but the receiving terminal does nothing. Only after I reboot the turtle a second time does the terminal(not monitor) return "term:9: attempt to index ? a (a nil value). Any ideas?

Also, thank you so much for your help! I never thoat about making the entire text into a variable and sending that, ingenious!

Edit: So I switched the loop to a while true do loop and same problem… I have to reboot the turtle twice before I get any kind of error on the receiving end. I just cant wrap my brain around this one.


Edit2: Silly me, some how I deleted the Peripheral Wrap. I added it back and it worked, but again only on the second boot from the turtle. I think somehow that might be the same problem I had earlier with having the add a "Blank" message before sending my really messages. Somehow the terminal is not picking up on the first signal sent to it after a reboot. Also the text does show up on the monitor but is there any way to add a break between each piece of info sent? So that each bit of text is on a different line and it all looks nice? thanks a lot! (back to debugging)

* heres the Entire code used on the receiving end:
Spoilermonitor = peripheral.wrap("top")


function startMonitor()
rednet.open("back")
rednet.receive()
term.redirect(monitor)
Repeat()
end




function Repeat()

while true
do
local event,p1,p2 = os.pullEvent()
if event == "rednet_message" then
print(p2)
end

end



end



startMonitor()

Edit 3: Haha, sorry for all the edits but I wanted to update anyone who is following this thread with what I have figured out so far and I didnt want to post a reply. I have removed the "rednet.receive()" from the receiving end and it works on first boot! thats great news and I really appreciate the help. Lastly all I need now is a way to position the text on the monitor in a way that looks pleasant =). And also to clear the screen before receiving the next wave a text.