Giter Site home page Giter Site logo

twine-events's Introduction

Twine Events

A simple macro/js framework for Twine that adds pub/sub events. Kind of useful if you're doing fairly complicated scripting and want to simplify things, or otherwise wish to decouple functionality within your Twine game.

How it works

Either from Twine or JS, you can trigger Events which are sent to Listeners. Events have a name and optional data attached to them. Listeners are functions, that specify a particular Event name that they're interested in. When an Event of that name is triggered, any applicable Listener functions will be called with that Event.

This allows you to send data and call functions between Passages, without the source of the data having to know who's interested (and vice-versa). You can have an unlimited amount of Listeners, and call your Events whatever you want.

Check out the example example/simple_events.twee file to see how this can be used.

In Twine

  • To trigger a 'foo' event, use: <<event trigger foo>>

    or as a convenience method:

    <<event foo>>

  • To provide some data with the event, do: <<event foo val1 val2 ... valn>>

  • To attach a listener for 'foo' events, do:

     <<set 
     func = function fn(e){ alert(e.data[0]); } 
     $handler = func
     >>
    
     <<event on foo handler>>
    

In Javascript

  • To trigger events, call: Events.trigger('foo', ["val1", "val2"]);
  • To register a listener, use: Events.on('foo', function(e){ alert(e.data[0]); });

Events

The data structure of the Event Object is as follows:

	event.type; 		//the base part of the event name. If the event was fired for 'foo.bar.baz', the type will equal 'foo'
	event.namespace; 	//the full name of the event
	event.data; 		//an array containing whatever values were passed when the event was triggered
	event.passage; 		//a reference to the Passage object from which this event was triggered
	event.currentTarget; 	//a reference to the DOM object corresponding to the Event's Passage instance
	event.timestamp; 	//Epoch timestamp indicating when the Event object was created

Namespaces

Events require names (the 'foo' bit in the examples above). However, these can be broken into dot-separated parts - so, you can do <<event foo.bar>> for example.

This way, you can register multiple listeners with different orders of specificity. A listener registered to 'foo' will get any events that start with "foo", including "foo.bar", whereas a listener matching 'foo.bar' will only ever be called against events for "foo.bar". Feel free to email me if that doesn't makes sense, but trust me, it's useful.

Removing listeners

If you add a listener in JS, the method will return an integer index of the listener you have added. var index = Events.on('foo', function(e) {});

You can use this index to remove the same event listener at a later point, by doing: Events.off(index);

To-do

Allow the removal of handlers via Twine macro. Add proper bubbling/containment to events.

Comments, forks and pull-requests welcome :)

twine-events's People

Contributors

peterhughesdev avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.