Posted 30 April 2012 - 12:26 PM
Hey all, looking for a little pointer in how to pass a table across Rednet, and have the recipient be able to parse that table, and read specific items from it as variables…
Been messing around in CC for a few days now, and managed to hack together some scripts to control my turtle remotely, mainly by modifying other scripts i've found on here*.
Thus far, I have the Aware API installed, and a script to execute turtle commands running on the turtle, which works great. The command and control script runs on the computer terminal, and so far can succesfully pass instructions to the client.
So, to my question - I'm attempting to send a message from the turtle to the computer at 6 points in a sequence, and the computer will print those 6 messages before breaking back to it's previous menu. At the moment, whenever the aware API moves, it broadcasts it's location and other data, which is being recieved by the terminal, and kicking off the 6 recieve events with the wrong data, so I'm attempting to filter the data as it arrives, and only react to the data which is of the appropriate class, as defined by the first variable in the table i'm trying to send below - class "Z" should trigger the 6 print events, any other data should not.
Currently the result is that the terminal sits and waits for 6 events which never come, and the turtle crashes with the error "Rednet:330 Attempt to index ? (a nil value)"
I can alter the format of the send command so that it appears to send, but I can't seem to make the client read the resultant message.. i've looked at the way Biolisten seems to do it, and can't quite figure out the use of the textutils.serialise function it seems to call…
So it's 2 parts really, first, how to safely send a table containing 2 or 3 elements within a rednet.send() command, and then how to read that table from a variable again..
I get the feeling i'm missing something basic, but i've been at it a while now.. i'm fairly new to LUA, and it's been a few years since I did any real code work (outside of html / css type stuff), so i'm a little rusty.
Snippets of relevent code below -
terminal code - Terminal sends message to turtle to say 'execute "repeat" command', and waits for 6 responses -
*Credits:
Been messing around in CC for a few days now, and managed to hack together some scripts to control my turtle remotely, mainly by modifying other scripts i've found on here*.
Thus far, I have the Aware API installed, and a script to execute turtle commands running on the turtle, which works great. The command and control script runs on the computer terminal, and so far can succesfully pass instructions to the client.
So, to my question - I'm attempting to send a message from the turtle to the computer at 6 points in a sequence, and the computer will print those 6 messages before breaking back to it's previous menu. At the moment, whenever the aware API moves, it broadcasts it's location and other data, which is being recieved by the terminal, and kicking off the 6 recieve events with the wrong data, so I'm attempting to filter the data as it arrives, and only react to the data which is of the appropriate class, as defined by the first variable in the table i'm trying to send below - class "Z" should trigger the 6 print events, any other data should not.
rednet.send(ID, {"Z","some message", anothervariable})
Currently the result is that the terminal sits and waits for 6 events which never come, and the turtle crashes with the error "Rednet:330 Attempt to index ? (a nil value)"
I can alter the format of the send command so that it appears to send, but I can't seem to make the client read the resultant message.. i've looked at the way Biolisten seems to do it, and can't quite figure out the use of the textutils.serialise function it seems to call…
So it's 2 parts really, first, how to safely send a table containing 2 or 3 elements within a rednet.send() command, and then how to read that table from a variable again..
I get the feeling i'm missing something basic, but i've been at it a while now.. i'm fairly new to LUA, and it's been a few years since I did any real code work (outside of html / css type stuff), so i'm a little rusty.
Snippets of relevent code below -
Spoiler
Turtle code:
elseif message == "repeat" then -- turtle is listening for a message, "repeat" is sent by terminal
i = 1
repeat
rednet.send(ID, {"Z","Going to site", i}) -- send message with progress update, Z denotes category, then message, then the iteration it is currently doing
aware.goto(site) -- go to site (defined elsewhere, works fine when i take out all the messaging stuff)
shell.run("Excavate2 ", "9") -- dig dig dig
rednet.send(ID, {"Z","Going home", i})
aware.gohome()
aware.turnleft()
aware.dump()
aware.turnright()
site[3] = site[3]-5
i = i + 1
until i > 3
terminal code - Terminal sends message to turtle to say 'execute "repeat" command', and waits for 6 responses -
info = "repeat" -- turtle is expecting this input. works ok.
rednet.send(ID, info)
i = 1
repeat
local ID, ackmsg = rednet.receive() -- store return message as "ackmsg"
category = ackmsg[1] -- take 1st element of table in "ackmsg" and store as "category"
if category == "Z" then -- if right category, print it, if wrong, ignore it
print(ackmsg)
i = i + 1
end
until i > 6 -- do it 6 times
print("Done")
sleep(7)
*Credits: