HOWTO: Write a Lily external

From LilyWiki

Jump to: navigation, search

Here's a quick tutorial to get you started writing a "Hello World" external object for Lily. Externals are the individual code modules that provide some specific functionality in a Lily program. In this tutorial, we'll create an external that prints out the string "Hello World" when it receives a "bang" message from a button.

1) First create a folder where you'll keep your code. In the Lily preferences dialog, (Help->Options) select that folder as your "Object Search Path". This will be the root directory where Lily will look for external objects to load. Searching is recursive, so you can create additional directories in this folder to keep things organized. Just don't store .js files that aren't valid Lily objects in here or it will cause problems.

2) Copy the javascript code below and save it in a file called "helloworld.js" somewhere in your Search Path folder. Restart Firefox and open a new patch document. (Tools->Lily->New). Double click on the patch window and begin typing "helloworld". You should see the name in the autocomplete list. Create the object and connect a "button" to the inlet and a "print" to the outlet. That's it. Clicking the button should print a hearty "hello world!" to the debug window.


//save as helloworld.js

//the name of the constructor is the filename/classname with a prepended "$".
function $helloworld()
{
	//save this to use in our inner functions 
	var thisPtr=this;
	
	//create an inlet- args to the inlet constructor are: inlet name, the current context (always this), help text.
	this.inlet1=new this.inletClass("inlet1",this,"bang outputs the message \"hello world\"");

	//create an outlet- args to the outlet constructor are: outlet name, the current context (always this), help text.
	this.outlet1 = new this.outletClass("outlet1",this,"outputs \"hello world\" after receiving a bang.");

	//adding an "bang" method to the inlet. when the inlet receives a "bang" message this handler will be called.
	this.inlet1["bang"]=function() {
		thisPtr.outlet1.doOutlet("hello world!"); //call the outlet's output method and send our greeting.
	}	
	
	return this;
}

//meta data module- required. the module name should take the form "$"+ classname/filename +"MetaData"
var $helloworldMetaData = {
	textName:"helloworld", //the name as it will appear to the user- can be different from the filename/classname
	htmlName:"helloworld", //same as above, but valid for an xhtml document with appropriate entity substitutions. 
	objectCategory:"Sample", //where to file, need not be an existing category
	objectSummary:"Outputs hello world on receiving a bang.", //one sentence description for help
	objectArguments:"" //also for help- object argument list if any, otherwise empty.
}

Personal tools