Lua Questions
Moderator: Forum Moderators
- battlestar
- Posts: 690
- Joined: January 1st, 2007, 7:12 am
Lua Questions
Reading through LuaWML, needing some help to understand a couple of things.
1. Is Lua capable of doing everything WML can inside [event] tags?
2. Saw some functions like W.redraw, W.message, and are they preset in the core or ones the user has to define? I couldn't find further details on them from the list of links near the bottom of the LuaWML page, and are there a list of similar functions?
3. Can [message] with multiple [option] done in Lua?
4. What are "prelude"'s referring to?
Thanks.
1. Is Lua capable of doing everything WML can inside [event] tags?
2. Saw some functions like W.redraw, W.message, and are they preset in the core or ones the user has to define? I couldn't find further details on them from the list of links near the bottom of the LuaWML page, and are there a list of similar functions?
3. Can [message] with multiple [option] done in Lua?
4. What are "prelude"'s referring to?
Thanks.
LUA: Llama Under Apprenticeship
Hell faction: completed
Hell faction: completed
-
- Inactive Developer
- Posts: 2461
- Joined: August 15th, 2008, 8:46 pm
- Location: Germany
Re: Lua Questions
yesbattlestar wrote:1. Is Lua capable of doing everything WML can inside [event] tags?
These are calls to the wml tags [redraw], [message] etc. (most likely). W is a shortcut for wesnoth.wml_actionsbattlestar wrote:2. Saw some functions like W.redraw, W.message, and are they preset in the core or ones the user has to define? I couldn't find further details on them from the list of links near the bottom of the LuaWML page, and are there a list of similar functions?
EDIT
Well, it could also be a shortcut for creating wml-table syntax. Depends on the context.
yes (see 1.)battlestar wrote:3. Can [message] with multiple [option] done in Lua?
Assuming it's preload; it's a wml event type executed when the player starts a scenario or loads a savegame.battlestar wrote:4. What are "prelude"'s referring to?
projects (BfW 1.12):
A Simple Campaign: campaign draft for wml starters • Plan Your Advancements: mp mod
The Earth's Gut: sp campaign • Settlers of Wesnoth: mp scenario • Wesnoth Lua Pack: lua tags and utils
updated to 1.8 and handed over: A Gryphon's Tale: sp campaign
A Simple Campaign: campaign draft for wml starters • Plan Your Advancements: mp mod
The Earth's Gut: sp campaign • Settlers of Wesnoth: mp scenario • Wesnoth Lua Pack: lua tags and utils
updated to 1.8 and handed over: A Gryphon's Tale: sp campaign
- Elvish_Hunter
- Posts: 1575
- Joined: September 4th, 2009, 2:39 pm
- Location: Lintanir Forest...
Re: Lua Questions
Just for completeness: it can be also a metatable created by using helper.set_wml_tag_metatable.Anonymissimus wrote:These are calls to the wml tags [redraw], [message] etc. (most likely). W is a shortcut for wesnoth.wml_actionsbattlestar wrote:2. Saw some functions like W.redraw, W.message, and are they preset in the core or ones the user has to define? I couldn't find further details on them from the list of links near the bottom of the LuaWML page, and are there a list of similar functions?
EDIT
Well, it could also be a shortcut for creating wml-table syntax. Depends on the context.
If you want to use a preload event, always use first_time_only=no. In past, I forgot this and nothing worked correctly. You can also load the Lua files in the _main.cfg, this will remove the need of using preload events.Anonymissimus wrote:Assuming it's preload; it's a wml event type executed when the player starts a scenario or loads a savegame.battlestar wrote:4. What are "prelude"'s referring to?
Current maintainer of these add-ons, all on 1.16:
The Sojournings of Grog, Children of Dragons, A Rough Life, Wesnoth Lua Pack, The White Troll (co-author)
The Sojournings of Grog, Children of Dragons, A Rough Life, Wesnoth Lua Pack, The White Troll (co-author)
- battlestar
- Posts: 690
- Joined: January 1st, 2007, 7:12 am
Re: Lua Questions
Do codes written in Lua run faster than WML?
(Because I may end up with tens of thousands of lines in WML code with tons of macros zig zagging through eachother to account for a variety of spells and other features, I'm concerned that things could get really laggy)
Is the current syntax for Lua in Wesnoth changing rapidly or pretty much stable?
Functions in Lua are pretty much similar to Classes in Java and macros in WML?
Are there any campaigns out there written with mostly with Lua?
Everything here: http://www.lua.org/manual/5.1/manual.html#2.3 applies?
What's a metatable?
Was the purpose of "local t = ..." initializing the variable "t"? What's "local" and what's "Vararg expression" aka "..." ?
(Having some trouble understanding how
put the string into t.text instead of t, or otherwise.)
Thanks.
(Because I may end up with tens of thousands of lines in WML code with tons of macros zig zagging through eachother to account for a variety of spells and other features, I'm concerned that things could get really laggy)
Is the current syntax for Lua in Wesnoth changing rapidly or pretty much stable?
Functions in Lua are pretty much similar to Classes in Java and macros in WML?
Are there any campaigns out there written with mostly with Lua?
Everything here: http://www.lua.org/manual/5.1/manual.html#2.3 applies?
What's a metatable?
Was the purpose of "local t = ..." initializing the variable "t"? What's "local" and what's "Vararg expression" aka "..." ?
(Having some trouble understanding how
Code: Select all
[args]
text = _ "Hello world!"
[/args]
Thanks.
LUA: Llama Under Apprenticeship
Hell faction: completed
Hell faction: completed
Re: Lua Questions
Yes, Lua is (much?) faster. If you have tens of thousands of lines of WML, you should, in my opinion, definitely switch to Lua.battlestar wrote:Do codes written in Lua run faster than WML?
(Because I may end up with tens of thousands of lines in WML code with tons of macros zig zagging through eachother to account for a variety of spells and other features, I'm concerned that things could get really laggy)
Pretty much stable.battlestar wrote:Is the current syntax for Lua in Wesnoth changing rapidly or pretty much stable?
Lua functions are more like WML custom events than WML macros, and more like Java, well, functions (methods?) than Java classes.battlestar wrote:Functions in Lua are pretty much similar to Classes in Java and macros in WML?
Yes. lua-users.org is also a good resource.battlestar wrote:Everything here: http://www.lua.org/manual/5.1/manual.html#2.3 applies?
A table associated with another table that provides custom functionality. Exempli gratia, if you try to add two tables, Lua will check if the first table's metatable contains anbattlestar wrote:What's a metatable?
__add
function. If it has such a function, the function will be called with the two tables as arguments. If the first table's metatable doesn't have an __add
function, the second table's metatable will be tried instead, and if it doesn't have an __add
function you'll get an error. There's also a metatable shared by all strings.Yes.battlestar wrote:Was the purpose of "local t = ..." initializing the variable "t"?
battlestar wrote:What's "local"
local
says that a variable is local, id est it will be deleted at scope exit (at the end of the function, if, for, etc.).battlestar wrote:and what's "Vararg expression" aka "..." ?
Code: Select all
function example (a, b, c)
local x = { ... }
-- if this function is called as example(1,2,3,4,5,6), x will be { 4, 5, 6 }
end
- Alarantalara
- Art Contributor
- Posts: 786
- Joined: April 23rd, 2010, 8:17 pm
- Location: Canada
Re: Lua Questions
Not quite. local means that it cannot be referenced outside of the scope it is defined in. Because Lua is block-structured (functions can be defined within functions, like Pascal), this makes local work somewhat like private members in Java classes in some cases. Also, local can be applied to functions as well.8680 wrote: local says that a variable is local, id est it will be deleted at scope exit (at the end of the function, if, for, etc.).
Re: Lua Questions
Apologies. I was tired. But my explanation is still true, if incomplete.
- battlestar
- Posts: 690
- Joined: January 1st, 2007, 7:12 am
Re: Lua Questions
Are there good online dictionaries for programming related terminologies? Thanks.
Does
end up with t.a=1, t.b=2, and t.c=3?
Could someone give me an example of message with options? Thanks.
What is the concept of metatables? Is it sort of like an array or matrix so that operations could be done to the values inside?
Local,
Does this mean the local function handler(t) is deleted at the end of ">>"?
Does
Code: Select all
[lua]
code = << local t = ...>>
[args]
a=1
b=2
c=3
[/args]
[/lua]
Could someone give me an example of message with options? Thanks.
What is the concept of metatables? Is it sort of like an array or matrix so that operations could be done to the values inside?
Local,
Code: Select all
[event]
name = preload
first_time_only = no
[lua]
code = <<
-- The function is now local, since its name does not have to be
-- visible outside this Lua scripts.
local function handler(t)
-- Behave like the [message] tag.
wesnoth.fire("message",
{ speaker = "narrator", message = t.sentence })
end
-- Create a new tag named [narrator].
wesnoth.register_wml_action("narrator", handler)
>>
[/lua]
[/event]
LUA: Llama Under Apprenticeship
Hell faction: completed
Hell faction: completed
Re: Lua Questions
Yes.battlestar wrote:Does ... end up with t.a=1, t.b=2, and t.c=3?
If my explanation was insufficient, see [1], [2], and [3].battlestar wrote:What is the concept of metatables? Is it sort of like an array or matrix so that operations could be done to the values inside?
Understand first that when you copy a table or function, pass it as an argument, or assign it to a variable (and note thatbattlestar wrote:Local, ... Does this mean the local function handler(t) is deleted at the end of ">>"?
function f() ... end
is the same as f = function() ... end
), you are actually copying, passing, or assigning a pointer to the table or function. { ... }
creates a table somewhere in memory and returns a pointer to it. function() ... end
creates a function somewhere and returns a pointer to it. Once no more pointers to a table or function exist (they were all in local variables that have gone out of scope, or maybe some were in global variables and were overwritten) the actual table or function will eventually be deleted by the garbage collector. Thus, when handler
goes out of scope (at the >>
), handler
, and the pointer to a function it contains, will be deleted. But before handler
goes out of scope, it, or rather the function pointer it contains, is copied with wesnoth.register_wml_action()
to wesnoth.wml_actions.narrator
, a global variable. Since a pointer to it still exists at scope exit, the function is not deleted at the >>
.- battlestar
- Posts: 690
- Joined: January 1st, 2007, 7:12 am
Re: Lua Questions
OOps, asked the same question twice. Thanks for the extra references, I still wasn't clear on the concept of metatables, and even more confused today as I am sick .
LUA: Llama Under Apprenticeship
Hell faction: completed
Hell faction: completed
Re: Lua Questions
I'd like to make one thing clear about metatables: Their only essential purpose is to hold metamethods. The name of a metamethod always starts with two underscores (__index, __add, etc.). Almost all valid metamethods are listed at link #1 that 8680 gave above.
The end result is to add functionality to a table that tables don't normally have, kind of like creating your own specialized data type. Here is a Wesnoth-specific example:
Here, the argument to set_wml_var_metatable is a new, empty table that will be returned by the function and assigned to V. This function sets a metatable that defines __index and __newindex in such a way that lets V act like an interface to the WML variables. When you access an element in V, __index returns the value of the corresponding WML variable. When you add a new element to V, __newindex assigns that data to the corresponding WML variable. Internally, the table V will be empty the whole time.
The end result is to add functionality to a table that tables don't normally have, kind of like creating your own specialized data type. Here is a Wesnoth-specific example:
Code: Select all
H = wesnoth.require 'lua/helper.lua'
V = H.set_wml_var_metatable{}
- Elvish_Hunter
- Posts: 1575
- Joined: September 4th, 2009, 2:39 pm
- Location: Lintanir Forest...
Re: Lua Questions
Complete with translation marks, it should be (untested):battlestar wrote:Could someone give me an example of message with options? Thanks.
Code: Select all
local _ = wesnoth.textdomain "wesnoth"
-- #textdomain wesnoth
wesnoth.wml_actions.message { speaker = "narrator",
image = "wesnoth-icon.png",
message = tostring( _"Test message" ),
{ "option", { message = tostring( _"Option 1" ),
{ "command", { wesnoth.wml_actions.redraw {} } }
} }
}
Current maintainer of these add-ons, all on 1.16:
The Sojournings of Grog, Children of Dragons, A Rough Life, Wesnoth Lua Pack, The White Troll (co-author)
The Sojournings of Grog, Children of Dragons, A Rough Life, Wesnoth Lua Pack, The White Troll (co-author)
- battlestar
- Posts: 690
- Joined: January 1st, 2007, 7:12 am
Re: Lua Questions
Thank you so much! Now the structures are starting to make some sense to me.
Could you show me an example of how the helper.set_wml_tag_metatable would allow less bracket usage?
Could nested events also be created via usage of W.message and brackets? If not, could someone show me an example of how they could be achieved?
Could you show me an example of how the helper.set_wml_tag_metatable would allow less bracket usage?
Could nested events also be created via usage of W.message and brackets? If not, could someone show me an example of how they could be achieved?
LUA: Llama Under Apprenticeship
Hell faction: completed
Hell faction: completed
Re: Lua Questions
The following are equivalent:
Code: Select all
## WML
#textdomain wesnoth-blah
[tag]
key1 = value1
key2 = _ "value2"
[subtag]
key3 = value3
[/subtag]
key4 = value4
[/tag]
-- Lua without tag constructors
local _ = wesnoth.textdomain "wesnoth-blah"
-- #textdomain wesnoth-blah
{"tag";{
key1 = "value1";
key2 = _ "value2";
{"subtag";{
key3 = "value3";
}};
key4 = "value4";
}};
-- Lua with tag constructors
local _ = wesnoth.textdomain "wesnoth-blah"
local T = wesnoth.require("lua/helper.lua").set_wml_tag_metatable {}
-- #textdomain wesnoth-blah
T.tag {
key1 = "value1";
key2 = _ "value2";
T.subtag {
key3 = "value3";
};
key4 = "value4";
};
- battlestar
- Posts: 690
- Joined: January 1st, 2007, 7:12 am