StendhalScripting/Lua: Difference between revisions
Content deleted Content added
imported>AntumDeluge →Tables: accessing table values |
imported>AntumDeluge add categories |
||
| (17 intermediate revisions by the same user not shown) | |||
Line 50:
-- table variable
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>
Line 169 ⟶ 186:
Like normal variables, functions can be declared as '''global''' or '''local''' & must be terminated with the <code>end</code> keyword.
There are two ways to
<pre>
local function myFunction()
Line 183 ⟶ 200:
</pre>
Functions can also be
<pre>
local myTable = {}
Line 261 ⟶ 278:
=== Add Zone Music ===
Music can be added to zones with the <code>
*
* <span style="color:darkgreen; font-style:italic;>args:</span> A table of key=value integers.
* Valid keys:
* ''x:'' (optional) The horizontal point for the source of the music.▼
** <span style="color:darkblue; font-style:italic;">volume:</span> Volume level (default: 100).
* ''y:'' (optional) The vertical point for the source of the music.▼
▲**
* ''radius:'' (optional) The radial range at which the music can be heard.▼
▲**
▲**
Example:
<pre>
if game:setZone("0_semos_plains_n") then
end
</pre>
Line 318 ⟶ 337:
}
▲ entities:setPath(npc, nodes)
-- Dialogue
Line 347 ⟶ 365:
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>
Example:
<pre>
frank:add(ConversationStates.IDLE,
ConversationPhrases.GREETING_MESSAGES,
ConversationStates.ATTENDING,
"Hello.",
Line 359 ⟶ 379:
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>
Example usage:
<pre>
local condition = conditions.
▲local condition = conditions.not(newCondition("PlayerHasItemWithHimCondition", "money")
</pre>
To add a ChatAction, we use the <code>
Example:
<pre>
frank:add(ConversationStates.IDLE,
ConversationPhrases.GREETING_MESSAGES,
ConversationStates.ATTENDING,
"Hello.",
</pre>
Line 385 ⟶ 403:
ConversationPhrases.GREETING_MESSAGES,
{
},
ConversationStates.ATTENDING,
nil,
{
})
</pre>
Line 401 ⟶ 419:
<pre>
local conditions = {
{
},
}
Line 413 ⟶ 431:
nil,
{
})
</pre>
Line 496 ⟶ 513:
end
</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]]
| |||