Jump to content




[1.31] Redstone-Event Extension

api

8 replies to this topic

#1 Espen

    Curious Explorer

  • Members
  • 708 posts

Posted 21 March 2012 - 02:03 PM

Description
It extends the "redstone" event that os.pullEvent() returns with the sides that have changed.

Posted Image


The API keeps track of the redstone input states on all 6 sides of the computer.
If any of the redstone input states change, the event "redstone" will return a string denoting the type of redstone-input, as well as a table of all the sides that have changed.
If vanilla redstone was changed, type will be "vanilla".
If bundled cable was changed, type will be "bundled_cable".
http://en.wikipedia.org/wiki/WTFPL

Exposed Function
pullEvent( event, side )
  • event is an optional event filter. ( Just like the native event filter, so nothing new. )
  • side is a table of strings and acts as a side filter, i.e. you can choose to listen to only specific sides and ignore all others.
  • Returns: event, type, changes
  • event == "redstone"

  • type == "vanilla" (for vanilla redstone) or "bundled_cable" (for redpower bundled cable)

  • changes = table of strings denoting the changed sides, e.g. { "back", "bottom }

Color Legend:
String
Table




How to use?
  • Save the API Code and make note of the filename, e.g. 're'

  • Load the API within the program where you want to make use of it via os.loadAPI, e.g. os.loadAPI( "re" )

  • Then call the API's pullEvent by accessing it via the API name, e.g. re.pullEvent()



API - Code
Pastebin: http://pastebin.com/egdXMS6X
Direct-View:
Spoiler



Example Program to try out the API
Save the API as "re".
Then run the program below, change some redstone inputs and see the results on screen.
Pressing "e" will exit the program.
Pastebin: http://pastebin.com/SpSbfa0h
Direct-View:
Spoiler


#2 Liraal

  • New Members
  • 477 posts
  • LocationPoland

Posted 21 March 2012 - 02:08 PM

quite useful IMO, but one thing is bugging me: why didn't you modify the default os.pullEvent()?

#3 Espen

    Curious Explorer

  • Members
  • 708 posts

Posted 21 March 2012 - 02:19 PM

View PostLiraal, on 21 March 2012 - 02:08 PM, said:

quite useful IMO, but one thing is bugging me: why didn't you modify the default os.pullEvent()?
For SSP it would be relatively easy to change this via the 'bios.lua', but you don't necessarily have the same luxury on SMP.
Also the global table in CC is protected, so you can't easily change os.pullEvent during runtime.

But regardless of this, the most important reason I preserved the native os.pullEvent() is because this way you can use both versions concurrently. It might not be necessary, but I'm all for choice, so there you go.
Think of it as a kind of Java 'extend' on os.pullEvent. :(/>

#4 Liraal

  • New Members
  • 477 posts
  • LocationPoland

Posted 21 March 2012 - 04:28 PM

there is an easy way. :(/>

and when I am adding functionality, i never worry about compatibility :)/>

#5 Espen

    Curious Explorer

  • Members
  • 708 posts

Posted 21 March 2012 - 06:33 PM

You mean somthing like I did with os.pullEvent here? http://www.computerc...ndpost__p__2514

Yes, it is an easy workaround, but as I was saying the crucial point for me was to make it optional, so you can use both.
This way you can still do the workaround whenever and wherever you choose to, but are not forced to use it.
It's really just a personal design choice of mine. I don't want to force anything on the user. :(/>

#6 Liraal

  • New Members
  • 477 posts
  • LocationPoland

Posted 21 March 2012 - 07:11 PM

No I don't, I mean it more like:
t[1]=turtle.up
function up()
local state = t[1]()
if state then y=y+1 os.queueEvent("turtleUp") end
return state
end
rawset(turtle,"up",up)

which design I use for overwriting global variables/APIs.
But when I do so, I too always leave a way not to use it. :(/>

#7 FuzzyPurp

    Part-Time Ninja

  • Members
  • 510 posts
  • LocationHarlem, NY

Posted 21 March 2012 - 07:40 PM

Nice work Espen.

#8 Wolvan

  • New Members
  • 384 posts
  • LocationIn the TARDIS

Posted 01 September 2012 - 11:48 AM

Am I allowed to add this in one of my APIs maybe and giving credit?

#9 Espen

    Curious Explorer

  • Members
  • 708 posts

Posted 16 September 2012 - 02:04 PM

View PostWolvan, on 01 September 2012 - 11:48 AM, said:

Am I allowed to add this in one of my APIs maybe and giving credit?
Sure, go right ahead.
Everybody can do with my stuff what they want. That's why I use the WTF "license": http://en.wikipedia.org/wiki/WTFPL :)/>





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users