— By TheOriginalBIT and remiX
Over the past few weeks there has been a large increase in the amount of people wanting to use ccSensors in Tekkit to do something. There is however no one that has a fantastic idea of how to fully use the peripheral due to a lack of documentation. So here is a compilation of everything we (TheOriginalBIT and remiX) have found out through our own experiments and helping people on “Ask a Pro”.
What is required:
- A ComputerCraft computer
- A Sensor Controller
- A Sensor
- A Sensor Card ( World, Inventory, BuildCraft, IndustrialCraft, Forestry, RedPower2, Equivalent Exchange, Advanced Power Systems, Thaumcraft2, Proximity )
- Transmitter card
Firstly, make sure you have what is needed in the above ‘What is required’. When you do, place the computer down anywhere with a Sensor Controller attached to it - like this. The Sensor Controller can be attached at any side to the computer.
Then place a sensor near what you want to be reading that is also within a reasonable distance to the SensorController you want it to be used with.
Once that is done, insert a Blank Transmitter Card into the SensorController at the bottom right and click ‘Encode’ and remove the Transmitter Card from the SensorController. You will notice the Transmitter Card will change colours, which is meant to happen.
Now open up the Sensor and place the Transmitter Card which you encoded from the SensorController into the bottom most left slot of the Sensor. You will notice there is another open spot next to the Transmitter Card slot - this slot is for the Module. There is also a spot where you can name the sensor to make it easier to identify in your code and/or on a UI.
In order for this all to work how you want it to, you will need the correct SensorModule. Search ‘Sensor’ and find the correct one, and place it into the slot next to the Transmitter Card in the Sensor. You should notice information come up on the sensor according to which SensorModule you placed inside the Sensor.
An example setup can be found here.
Do notice the colours of the SensorController and the Sensor are the same? That means they are connected.
Coding For The Sensors:
There is a very limited amount of documentation for ccSensors that can be found here.
Finding the Sensor Controller
The first piece of code that we can use is to find out the side of the computer that the sensor controller is on. There are two methods of doing this; Using the peripherals API or using a function that ccSensors gives us to use. The ccSensors way is much easier, but both are detailed below:
ccSensors method:
local controllerSide = sensors.getController()
peripheral API method:
local function findSensorController()
for _,side in pairs( rs.getSides() ) do
if peripheral.getType( side ) == “SensorController” then
return side
end
end
end
local controllerSide = findSensorController()
Using either of these methods to find the sensor controller is acceptable and comes down to personal choice ( although I don’t know why you wouldn’t use the one line, ccSensors, method over the longer, peripherals API method ).
Getting the Controller’s Sensors
Next we must find all the sensors that the Sensor Controller is able to access and use. To do this we use the following function.
local sensorsList = sensors.getSensors( controllerSide )
If you wish to see all the sensors that the controller can access you can use the following code.
for _, v in pairs( sensors.getSensors( controllerSide ) do
print( v )
end
When we use the getSensors function it will return us a table containing all the names of the sensors that the controller can access ( NOTE: It has been discovered that in some SMP worlds that the names do not work and all sensors will have the name “Sensor”)
Getting the Sensor’s Probes
We now have a list of the sensors, now we need to get the probes for the sensors. There are various different probes for each sensor.
sensors.getProbes( controllerSide, sensorName )
To see a list of all the probes available to the sensors you can use the following code
for _, sensor in pairs( sensors.getSensors( controllerSide ) ) do
print( “Probes for the sensor: “..sensor )
for _, probes in pairs( sensors.getProbes( controllerSide, sensor ) ) do
print( probes )
end
end
If there is a sensor in particular that you can extract it directly from the return of the getProbes function like so
local probes = sensors.getProbes( controllerSide, sensor )[3]
Getting the Probe’s Targets
Once we have the probe or probes we are after we must then get the targets for the sensor. These targets are the actual items or blocks ( or any data ) that is in the game that we are trying to display or gather information for. This information varies for each Sensor type. The data that you have access from the target can be found out by the following code
sensors.getSensorReadingAsDict( controllerSide, sensor, target, probe )
Getting Information About the Target
To get the information or data about a target we use the following code
sensors.getSensorReadingAsDict( controllerSide, sensor, target, probe )
This will return us a table containing all the data about a given target. We can then access this data and use it in the functioning of our program. An example to display the amount of energy, as power level and percentage, inside of an IC2 Power Storage device is as follows:
print( “Power level: “..data.energy..”/”..data.maxStorage )
print( “Percentage: “..( data.energy / data.maxStorage * 100 )..”%” )
As there are so many sensor types there is no way we are going to list all the data for each target. So that being said the following example will print out all the data available for all the available targets, in all the available sensors ( this is recommended for use when your trying to find that one particular piece of data )
for _,sensor in pairs( sensors.getSensors( controllerSide ) ) do
for _,probe in pairs( sensors.getProbes( controllerSide, sensor ) ) do
for _, target in pairs( sensors.getAvailableTargetsforProbe( controllerSide, sensor, probe ) ) do
for k, data in pairs( sensors.getSensorReadingAsDict( controllerSide, sensor, target, probe ) ) do
print( k.." : "..tostring( data ) )
end
end
end
end
Summary
Using the steps shown above a great deal can be learnt about ccSensors and in fact it is how I ( TheOriginalBIT ) actually first figured out how to use ccSensors and still use when helping people in “Ask a Pro” with their ccSensors issues. Actually this method is quite a good method to use to figure out how to use ANY peripheral, just go through step-by-step until you get to where you need
Making it Easier
If all of this so far still isn’t making much sense, or you’re just not sure what sensor to use, remiX has made a small script to make sensor selection MUCH easier, this can be found here, an image of it running here.
Further Examples:
IC2 Power Storage Levels — By TheOriginalBIT: http://pastebin.com/asSLPyqZ
IC2 Power Levels — By remiX: http://pastebin.com/1UqupYub
Proximity — By remiX: http://pastebin.com/jzwn2xPQ
Inventory Module — By remiX: http://pastebin.com/8QZzwre9
Player Detector — By TheOriginalBIT: http://pastebin.com/F9DwAFcn
Still Don’t Get It?
Feel free to leave a comment below asking us a question and we will try to answer it as best as we can :)/>