StendhalScripting/Lua: Difference between revisions
Content deleted Content added
imported>AntumDeluge →Adding Entities: instructions on how to add merchant behavior to NPCs |
imported>AntumDeluge →Objects and Functions: some instructions on using the "luajava" object |
||
Line 10:
The following objects & functions are exposed to the Lua engine:
=== luajava ===
This is an object of the LuajavaLib library. It can be used to coerce Java static objects to Lua or create new Java object instances.
Example of exposing a static object & enums to Lua:
<pre>
-- store a Java enum in a Lua global variable
ConversationStates = luajava.bindClass("games.stendhal.server.entity.npc.ConversationStates")
-- access the enum values like so
ConversationStates.IDLE
</pre>
Example of creating an object instance:
<pre>
-- store instance in local variable
local dog = luajava.newInstance("games.stendhal.server.entity.npc.SilentNPC")
-- access object methods like so
dog:setEntityClass("animal/puppy")
dog:setPosition(2, 5)
-- class with constructor using parameters
local speaker = luajava.newInstance("games.stendhal.server.entity.npc.SpeakerNPC", "Frank")
speaker:setOutfit("body=0,head=0,eyes=0,hair=5,dress=5")
speaker:setPosition(2, 6)
</pre>
To make scripting easier, Stendhal employs a {{StendhalFile|master|src/games/stendhal/server/core/scripting/lua/init.lua|master script}} & some helper objects & methods to handle the functionality mentioned above. An explanation of these objects & methods follows.
=== game ===
| |||