Posted 29 August 2012 - 12:40 AM
For those of you that don't want to sit through a 45 minute video (although funshinex's video is DEFINITELY worth watching to get a start), here is the simplest way to work with sensors. I actually use this "framework" for all my sensors in every map I've used sensors in.
here goes:
Also, you can edit your lua programs without using the in-game editor. Just figure out which computer you are on (run id) then go look in your %appdata%/roaming/.minecraft/saves/WORLDNAME/computer/computer's ID.
If you want syntax highlighting get gVim. Makes it MUCH easier to code in color.
Hope this has been helpful.
-W
here goes:
-- First we get where the controller is on the computer
-- and all the sensors on that controller (and save the total number of sensors)
ctrl = sensors.getController()
sensorList = sensors.getSensors(ctrl)
sensorTotal = table.maxn(sensorList)
sensorTargetMap = {} -- empty table declaration.
sensorProbe = "" -- This is set to whatever type you are looking for.
-- (IE): RedstoneEngine, LiquidTank, etc.
probeType ="" -- This is set to whatever information you are trying
-- to pull from the sensor.
-- Each probeType is dependent on whatever sensorProbe
-- is set to. For example, with RedstoneEngine you can
-- find out if it is running or not by setting probeType
-- to "engine.energy" . If this value is less than 1 then
-- the engine is not running.
-- Now select the sensors in the order WE want to query them.
-- These names are whatever you called your sensors.
sensorMap = { "SensorA", "SensorB", "SensorC", "SensorD" }
-- Here comes the real work. This is several nested 'for' loops.
-- Basically, for every sensor, get every target, and from every target
-- find the sensors we want and get it's information.
-- NOTE: This for loop evaluates every sensor ONE AT A TIME.
for a = 1, table.maxn(sensorList) do
curSensor = sensorMap[a]
sensorTarg = sensors.getAvailableTargetsforProbe(ctrl,
curSensor, sensorProbe)
-- Now we get targets for the current sensor and store them in our map.
for b = 1, table.maxn(sensorTarg) do
table.insert(sensorTargetMap, sensorTarg[b])
end
-- and finally we get the sensor data we're looking for.
for c = 1, table.maxn(sensorTargetMap) do
sensorResult = sensors.getSensorReadingAsTable(ctr, curSensor,
sensorTargetMap[c], sensorProbe)
for d = 1, table.maxn(sensorResult) do
if tostring(sensorResult[d]) == probeType then
probeState = tonumber(sensorResult[d+1])
-- do whatever you want with the information.
end
end
end
sensorTargetMap = {} -- empty the target table so we can move to the
-- next sensor.
end
Also, you can edit your lua programs without using the in-game editor. Just figure out which computer you are on (run id) then go look in your %appdata%/roaming/.minecraft/saves/WORLDNAME/computer/computer's ID.
If you want syntax highlighting get gVim. Makes it MUCH easier to code in color.
Hope this has been helpful.
-W