StendhalScripting/Lua: Difference between revisions
Jump to navigation
Jump to search
Content deleted Content added
imported>AntumDeluge add navigation menu |
imported>AntumDeluge →Adding Entities: instructions on how to add merchant behavior to NPCs |
||
| Line 108: | Line 108: | ||
logger:error("Could not set zone: " .. zone) |
logger:error("Could not set zone: " .. zone) |
||
end |
end |
||
</pre> |
|||
==== Adding Merchant Behavior ==== |
|||
Merchant behavior ''(buying/selling)'' can be set with one of the following helper functions: |
|||
* ''npcHelper:addMerchant(merchantType, npc, prices, addOffer)'' |
|||
* ''npcHelper:addBuyer(npc, prices, addOffer)'' |
|||
* ''npcHelper:addSeller(npc, prices, addOffer)'' |
|||
** Arguments: |
|||
*** ''merchantType:'' (string) If set to "buyer", will add buyer behavior, otherwise will be "seller" (may change type to boolean in future). |
|||
*** ''npc:'' (SpeakerNPC) The NPC to add the behavior to. |
|||
*** ''prices:'' (Map<String, Integer> or LuaTable) List of items & their prices. |
|||
*** ''addOffer:'' (boolean) If <code>true</code>, will add default replies for "offer". |
|||
Example of adding seller behavior to an NPC: |
|||
<pre> |
|||
if game:setZone("0_semos_city") then |
|||
local frank = npcHelper.createSpeakerNPC("Frank") |
|||
npcHelper:addSeller(frank, shops:get("shopname"), true) |
|||
game:add(frank) |
|||
end |
|||
</pre> |
|||
To create a custom shop list, you can use a Lua table (there are multiple ways to add elements to a Lua table): |
|||
Method 1: |
|||
<pre> |
|||
local priceList = { |
|||
meat = 50, |
|||
["ham"] = 70, |
|||
} |
|||
</pre> |
|||
Method 2: |
|||
<pre> |
|||
local priceList = {} |
|||
priceList.meat = 50 |
|||
priceList["ham"] = 70 |
|||
</pre> |
|||
The helper methods have special handling for underscore characters as well (the following are all the same): |
|||
<pre> |
|||
local priceList = { |
|||
smoked_ham = 100, |
|||
["smoked ham"] = 100, |
|||
} |
|||
priceList.smoked_ham = 100 |
|||
priceList["smoked ham"] = 100 |
|||
</pre> |
|||
Then add the seller behavior using the custom list: |
|||
<pre> |
|||
npcHelper:addSeller(frank, priceList, true) |
|||
</pre> |
</pre> |
||