20 posts
Posted 09 March 2013 - 09:41 AM
Hello anybody willing to help. I'm in need of assistance with tables.. What i want is to store all the values of one table into the values of another.. i thought it would be simpler but I'm having issues :/
I'll pastebin my code so you can run it. The error is in the calibrate() function and the error comes from the dist(pos) function where I try to perform math and get error __sub on number and nil where nil is offset."x"y or z in the dist(pos) function. I'm sure my for_looping skills are crap and this is why i cant get the var i want.
Sorry if I'm unclear or my terminology is off. Thank you in advance for any input :)/>
CODE:
http://pastebin.com/DQGW2UJAFunctions in question: getOffset() and calibrate()
Thanks again,
-Molinko a.k.a le Nub
1511 posts
Location
Pennsylvania
Posted 09 March 2013 - 10:01 AM
On what line is the code throwing an error?
20 posts
Posted 09 March 2013 - 10:11 AM
On what line is the code throwing an error?
" autoDoorv2 :91: attempt to perform arithmetic __sub on number and nil" so line 91
or if you run the program in game and run "autoDoor calibrate" and touch the monitor to call the calibrate funtion you get…
"autoDoorv2 :91: attempt to index ? (a nil value)"
1688 posts
Location
'MURICA
Posted 09 March 2013 - 10:20 AM
I think this is what you're trying to do:
local function getOffset()
local offset = {}
local target = prox.getTargets()
for k,v in pairs(target) do
offset[k] = v
end
return offset.X, offset.Y, offset.Z
end
Or just
local function getOffset()
local target = prox.getTargets()
return target.X, target.Y, target.Z
end
20 posts
Posted 09 March 2013 - 10:30 AM
I think this is what you're trying to do:
local function getOffset()
local offset = {}
local target = prox.getTargets()
for k,v in pairs(target) do
offset[k] = v
end
return offset.X, offset.Y, offset.Z
end
Or just
local function getOffset()
local target = prox.getTargets()
return target.X, target.Y, target.Z
end
I know this solution is close but the value im trying to grab is under v.Position in (target). Position holds {X =,Y=,Z=}
i need to know how to get into that table and assign to offset.X,Y,Z to v.Position.X,Y,Z
thank you :)/>
1688 posts
Location
'MURICA
Posted 09 March 2013 - 10:44 AM
This is my best guess:
local function getOffset()
local target = prox.getTargets()[1]
return target.X, target.Y, target.Z
end
As I would imagine, since you're looping through prox.getTarget
s(), that it's a table containing multiple targets, and that you want to get the first of them. No looping is really required here.
8543 posts
Posted 09 March 2013 - 11:07 AM
That's not the problem, nor how target tables work in OCS. The issue is the handling of the return values from getOffset in calibrate. You should probably be calling getOffset from dist and using the values directly. As it is now, you're storing the value of offset.X (offset here being the table local to getOffset), which is nil, in the global table offset. Hence the attempt to index nil. You don't seem to quite understand tables, which is the source of these problems. I can help further later this evening, if someone hasn't already.
20 posts
Posted 09 March 2013 - 11:14 AM
That's not the problem, nor how target tables work in OCS. The issue is the handling of the return values from getOffset in calibrate. You should probably be calling getOffset from dist and using the values directly. As it is now, you're storing the value of offset.X (offset here being the table local to getOffset), which is nil, in the global table offset. Hence the attempt to index nil. You don't seem to quite understand tables, which is the source of these problems. I can help further later this evening, if someone hasn't already.
I don't know how tables work.. I've tried and tried again.. but the best way seems to get my hands dirty and when im too stuck, ask for help.. thanks for the input
I know the calibrate function is has its own issues but for now my main error is with not returning offset from my function..
I tried this bit of code to to see if I could find what I wanted. Here's the code:
os.loadAPI("ocs/apis/sensor")
local prox = sensor.wrap("back") -- side of sensor
local offset = {}
local target = prox.getTargets()
for k,v in pairs(target) do
for i,val in pairs(v.Position) do
offset.i = v.Position.val
print(i.." : "val)
end
end
Output:
Y : <coordinate>
X : <coordinate>
Z : <coordinate>