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

OpenCCSensors Help

Started by hego555, 09 March 2013 - 03:22 PM
hego555 #1
Posted 09 March 2013 - 04:22 PM

os.loadAPI("ocs/apis/sensor")
  prox = sensor.wrap("left")
  
  local target = prox.getTargets()
  
     local targets = (textutils.serialize(target))
  -- print (targets)
  -- print (targets[2].Name)
  -- print (targets["Filter"])
  
for k, v in pairs(target) do
print (v.RawName.Slots)


  end

This is my code, my code is evil, the output is blank…

I have absolutely no idea what I am doing, this mod has horrible documentation, or it may be I cant find it ;(
theoriginalbit #2
Posted 09 March 2013 - 04:24 PM
what does this output?

for k, v in pairs(prox) do
print (tostring(k)..' : '..tostring(v))
end
3LiD #3
Posted 09 March 2013 - 04:29 PM
I pretty sure there isnt a prox api…. maybe change

local target = prox.getTargets()

to:

local target = sensor.getTargets()

since you loaded the sensor api…
hego555 #4
Posted 09 March 2013 - 04:40 PM
0,0,0 : table: 7cf2a8fc
-2, 0, -2 : table: 2dd1a197
-2, 0, 1 : table: 2b3638ed
-2, 0, 2 : table: 62be62c7
Lyqyd #5
Posted 09 March 2013 - 05:07 PM
What are you trying to do? It looks like you need to use the getTargetDetails function to grab the data you're actually looking for. What's the end-goal here?
hego555 #6
Posted 09 March 2013 - 05:08 PM
Spoilerlocal function waitForResponse( _id )
while true do
local event = {os.pullEvent()}
if event[2] == _id then
if event[1] == "ocs_success" then
return event[3]
elseif event[1] == "ocs_error" then
error(event[3],2)
end
end
end
end

function wrap(side)
local wrappedTable = {}
if peripheral.getType(side) == "sensor" then
local periph = peripheral.wrap(side)
for k,v in pairs(periph) do
if type(k) == "string" and type(v) == "function" then
wrappedTable[k] = function(…)
local id = periph[k](…)
if id == -1 then
return false
end
return waitForResponse(id)
end
end
end
return wrappedTable
else
return nil, "not a sensor"
end
end

function call(side, …)
if peripheral.getType(side) == "sensor" then
local id = peripheral.call(side, …)
if id == -1 then
return false
end
return waitForResponse(id)
else
return nil, "not a sensor"
end
end
Lyqyd #7
Posted 09 March 2013 - 05:10 PM
The sensor API is not the problem.
hego555 #8
Posted 09 March 2013 - 05:12 PM
The sensor API is not the problem.

I figured, but I did as asked.

I can not figure out how to get the info I want.
3LiD #9
Posted 09 March 2013 - 05:22 PM
[right].[/right]
hego555 #10
Posted 09 March 2013 - 05:25 PM
Do you have an api called prox?

Dude what are you saying?

You are just bringing in random things… prox is the object/variable!!!
Lyqyd #11
Posted 09 March 2013 - 05:49 PM
You need to be using getTargetDetails to fetch the information you're looking for. Have you tried using the sensorview program (ocs/programs/sensorview) to examine the detailed information table for the target(s) you want information on?

Perhaps this modification of your script will help show you what I mean:


os.loadAPI("ocs/apis/sensor")
  prox = sensor.wrap("left")
 
  local target = prox.getTargets()
 
for tarName, _ in pairs(target) do
    --fetch the detailed information table for the current target.
    local details = prox.getTargetDetails(tarName)
    for k, v in pairs(details) do
        print(tostring(k)..": "..tostring(v))
    end
end

Please ignore 3LiD. He obviously has no idea what he's talking about.
hego555 #12
Posted 09 March 2013 - 05:58 PM
You need to be using getTargetDetails to fetch the information you're looking for. Have you tried using the sensorview program (ocs/programs/sensorview) to examine the detailed information table for the target(s) you want information on?

Perhaps this modification of your script will help show you what I mean:


os.loadAPI("ocs/apis/sensor")
  prox = sensor.wrap("left")

  local target = prox.getTargets()

for tarName, _ in pairs(target) do
    --fetch the detailed information table for the current target.
    local details = prox.getTargetDetails(tarName)
    for k, v in pairs(details) do
        print(tostring(k)..": "..tostring(v))
    end
end

Please ignore 3LiD. He obviously has no idea what he's talking about.

Dear Sir, I love you

Thank you

Very Much

I have no idea what you did, but now I will dissect your code… and learn!

I LOVE YOU!
hego555 #13
Posted 09 March 2013 - 06:00 PM
You need to be using getTargetDetails to fetch the information you're looking for. Have you tried using the sensorview program (ocs/programs/sensorview) to examine the detailed information table for the target(s) you want information on?

Perhaps this modification of your script will help show you what I mean:


os.loadAPI("ocs/apis/sensor")
  prox = sensor.wrap("left")

  local target = prox.getTargets()

for tarName, _ in pairs(target) do
    --fetch the detailed information table for the current target.
    local details = prox.getTargetDetails(tarName)
    for k, v in pairs(details) do
        print(tostring(k)..": "..tostring(v))
    end
end

Please ignore 3LiD. He obviously has no idea what he's talking about.

Dear Sir, I love you

Thank you

Very Much

I have no idea what you did, but now I will dissect your code… and learn!

I LOVE YOU!

Dear Sir, I have one question

Can I skip the for loop if I know which item I already want?

For example

RawName: icbm.machine.0
Lyqyd #14
Posted 09 March 2013 - 06:05 PM
Yes, if you know exactly what you're looking for, don't worry about the loops and just index exactly what you need. For instance, if you were trying to get the number of items in slot 1 of an inventory at 1,0,1 relative to an inventory sensor, all you need is:


os.loadAPI("ocs/apis/sensor")
invSense = sensor.wrap("left")
details = invSense.getTargetDetails("1,0,1")
print(details[1].Size)

Obviously, the exact nature of what you use depends on what the details table has in it. I don't know exactly what you're trying to get your script to do, so without further explanation, I can't provide better example code.
hego555 #15
Posted 09 March 2013 - 06:20 PM
Yes, if you know exactly what you're looking for, don't worry about the loops and just index exactly what you need. For instance, if you were trying to get the number of items in slot 1 of an inventory at 1,0,1 relative to an inventory sensor, all you need is:


os.loadAPI("ocs/apis/sensor")
invSense = sensor.wrap("left")
details = invSense.getTargetDetails("1,0,1")
print(details[1].Size)

Obviously, the exact nature of what you use depends on what the details table has in it. I don't know exactly what you're trying to get your script to do, so without further explanation, I can't provide better example code.

Good sir,

I am trying to check if a ICBM missile launcher has a missile in it!

The code you gave me gave me a

Sensor: 4: attempt to index ? (a nil value)

I copied and pasted your exact code :)/>

