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

How do I read variable values from a table? (GPS home-recording program)

Started by Chaz, 19 January 2016 - 06:27 AM
Chaz #1
Posted 19 January 2016 - 07:27 AM
Hi there! I'm tinkering about with CC 1.78 and learning a bit more about how GPS systems work - Mostly I'm tinkering about in Creative right now but I made a GPS cluster tower using Ender Modems, and everything's working fine.

I'm trying to write a pair of programs - One records the player's current position and writes those coordinates to a "Home Position" file, the other should take the values from that file, subtract it from the player's current position, run a square root on calculate the sum of the three and return an absolute value as to how far away they are from their designated "Home" point.

Everything worked fine when I had static values for X, Y and Z (i.e. I just had them declared in the file rather than being recorded), but I'm not quite understanding how to work with tables in terms of reading what I've recorded.

Here's the program that sets the player's home:


print("Saving current location as home.")
local x, y, z=gps.locate(5)

local coords={
x,
y,
z,
}

local f=fs.open("/data/homepos", "w")
f.write(textutils.serialise(coords))
f.close()

Sure enough, I get a file in my system called homepos, that contains the GPS coordinates, so that part works.
Here's the program that runs the calculations for the user's current distance from home:


local f=fs.open("/data/homepos", "r")
local x, y, z=textutils.unserialise(f.readAll())
f.close()

local curx, cury, curz=gps.locate(5)

local distx=curx-x
local disty=cury-y
local distz=curz-z

local distsum=distx+disty+distz

local absdist=math.abs(distsum)

print("I am "..math.floor(absdist+0.5).."m away from home!")

I'm getting an error at line 7: "Attempt to perform arithmetic __sub on number and table", which I can only conclude means I don't know how to convert table values into usable variables. As always, help's appreciated!

EDIT: Man I'm bad at math. I don't need to square-root anything apparently. The sum of the three distances works just fine as far as measuring distance goes. Regardless, I'm still trying to figure out how tables work, haha.
Edited on 19 January 2016 - 06:34 AM
Lupus590 #2
Posted 19 January 2016 - 08:45 AM
you already have been reading from tables. fs is a table, f is also a table and so is math and gps. you just didn't know it

using your coords example, cords.x will allow me to set or get the x coordinate

edit: and this is why you test your code
Edited on 19 January 2016 - 12:55 PM
Dragon53535 #3
Posted 19 January 2016 - 01:49 PM
using your coords example, cords.x will allow me to set or get the x coordinate
No, no it won't. cords[1] will set or get the x coordinate.


For your program here, you either want named values, which is probably easier to read, or numbered values which are easier for the computer to use.


local f=fs.open("/data/homepos", "r") --#EXAMPLE WITH CURRENT SAVING
local tbl=textutils.unserialise(f.readAll()) --# Variable is now tbl, this is numbered values
f.close()
local curx, cury, curz=gps.locate(5)
local distx=curx-tbl[1] --#tbl[1] is your x coordinate
local disty=cury-tbl[2] --#y
local distz=curz-tbl[3] --#z
local distsum=distx+disty+distz
local absdist=math.abs(distsum)
print("I am "..math.floor(absdist+0.5).."m away from home!")

However, if you want to do it so that you use tbl.x to reference your x coordinate, you need to first edit your saving to accommodate that.


print("Saving current location as home.")
local x, y, z=gps.locate(5)
local coords={
["x"] = x,
["y"] = y,
["z"] = z,
}
local f=fs.open("/data/homepos", "w")
f.write(textutils.serialise(coords))
f.close()
Now, when you save your coordinates table, it will allow you to change the tbl[1], tbl[2], and tbl[3] into tbl.x, tbl.y, and tbl.z.
Edited on 19 January 2016 - 12:49 PM
Chaz #4
Posted 19 January 2016 - 05:49 PM
Aha, that's solved it. Thank you for your help, Dragon :D/> I understand how to use tables a bit better now!