StendhalScripting/Lua: Difference between revisions
Content deleted Content added
imported>AntumDeluge →Adding Transitions: nested tables |
imported>AntumDeluge add categories |
||
| (36 intermediate revisions by the same user not shown) | |||
Line 24:
== Variables ==
<pre>
-- a global variable
Line 31:
-- a local variable
local var2 = "Hello world!"
</pre>
== Data Types ==
Some common data types in Lua are ''string'', ''integer'', ''boolean'', & ''table''. Type names do not need to be declared when setting variables.
Examples:
<pre>
-- string variable
local var1 = "Hello world!"
-- integer variable
local var2 = 11
-- boolean variable
local var3 = true
-- 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>
=== Tables ===
A Lua table is a data type similar to a Java list or map. Tables can be indexed or use key=value pairs.
''(<span style="color:red;">IMPORTANT NOTE: Lua table indexes begin at 1, not 0</span>)''
==== Creating Tables ====
An empty table is initialized with a pair of curly braces (<code>{}</code>):
Line 47 ⟶ 85:
<pre>
-- create a table with values
local mytable = {"foo"}
-- add value
Line 57 ⟶ 93:
To create a key=value table, any of the following methods can be used to add values:
<pre>
-- all of these do the same thing, that is, assigning "bar" to mytable.foo
local mytable {
foo = "bar",
["foo"] = "bar",
}
mytable.foo = "bar"
mytable["foo"] = "bar"
</pre>
==== Accessing Table Values ====
Square brackets (<code>[]</code>) enclosing an index number are used to access values in indexed tables (''remember that Lua table indexes start at "1" not "0"''):
<pre>
local mytable = {"foo", "bar"}
print(mytable[1]) -- prints "foo"
print(mytable[2]) -- prints "bar"
</pre>
In a key=value table, values can be accessed by either enclosing the key string in square brackets or concatenating the key member using a <code>.</code>:
<pre>
local mytable = {foo="bar"}
-- using square brackets
print(mytable["foo"]) -- prints "bar"
-- using concatenated member
print(mytable.foo) -- prints "bar"
</pre>
==== Iterating Tables ====
Tables can be iterated in a
<pre>
local mytable = {"foo", "bar"}
print("indexes:")
Line 132 ⟶ 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 146 ⟶ 200:
</pre>
Functions can also be
<pre>
local myTable = {}
Line 174 ⟶ 228:
</pre>
== Comparison Operators ==
{| class="wikitable"
|+ Logical Operators
! Operator !! Description !! Java Equivalent
|-
| and || logical ''and'' || &&
|-
| or || logical ''or'' || <nowiki>||</nowiki>
|-
| not || logical ''opposite'' || !
|}
{| class="wikitable"
|+ Relational Operators
! Operator !! Description !! Java Equivalent
|-
| < || less than || <
|-
| > || greater than || >
|-
| <= || less than or equal to || <=
|-
| >= || greater than or equal to || >=
|-
| == || equal to || ==
|-
| ~= || not equal to || !=
|}
= Stendhal Application =
== Zones ==
=== Setting Zone ===
To set the zone to work with, use the <code>game</code> object:
<pre>
game:setZone("0_semos_city")
</pre>
=== Create New Zone ===
It is recommended to create new zones in the XML configurations in {{StendhalFile|master|data/conf/zones|data/conf/zones}}.
Currently creating new zones via Lua is not supported.
=== Add Zone Music ===
* <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.
* Valid keys:
** <span style="color:darkblue; font-style:italic;">volume:</span> Volume level (default: 100).
** <span style="color:darkblue; font-style:italic;">x:</span> The horizontal point for the source of the music (default: 1).
** <span style="color:darkblue; font-style:italic;">y:</span> The vertical point for the source of the music (default: 1).
** <span style="color:darkblue; font-style:italic;">radius:</span> The radial range at which the music can be heard (default: 10000).
Example:
<pre>
if game:setZone("0_semos_plains_n") then
game:setMusic("pleasant_creek_loop", {volume=85, radius=100})
end
</pre>
Line 255 ⟶ 298:
=== Signs ===
Signs can be created with <code>
<pre>
Line 261 ⟶ 304:
if game:setZone(zone) then
-- create the sign instance
local sign =
sign:setEntityClass("signpost")
sign:setPosition(12, 55)
Line 275 ⟶ 318:
=== NPCs ===
Use the <code>
<pre>
Line 281 ⟶ 324:
if game:setZone(zone) then
-- Use helper object to create a new NPC
local npc =
npc:setEntityClass("littlegirlnpc")
npc:setPosition(10, 55)
Line 294 ⟶ 337:
}
npc:setPath(nodes)
-- Dialogue
Line 312 ⟶ 354:
A simple example of adding a chat transition can be done without any special functionality:
<pre>
local frank =
frank:add(ConversationStates.IDLE,
ConversationPhrases.GREETING_MESSAGES,
Line 321 ⟶ 363:
</pre>
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 335 ⟶ 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>
</pre>
To add a ChatAction, we use the <code>
Example:
<pre>
frank:add(ConversationStates.IDLE,
ConversationPhrases.GREETING_MESSAGES,
ConversationStates.ATTENDING,
"Hello.",
</pre>
Line 355 ⟶ 403:
ConversationPhrases.GREETING_MESSAGES,
{
},
ConversationStates.ATTENDING,
nil,
{
})
</pre>
Line 371 ⟶ 419:
<pre>
local conditions = {
{
},
}
Line 383 ⟶ 431:
nil,
{
})
</pre>
==== Adding Merchant Behavior ====
The <code>merchants</code> object is used for adding merchant behavior (buying/selling) to an NPC.
Example of adding seller behavior to an NPC:
<pre>
if game:setZone("0_semos_city") then
local frank =
game:add(frank)
Line 440 ⟶ 479:
Then add the seller behavior using the custom list:
<pre>
</pre>
== System Properties ==
Java's system properties are exposed to Lua with the <code>properties</code> object.
Examples:
<pre>
-- property state
if properties:enabled("stendhal.testserver") then
print("Test server enabled")
if properties:equals("stendhal.testserver", "junk") then
print("Junk enabled")
else
print("Junk disabled")
end
else
print("Test server disabled")
end
-- property value
local prop = properties:getValue("stendhal.testserver")
if prop ~= nil then
print("Test server enabled")
if prop == "junk" then
print("Junk enabled")
else
print("Junk disabled")
end
else
print("Test server disabled")
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]]
| |||