StendhalScripting/Lua: Difference between revisions
Jump to navigation
Jump to search
Content deleted Content added
imported>AntumDeluge |
imported>AntumDeluge add categories |
||
| (7 intermediate revisions by the same user not shown) | |||
| Line 50: | Line 50: | ||
-- table variable |
-- table variable |
||
local var4 = {} |
local var4 = {} |
||
</pre> |
|||
=== Strings === |
|||
==== String Concatenation ==== |
|||
String concatenation is simple, much like Java uses a plus operator (<code>+</code>) to join strings, Lua uses two periods (<code>..</code>). |
|||
Example: |
|||
<pre> |
|||
-- create a string variable |
|||
local var = "Hello" |
|||
-- append another string |
|||
var = var .. " world!" |
|||
print(var) -- prints "Hello world!" |
|||
</pre> |
</pre> |
||
| Line 261: | Line 278: | ||
=== Add Zone Music === |
=== Add Zone Music === |
||
Music can be added to zones with the <code> |
Music can be added to zones with the <code>game:setMusic</code> function. It supports the following arguments: |
||
* |
* <span style="color:darkgreen; font-style:italic;>filename:</span> Basename of the OGG audio file to use stored in {{StendhalFile|master|data/music|data/music}}. |
||
* <span style="color:darkgreen; font-style:italic;>args:</span> A table of key=value integers. |
|||
* ''volume:'' (optional) Volume level. |
|||
* Valid keys: |
|||
| ⚫ | |||
** <span style="color:darkblue; font-style:italic;">volume:</span> Volume level (default: 100). |
|||
| ⚫ | |||
| ⚫ | |||
| ⚫ | |||
| ⚫ | |||
| ⚫ | |||
Example: |
Example: |
||
<pre> |
<pre> |
||
if game:setZone("0_semos_plains_n") then |
if game:setZone("0_semos_plains_n") then |
||
game:setMusic("pleasant_creek_loop", {volume=85, radius=100}) |
|||
end |
end |
||
</pre> |
</pre> |
||
| Line 318: | Line 337: | ||
} |
} |
||
| ⚫ | |||
-- Use helper object to create NPC path |
|||
| ⚫ | |||
-- Dialogue |
-- Dialogue |
||
| Line 347: | Line 365: | ||
This simply adds a response to saying "hello" & sets the NPC to attend to the player (equivalent of <code>frank:addGreeting("Hello")</code>). |
This simply adds a response to saying "hello" & sets the NPC to attend to the player (equivalent of <code>frank:addGreeting("Hello")</code>). |
||
For more complicated behavior, we need to use some helper methods. If we want to check a condition we use the <code> |
For more complicated behavior, we need to use some helper methods. If we want to check a condition we use the <code>conditions:create</code> method. The first parameter is the string name of the ChatCondition we want to instantiate. The second parameter is a table that contains the values that should be passed to the ChatCondition constructor. |
||
Example: |
|||
<pre> |
<pre> |
||
frank:add(ConversationStates.IDLE, |
frank:add(ConversationStates.IDLE, |
||
ConversationPhrases.GREETING_MESSAGES, |
ConversationPhrases.GREETING_MESSAGES, |
||
conditions:create("PlayerHasItemWithHimCondition", {"money"}), |
|||
ConversationStates.ATTENDING, |
ConversationStates.ATTENDING, |
||
"Hello.", |
"Hello.", |
||
| Line 359: | Line 379: | ||
In this scenario, the NPC will only respond if the player is carrying <item>money</item>. |
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> |
A NotCondition instance can be created with the <code>actions:notCondition</code> method: |
||
Example usage: |
Example usage: |
||
<pre> |
<pre> |
||
| ⚫ | |||
-- using newNotCondition |
|||
local condition = newNotCondition("PlayerHasItemWithHimCondition", "money") |
|||
-- using conditions.not |
|||
| ⚫ | |||
</pre> |
</pre> |
||
To add a ChatAction, we use the <code> |
To add a ChatAction, we use the <code>actions:create</code> method. Its usage is identical to <code>conditions:create</code>. |
||
Example: |
|||
<pre> |
<pre> |
||
frank:add(ConversationStates.IDLE, |
frank:add(ConversationStates.IDLE, |
||
ConversationPhrases.GREETING_MESSAGES, |
ConversationPhrases.GREETING_MESSAGES, |
||
conditions:create("PlayerHasItemWithHimCondition", {"money"}), |
|||
ConversationStates.ATTENDING, |
ConversationStates.ATTENDING, |
||
"Hello.", |
"Hello.", |
||
actions:create("NPCEmoteAction", {"looks greedily at your pouch of money.", false})) |
|||
</pre> |
</pre> |
||
| Line 385: | Line 403: | ||
ConversationPhrases.GREETING_MESSAGES, |
ConversationPhrases.GREETING_MESSAGES, |
||
{ |
{ |
||
conditions:create("PlayerHasItemWithHimCondition", {"money"}), |
|||
conditions:notCondition(conditions:create("NakedCondition")), |
|||
}, |
}, |
||
ConversationStates.ATTENDING, |
ConversationStates.ATTENDING, |
||
nil, |
nil, |
||
{ |
{ |
||
actions:create("SayTextAction", {"Hello."}), |
|||
actions:create("NPCEmoteAction", {"looks greedily at your pouch of money.", false}), |
|||
}) |
}) |
||
</pre> |
</pre> |
||
| Line 401: | Line 419: | ||
<pre> |
<pre> |
||
local conditions = { |
local conditions = { |
||
conditions:create("PlayerHasItemWithHimCondition", {"money"}), |
|||
{ |
{ |
||
conditions:notCondition(conditions:create("NakedCondition")), |
|||
}, |
}, |
||
} |
} |
||
| Line 413: | Line 431: | ||
nil, |
nil, |
||
{ |
{ |
||
actions:create("SayTextAction", {"Hello."}), |
|||
actions:create("NPCEmoteAction", {"looks greedily at your pouch of money.", false}), |
|||
}) |
}) |
||
</pre> |
</pre> |
||
| Line 496: | Line 513: | ||
end |
end |
||
</pre> |
</pre> |
||
== Misc == |
|||
=== Typecasting === |
|||
Lua does not support typecasting (as far as I know), but if the class you want to cast to has a copy constructor, achieving the same functionality is quite simple. |
|||
<pre> |
|||
-- "entities:getItem" returns an instance of Item |
|||
local bestiary = entities:getItem("bestiary") |
|||
-- in order to use the bestiary's "setOwner" method, we must convert it to an "OwnedItem" instance by calling its copy constructor |
|||
bestiary = luajava.newInstance("games.stendhal.server.entity.item.OwnedItem", bestiary) |
|||
bestiary:setOwner("Ted") |
|||
</pre> |
|||
= See Also = |
|||
* [[StendhalScripting/LuaAPI|Lua API]] |
|||
[[Category:Stendhal]] |
|||
[[Category:Documentation]] |
|||
[[Category:API]] |
|||
[[Category:Scripting]] |
|||
[[Category:Lua]] |
|||