Scripting: Difference between revisions
No edit summary |
No edit summary |
||
Line 22: | Line 22: | ||
var thiefGoal = { | var thiefGoal = { | ||
npc : npc, | npc : npc, | ||
reset : function() { | |||
this.player = null; | this.player = null; | ||
this.selector = null; | this.selector = null; | ||
}, | }, | ||
run : function() { | |||
if (!this.player || !this.player.isOnline()) { | if (!this.player || !this.player.isOnline()) { | ||
this.selector.finish(); | this.selector.finish(); | ||
Line 52: | Line 52: | ||
} | } | ||
}, | }, | ||
shouldExecute : function(selector) { | |||
if (!this.npc.isSpawned()) | if (!this.npc.isSpawned()) | ||
return false; | return false; | ||
Line 65: | Line 65: | ||
} | } | ||
} | } | ||
return false; | |||
}; | }; | ||
goals.addGoal(1, new Goal(thiefGoal)); | goals.addGoal(1, new Goal(thiefGoal)); |
Revision as of 09:42, 7 August 2012
Scripting Support in Citizens2
Citizens2 includes support for various scripting languages including Lua, Javascript and Python.
Javascript support is built into Java; additional languages can be added in through installing things such as JRuby (http://jruby.org/), Jython (http://www.jython.org/), Kahlua (http://www.ohloh.net/p/kahlua) and more. These use the Java Scripting API to give common scripting support to Citizens.
The scripting API contains a few built in objects in the global namespace which aid in some tasks. Currently, the 'events' object allows registering event listeners for Bukkit events and a reference to the Citizens plugin object is available under 'plugin'.
Let's see an example of how to use scripts. Citizens builds in by default the 'Behaviour' trait - this lets you specify AI tasks via scripts.
Scripts for behaviours should be placed in the plugins/Citizens/scripts/behaviours folder. Scripts are differentiated by their extension -- make sure it is correct!
Behaviours can be added to the NPC via the /npc behaviour [behaviours] command, where behaviours is a list of files to use.
When the script file is loaded by Citizens, it will call the method 'addGoals(Goals, NPC)'. Goals can be added via calling goals.addGoal(priority, goal), where goal implements the interface specified here -http://jd.citizensnpcs.net/net/citizensnpcs/api/ai/Goal.html.
Code: Example done in Javascript |