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

[Lua]Vectors?

Started by YoavD, 24 March 2012 - 08:55 PM
YoavD #1
Posted 24 March 2012 - 09:55 PM
Hey there,
I've just found out about the GPS feature which is very cool, but I can only find mathematical/complex commands for vectors (such as multiplying). I'm looking to seperate only the Y value, but I can't figure out how to do that (y = VectorA.y? y = VectorA[y]? ). Help?

(p.s. If im at the subject, can you add a page with all Computercrafts basic commads (Loops, If, arrays etc.) for us programmers who dont know Lua so we can learn faster?)
Advert #2
Posted 24 March 2012 - 10:20 PM
There is plenty of information to get you started in the tutorial section, and in links in the stickies.

If you'd have tried this, then you would find that


local y = vec.y

will give you the y value.
YoavD #3
Posted 25 March 2012 - 01:05 PM
My test program is:


rednet.open("right")
vec = vector.new(0, 0, 0)
vec = gps.locate(5, true)
y = vec.y
print("H")
print(y)

But it returns:


Finding position...
10.440307 meters from 821,5,881
12 meters from 831,5,872
9.219544 meters from 837,5,891
Position is 831,5,884
vector:7: attempt to index ? (a number value)
Advert #4
Posted 25 March 2012 - 01:07 PM
My test program is:


rednet.open("right")
vec = vector.new(0, 0, 0)
vec = gps.locate(5, true)
y = vec.y
print("H")
print(y)

But it returns:


Finding position...
10.440307 meters from 821,5,881
12 meters from 831,5,872
9.219544 meters from 837,5,891
Position is 831,5,884
vector:7: attempt to index ? (a number value)

You are overwriting vec when you do vec = gps.locate(5, true), with a number.
YoavD #5
Posted 25 March 2012 - 01:10 PM
Doesn't gps.locate return a vector?
Espen #6
Posted 25 March 2012 - 01:24 PM
Doesn't gps.locate return a vector?
No, it returns 3 number values.

You have to use:
local x, y, z = gps.locate(5, true)

The way you used it just grabbed the first returned value of locate(), the x coordinate.
And since x is just a number, so will your vec be. That's why there isn't any vec.y
YoavD #7
Posted 25 March 2012 - 01:42 PM
Ok, now it works thank you very much