StendhalScripting/Lua: Difference between revisions

Content deleted Content added
imported>AntumDeluge
imported>AntumDeluge
NPCs: adding transitions
Line 307:
end
</pre>
 
==== Adding Transitions ====
 
A simple example of adding a chat transition can be done without any special functionality:
<pre>
local frank = npcHelper:createSpeakerNPC("Frank")
frank:add(ConversationStates.IDLE,
ConversationPhrases.GREETING_MESSAGES,
nil,
ConversationStates.ATTENDING,
"Hello.",
nil)
</pre>
 
This simply adds a response to saying "hello" & sets the NPC to attend to the player.
 
For more complicated behavior, we need to use some helper methods. If we want to check a condition we use the <code>newCondition</code> global function:
<pre>
frank:add(ConversationStates.IDLE,
ConversationPhrases.GREETING_MESSAGES,
newCondition("PlayerHasItemWithHimCondition", "money"),
ConversationStates.ATTENDING,
"Hello.",
nil)
</pre>
 
In this scenario, the NPC will only respond if the player is carrying <item>money</item>.
 
A NotCondition instance can be created with the <code>newNotCondition</code> global function:
<pre>
newNotCondition(newCondition("PlayerHasItemWithHimCondition", "money"))
</pre>
 
To add a ChatAction, we use the <code>newAction</code> global function:
<pre>
frank:add(ConversationStates.IDLE,
ConversationPhrases.GREETING_MESSAGES,
newCondition("PlayerHasItemWithHimCondition", "money"),
ConversationStates.ATTENDING,
"Hello.",
newAction("NPCEmoteAction", "looks greedily at your pouch of money.", false))
</pre>
 
Lua tables can be used to add multiple conditions or actions:
<pre>
frank:add(ConversationStates.IDLE,
ConversationPhrases.GREETING_MESSAGES,
{
newCondition("PlayerHasItemWithHimCondition", "money"),
newNotCondition(newCondition("NakedCondition")),
},
ConversationStates.ATTENDING,
nil,
{
newAction("SayTextAction", "Hello."),
newAction("NPCEmoteAction", "looks greedily at your pouch of money.", false),
})
</pre>
 
In this scenario, the NPC will respond if the player has money & is not naked.
 
==== Adding Merchant Behavior ====