StendhalScripting/Lua: Difference between revisions

From Arianne
Jump to navigation Jump to search
Content deleted Content added
imported>AntumDeluge
Adding some basic instructions
imported>AntumDeluge
add navigation menu
Line 1: Line 1:
{{Navigation for Stendhal Top|Playing}}

<span style="color: red; font-style: italic;">this page is a work-in progress</span>
<span style="color: red; font-style: italic;">this page is a work-in progress</span>



Revision as of 19:25, 29 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