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

[SOLVED]problem with openPeripheral sensor, function returning a list of functions instead of desired info

Started by CreeperGoBoom, 18 December 2018 - 05:29 PM
CreeperGoBoom #1
Posted 18 December 2018 - 06:29 PM
This code

local p = peripheral.wrap("bottom") -- Presuming the sensor is on the left
for _, id in ipairs(p.getEntityIds("mob")) do -- Or just p.getMobIds() -- Can also use "minecart" and "item"
  print("Entity: " .. id)
  for k, v in pairs(p.getEntityData(id,"mob")) do
	 print(k .. ": " .. tostring(v))
  end
end

returns this:


o.0 (what is all this…)
Using a friendly vanilla non custom creeper nearby. I want to see the name "Creeper" or something similar returned.

In theory getEntityData(id,"mob") should return custom name etc. Why isnt it? maybe one of you guys knows whats going on.

Any help would be appreciated :)/>.
Edited on 19 December 2018 - 05:13 PM
Lupus590 #2
Posted 18 December 2018 - 11:07 PM
Is this useful to you? https://ftbwiki.org/Sensor
CreeperGoBoom #3
Posted 19 December 2018 - 10:08 AM
Not really, thats whats giving me the output.

On that note, in general what do you do if a function returns a list of functions instead of a table of data?

If I change the code to this:


local p = peripheral.wrap("bottom") -- Presuming the sensor is on the left
for _, id in ipairs(p.getMobIds()) do -- Or just p.getMobIds() -- Can also use "minecart" and "item"
  for i,v in pairs(p.getEntityData(id, "mob")) do
    print(i,v)
   end
 
end

i still get a list of functions, any ideas?
Lupus590 #4
Posted 19 December 2018 - 11:38 AM
Your screenshot didn't work for me so I can't see it (you may want to upload it to imgur or simular and link to it here)

does that docs folder that we told you about in a previous ask a pro help at all?
CreeperGoBoom #5
Posted 19 December 2018 - 12:19 PM
heres the output from imgur:


EDit: cant figure out ho to use doc, i use opdoc ( Can't remember ho uploaded it) or listmethods nowadays

*sigh* if only I could listmethods my new OP wireless keyboard #Frustrated emoji face#

EDIT2: In case that didn't work here's the url https://imgur.com/a/ggE8sy7

EDIT3: Nailed it! After some research, I found that i needed to save the results of getMobData to a table and then call the function basic() inside the table to get the info I needed.
SpoilerYay, Custom boss fights here I come :D/>

Just one problem though…

I noticed something which in coding terms might look like this


table[function[table]]

--which more accurately would be

sensor.getEntityData["basic"["pos"]]


Here is my current working code:

local results = {}

local p = peripheral.wrap("bottom")
for _, id in ipairs(p.getEntityIds("mob")) do -- Or just p.getMobIds() -- Can also use "minecart" and "item
	  for i,v in pairs(p.getEntityData(id, "mob")) do
	 results[i] = v
   end
	for a,c in pairs(results["basic"]()) do
	print(a.." : ",c)
	end
end

Current Output as it stands with my nice patient friend who's name is JohnnyDaCreeper (dont you just love how they calmly sit there in creative? :D/>)

UUID : <Whatever>
position : table : <some numbers> --how would i access this table?
name : JohnnyDaCreeper --YES! Nailed it!
id : <some numbers>

Any help would be good :)/>
[tutorial]So…How do i read a table within a table…

the answer is, say you have a table called results, simply update the table once you are finished with it.
this is what i did to also return my friends position "JohnnyDaCreeper" relative to the sensor

Sky is the limit :D/>


local results = {}

local p = peripheral.wrap("bottom") -- Presuming the sensor is on the left
for _, id in ipairs(p.getEntityIds("mob")) do -- Or just p.getMobIds() -- Can also use "minecart" and "item
   for i,v in pairs(p.getEntityData(id, "mob")) do
	 results[i] = v
   end
   for a,c in pairs(results["basic"]()) do
	 print(a," : ",c)
	 results[a] = c -- This updates my results table from the results of getEntityData to whatever was contained in the "position" table
   end
   if results["position"] then
	 for n,p in pairs(results["position"]) do
	   print(n," : ",p)
	 end
   end
end

BTW: [SOLVED]
Edited on 19 December 2018 - 05:19 PM