StendhalScripting/Lua: Difference between revisions

From Arianne
Jump to navigation Jump to search
Content deleted Content added
imported>AntumDeluge
Reserve page for Lua scripting instructions
 
imported>AntumDeluge
Adding some basic instructions
Line 1: Line 1:
<span style="color: red; font-style: italic;">this page is a work-in progress</span>
TODO

Stendhal supports [https://www.lua.org/ Lua scripting] via the [https://sourceforge.net/projects/luaj/ LuaJ library].

Lua scripts end in the <code>.lua</code> extension & are stored in the <code>data/script</code> directory.

== Objects and Functions ==

The following objects & functions are exposed to the Lua engine:

=== game ===

The main object that handles setting zone & adding entities to game.

Methods:
* <code>game:add(object)</code> - Adds an object to the current zone.
* <code>game:setZone(name)</code> - Sets the current zone.
* <code>game:createSign(visible)</code> - Creates a new {{StendhalFile|master|src/games/stendhal/server/entity/mapstuff/sign/Sign.java|Sign}} instance.
* <code>game:createShopSign(name, title, caption, seller)</code> - Creates a new {{StendhalFile|master|src/games/stendhal/server/entity/mapstuff/sign/ShopSign.java|ShopSign}} instance.

=== npcHelper ===

This object helps to create instances of {{StendhalFile|master|src/games/stendhal/server/entity/npc/SpeakerNPC.java|SpeakerNPC}} & {{StendhalFile|master|src/games/stendhal/server/entity/npc/SilentNPC.java|SilentNPC}} classes.

Methods:
* <code>npcHelper:createSpeakerNPC(name)</code> - Creates a new SpeakerNPC.
* <code>npcHelper:createSilentNPC()</code> - Creates a new SilentNPC.
* <code>npcHelper:setPath(npc, path, loop)</code> - Sets the path for the specified NPC.
* <code>npcHelper:setPathAndPosition(npc, path, loop)</code> - Sets the path & starting position of the specified NPC.
* <code>npcHelper:addMerchant(merchantType, npc, items, offer)</code> - Adds merchant behavior to <code>npc</code> of either a buyer or seller defined by <code>merchantType</code>.
* <code>npcHelper:addSeller(npc, items, offer)</code> - Adds seller merchant behavior to <code>npc</code>.
* <code>npcHelper:addBuyer(npc, items, offer)</code> - Adds buyer merchant behavior to <code>npc</code>.

== Setting Zone ==

To set the zone to work with, use the <code>game</code> object:

<pre>
game:setZone("0_semos_city")
</pre>

The logger is exposed to Lua via the <code>logger</code> object:

<pre>
local zone = "0_semos_city"
if game:setZone(zone) then
-- do something
else
logger:error("Could not set zone: " .. zone)
end
</pre>

== Adding Entities ==

=== Signs ===

Signs can be created with <code>game:createSign</code> and <code>game:createShopSign</code>:

<pre>
local zone = "0_semos_city"
if game:setZone(zone) then
-- create the sign instance
local sign = game:createSign()
sign:setEntityClass("signpost")
sign:setPosition(12, 55)
sign:setText("Meet Lua!")

-- Add it to the world
game:add(sign)
else
logger:error("Could not set zone: " .. zone)
end
</pre>

=== NPCs ===

Use the <code>game:createSpeakerNPC</code> method to create an interactive NPC:

<pre>
local zone = "0_semos_city"
if game:setZone(zone) then
-- Use helper object to create a new NPC
local npc = npcHelper:createSpeakerNPC("Lua")
npc:setEntityClass("littlegirlnpc")
npc:setPosition(10, 55)
npc:setBaseSpeed(0.1)
npc:setCollisionAction(CollisionAction.STOP)

local nodes = {
{10, 55},
{11, 55},
{11, 56},
{10, 56},
}

-- Use helper object to create NPC path
npcHelper:setPath(npc, nodes)

-- Dialogue
npc:addJob("Actually, I am jobless.")
npc:addGoodbye();

-- Add to the world
game:add(npc)
else
logger:error("Could not set zone: " .. zone)
end
</pre>

Revision as of 09:36, 27 February 2020

this page is a work-in progress

Stendhal supports Lua scripting via the LuaJ library.

Lua scripts end in the .lua extension & are stored in the data/script directory.

Objects and Functions

The following objects & functions are exposed to the Lua engine:

game

The main object that handles setting zone & adding entities to game.

Methods:

  • game:add(object) - Adds an object to the current zone.
  • game:setZone(name) - Sets the current zone.
  • game:createSign(visible) - Creates a new Sign instance.
  • game:createShopSign(name, title, caption, seller) - Creates a new ShopSign instance.

npcHelper

This object helps to create instances of SpeakerNPC & SilentNPC classes.

Methods:

  • npcHelper:createSpeakerNPC(name) - Creates a new SpeakerNPC.
  • npcHelper:createSilentNPC() - Creates a new SilentNPC.
  • npcHelper:setPath(npc, path, loop) - Sets the path for the specified NPC.
  • npcHelper:setPathAndPosition(npc, path, loop) - Sets the path & starting position of the specified NPC.
  • npcHelper:addMerchant(merchantType, npc, items, offer) - Adds merchant behavior to npc of either a buyer or seller defined by merchantType.
  • npcHelper:addSeller(npc, items, offer) - Adds seller merchant behavior to npc.
  • npcHelper:addBuyer(npc, items, offer) - Adds buyer merchant behavior to npc.

Setting Zone

To set the zone to work with, use the game object:

game:setZone("0_semos_city")

The logger is exposed to Lua via the logger object:

local zone = "0_semos_city"
if game:setZone(zone) then
	-- do something
else
	logger:error("Could not set zone: " .. zone)
end

Adding Entities

Signs

Signs can be created with game:createSign and game:createShopSign:

local zone = "0_semos_city"
if game:setZone(zone) then
	-- create the sign instance
	local sign = game:createSign()
	sign:setEntityClass("signpost")
	sign:setPosition(12, 55)
	sign:setText("Meet Lua!")

	-- Add it to the world
	game:add(sign)
else
	logger:error("Could not set zone: " .. zone)
end

NPCs

Use the game:createSpeakerNPC method to create an interactive NPC:

local zone = "0_semos_city"
if game:setZone(zone) then
	-- Use helper object to create a new NPC
	local npc = npcHelper:createSpeakerNPC("Lua")
	npc:setEntityClass("littlegirlnpc")
	npc:setPosition(10, 55)
	npc:setBaseSpeed(0.1)
	npc:setCollisionAction(CollisionAction.STOP)

	local nodes = {
		{10, 55},
		{11, 55},
		{11, 56},
		{10, 56},
	}

	-- Use helper object to create NPC path
	npcHelper:setPath(npc, nodes)

	-- Dialogue
	npc:addJob("Actually, I am jobless.")
	npc:addGoodbye();

	-- Add to the world
	game:add(npc)
else
	logger:error("Could not set zone: " .. zone)
end