Posted 30 November 2015 - 03:55 PM
Hello fellow forum members!
Recently I started to do some Lua again and I had to write boilerplate code over and over again to track the redstone inputs. Out of annoyance I wrote a simple API that keeps track of the states of all sides, but it needed more. I decided to implement callbacks for users to implement to not even have to check which side changed. This is how the API looks, it has three functions:
If you are still interested, feel free to download it on pastebin or get it directly on your CC PC:
If you want to use this code in a program of yours, a credit to me would be nice but is not really required since it is a very small utility.
Enjoy!
Recently I started to do some Lua again and I had to write boilerplate code over and over again to track the redstone inputs. Out of annoyance I wrote a simple API that keeps track of the states of all sides, but it needed more. I decided to implement callbacks for users to implement to not even have to check which side changed. This is how the API looks, it has three functions:
- redtracker.update() No arguments need to be supplied to this function. This function checks if any sides changed and if a side changed, it will call the callbacks which are registered. Note that the functions are called directly if input has changed, so the function which handles it should not take a long time to proces; it has not been designed to do so.
- redtracker.addCallback( mode, function [, side]) This function does what you expect it to do, it adds a callback function to the specific side and mode. If the side is not given, it will register to all sides, this is useful if you need to look at all sides and don't care for the specific side. The mode is at this point 'regular', 'analog' or 'bundled.' This is done so the function and underlying code is dynamic. That means when another redstone.get*Input gets added, it is an easy thing to add this. When a callback function is called, it will be given the side and state of redstone, so let's imagine this code:
- redtracker.addRegularCallback( func [,side]) This function is a synonym for redtracker.addCallback("regular", func, side)
- redtracker.addAnalogCallback( func [,side]) This function is a synonym for redtracker.addCallback("analog", func, side)
- redtracker.addBundledCallback( func [,side]) This function is a synonym for redtracker.addCallback("bundled", func, side)
- redtracker.waitRedstoneUpdate() This function simply waits for a redstone event to occur and then calls the update method. This is usefull if you don't want to call os.pullEvent yourself for some odd reason :P/>
local function callback(side, active)
print("The redstone on the " .. side .. " is " .. (active and "" or "not ") .. "active!")
end
os.loadAPI("redtracker")
redtracker.addRegularCallback(callback)
Let's imagine now the redstone on the top turns on; the redstone on the back turns on; the redstone on the top turns off. The output would be:
The redstone on the top is active!
The redstone on the back is active!
The redstone on the top is not active!
The active parameter changes with type of redstone, for the bundled register the active parameter will be a number representing its colors; for the analog parameter it will be a number ranging from 0-15 representing the analog signal as specified on the wiki.
os.loadAPI("redtracker")
-- Add the callbacks to the sides
while true do
redtracker.waitRedstoneUpdate()
end
When your application also needs to capture other events, simply do:
os.loadAPI("redtracker")
-- Add the callbacks to the sides
while true do
local event, p1, p2, p3 = os.pullEvent()
if event == "redstone" then
redtracker.update() -- You can always call the update function, but it only has literal function when redstone changes
elseif event == "some other event " then
-- blabla
end
end
If you are still interested, feel free to download it on pastebin or get it directly on your CC PC:
pastebin get ANDVZV8V redtracker
If you want to use this code in a program of yours, a credit to me would be nice but is not really required since it is a very small utility.
Enjoy!
Edited on 30 November 2015 - 07:30 PM