Thank You

(if this works, I will name my second child after you)
Lyqyd #16
Posted 09 March 2013 - 06:51 PM
Yes, that code was just an example code. Where it has 1,0,1 you should try swapping out for the coordinates you told us about earlier:

-2, 0, -2
-2, 0, 1
-2, 0, 2

Unless you know which one is the launcher already. The sensorview program may help you find the correct target (the coordinates) and the correct field to look at. :)/>
hego555 #17
Posted 09 March 2013 - 07:41 PM
Yes, that code was just an example code. Where it has 1,0,1 you should try swapping out for the coordinates you told us about earlier:

-2, 0, -2
-2, 0, 1
-2, 0, 2

Unless you know which one is the launcher already. The sensorview program may help you find the correct target (the coordinates) and the correct field to look at. :)/>/>

Good sir, I have already completed that before I posted my concern.

Any other suggestion?
hego555 #18
Posted 09 March 2013 - 08:05 PM
I got it semi-working

But I cant the piece I want

Spoiler

os.loadAPI("ocs/apis/sensor")
invSense = sensor.wrap("right")
details = invSense.getTargetDetails("-2,0,0")
print(textutils.serialize(details.Slots[Name]))

if I remove the [Name] it will output

{[1]={=1,["MaxSstack"]=1,["DamageValue"]=5,["RawName"]="icbm.missle.repulsive,["Name"]="Repulsive Missle",}.}
hego555 #19
Posted 09 March 2013 - 08:13 PM
nevermind, I have gotten it!

print(textutils.serialize(details.Slots[1].Name))

Thanks you Lyqyd, my 2nd child will be named in your honor!