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

Code help needed

Started by df123451, 20 November 2014 - 11:24 PM
df123451 #1
Posted 21 November 2014 - 12:24 AM
I am looking to make or get a code that will allow me to use OpenCCSensors(alpha 1.7) to show a list of block inventories within the radius of the sensor on a advanced monitor. I want to be able to click on the block name that has the inventory and see the inventory. totally new to lua, and trying to figure out how to do this, if anyone has help on how to do this, or if someone wants to be an amazing person and make it for me, I would love it. Running the direwolf20 1.7 pack( a few custom mods installed) on a server if that changes anything:)
Lyqyd #2
Posted 21 November 2014 - 12:53 AM
Well, the interface isn't clickable, but you can use the sensorview program if you're just trying to look at how the sensor gives the data. Just run ocs/programs/sensorview on the computer and use the arrow keys and page-up/page-down to look at the information. You'll have to disable NEI (open inventory, hit O) to use the page-up and -down keys.

As for creating your own program, you'd want to use the getTargets method to get a list of inventories in the vicinity, then use getTargetDetails(targetName) on whichever targets you're interested in. The documentation should explain the usage on those calls more fully.
Evil_Bengt #3
Posted 24 November 2014 - 07:27 AM
Well, the interface isn't clickable, but you can use the sensorview program if you're just trying to look at how the sensor gives the data. Just run ocs/programs/sensorview on the computer and use the arrow keys and page-up/page-down to look at the information. You'll have to disable NEI (open inventory, hit O) to use the page-up and -down keys.

As for creating your own program, you'd want to use the getTargets method to get a list of inventories in the vicinity, then use getTargetDetails(targetName) on whichever targets you're interested in. The documentation should explain the usage on those calls more fully.

Not clickable? Not sure if u actually meant the adv. comuptes interface or something else but u can make it clickable, np…
Bomb Bloke #4
Posted 24 November 2014 - 07:40 AM
He's referring to the sensorview script, which presumably in its current state, ignores mouse input.
CherryCoke #5
Posted 24 November 2014 - 11:16 AM
I have a question in response to Lyqyd's post…

I myself am trying to use the getTargetDetails, and I did look at the documentation, and it says I need to input a key.

Problem is, the documentation doesn't tell me what the key is, or where I find it. So currently I have code that looks something like:

(And I'm relatively new to lua…I have a background in Python but have only spent a day/two in lua)

os.loadAPI("ocs/apis/sensor")
mySensor = sensor.wrap("top")
print (mySensor.getSensorName())
--This line works
print (textutils.serialize(mySensor.getTargets()))
--But then I get here and I'm not quite sure what to input
print (textutils.serialize(mySensor.getTargetDetails(KEY)))
--Currently, I have the key as MFSU, but I also tried ic2.mfsu
--because it was an output from the getTargets() function.

What am I missing here?
Lyqyd #6
Posted 24 November 2014 - 04:36 PM
It's the key from the table getTargets returns. For the tile-entity based sensors, it's usually a string of coordinates. So, in the sensorview program, it's the list in the left panel. Each row is one key. An example might be:


local details = mySensor.getTargetDetails("-1,2,-4")
CherryCoke #7
Posted 24 November 2014 - 06:05 PM
Alright, thanks for your help so far, and I've managed to make a little more progress (I think). I've edited what I had with your advice, so now it looks somethig like


os.loadAPI("ocs/apis/sensor")
local mySensor = sensor.wrap("top")
print (mySensor.getSensorName()))
print (textutils.serialize(mySensor.getTargets()))
print("")
local details = mySensor.getTargetDetails("1, -1, 0")
print (details.Stored)

The last line I got from a previous thread/question about reading MFSU's, so I assumed nothing had change syntax-wise for retrieving how much EU is currently in the MFSU.

When I compile the code, however, I get the following error


powerCard


{
	 [ "1, -1, 0"] = {
	--# All that other MFSU stuff
}
test:10 attempt to index ? (nil value)

I've tried fiddling with it about a million different ways, with no success so far. One thing I read in another post of Lyqwd's was that OpenPeripherals doesn't play nice with OpenCC sensors…is that potentially the issue?

Thanks for all your help.
KingofGamesYami #8
Posted 24 November 2014 - 07:18 PM
How can you get an error on line 10 if your program is 7 lines long?
Lyqyd #9
Posted 24 November 2014 - 07:53 PM
No, if you're getting anything out of it, there's no conflict issue with OpenP. However, I don't believe there are any spaces in the key names, so that's probably the issue you're having.
CherryCoke #10
Posted 24 November 2014 - 07:54 PM
Ah that's actually an easy one to answer (You had me worried for a second)

The error occured on line 10 because in-game I included white spaces in the code to make it more readable (to me), so the line that's really flagging an error is line 7:


print (details.Stored)
KingofGamesYami #11
Posted 24 November 2014 - 08:33 PM
Well, in that case…

mySensor.getTargetDetails("1, -1, 0")
The above is returning nil for some reason.

Also, I'm pretty sure details.Stored would not be a valid entry, the capitalization of lua would call for the variable to be details.stored.
Lyqyd #12
Posted 24 November 2014 - 08:57 PM
As I mentioned above, the reason for it returning nil is likely because the spaces need to be removed.

details.Stored is correct.
CherryCoke #13
Posted 24 November 2014 - 11:39 PM
As I mentioned above, the reason for it returning nil is likely because the spaces need to be removed.

details.Stored is correct.

Two things:
a.) You are a genius
b.) I'm an idiot

For whatever reason, despite whitespace being so important in Python, I'd gotten so comfortable with not worrying about it in Lua that it went right over my head that a strings DO still care about whitespace.

Thank you for your patience and help :D/>

Edit: In case anybody was wondering, I included the quote with the solution in effort to help anybody else with a similar problem.
Edited on 24 November 2014 - 10:44 PM