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

Properly relay GPS x,y,z coords over modem?

Started by Jtpetch, 27 August 2016 - 06:47 AM
Jtpetch #1
Posted 27 August 2016 - 08:47 AM
So I'm trying to, ultimately, create a program to have a turtle follow the player (or at the very least, respond to a "Come to me" command) via X,Y,Z coordinates.
(That's gonna take a while, but I'm working on the basics right now)
I'm having issues, however, transmitting x,y,z coordinates over modems.

I have a script on my PDA (works the same on a stationary PC with modem, though) transmitting my GPS coordinates every 2 seconds:

while true do
location=vector.new(gps.locate())
peripheral.call("back", "transmit", 3, 1, tostring(location))
print(location)
os.sleep(2)
end
It's a vector right now, but I've tried everything I could think of to get it to send usable information. From what I see, though, only strings can be sent/received, hence the tostring.

This is the code on the receiving end (Currently a PC with ender modem):


while true do
local modem = peripheral.wrap("right")
modem.open(3)  -- Open channel 3 so that we can listen on it
local event, modemSide, senderChannel,
  replyChannel, message, senderDistance = os.pullEvent("modem_message")
  location=tonumber(message)
  print(location)
end

It is currently trying to put the message to numbers (which I suppose makes sense wouldn't work), but again, I've tried everything I could think of to translate it into usable x, y, and z individual variables.
Basically, what (I think) I need to do is get my X,Y,Z coords, send them as a string, and translate them on the other end into usable, seperate x,y,z coordinates.
As a vector, it prints nil, if I translate them into a string, it prints the coordinates with , between, but won't translate to numbers.

Is there a feasible way to do this that I'm just missing?
Edited on 27 August 2016 - 06:48 AM
KingofGamesYami #2
Posted 27 August 2016 - 01:00 PM
You could just send a table.

CC Wiki said:
The message may be any type of data excluding functions and threads, which will be received as nil. Prior to about CC 1.53, tables were also received as nil.

source
Jtpetch #3
Posted 27 August 2016 - 09:59 PM
You could just send a table.

CC Wiki said:
The message may be any type of data excluding functions and threads, which will be received as nil. Prior to about CC 1.53, tables were also received as nil.

source

Ah, yes, that works! I thought I'd tried that, but I wasn't reading it correctly on the receiver. Thanks Yami!
This is working now:
Sender:

while true do
local x, y, z=gps.locate()
local location={x,y,z,}
peripheral.call("back", "transmit", 3, 1, location)
print(tostring(x..", "..y..", "..z))
os.sleep(2)
end

Receiver:

while true do
local modem = peripheral.wrap("right")
modem.open(3)  -- Open channel 3 so that we can listen on it
local event, modemSide, senderChannel,
  replyChannel, message, senderDistance = os.pullEvent("modem_message")
  print(message[1])
  print(message[2])
  print(message[3])
end

Now whether or not this is the most efficient method is another question…