Lua Questions

Discussion of Lua and LuaWML support, development, and ideas.

Moderator: Forum Moderators

User avatar
battlestar
Posts: 690
Joined: January 1st, 2007, 7:12 am

Lua Questions

Post by battlestar »

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.
LUA: Llama Under Apprenticeship
Hell faction: completed
Anonymissimus
Inactive Developer
Posts: 2461
Joined: August 15th, 2008, 8:46 pm
Location: Germany

Re: Lua Questions

Post by Anonymissimus »

battlestar wrote:1. Is Lua capable of doing everything WML can inside [event] tags?
yes
battlestar 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?
These are calls to the wml tags [redraw], [message] etc. (most likely). W is a shortcut for wesnoth.wml_actions

EDIT
Well, it could also be a shortcut for creating wml-table syntax. Depends on the context.
battlestar wrote:3. Can [message] with multiple [option] done in Lua?
yes (see 1.)
battlestar wrote:4. What are "prelude"'s referring to?
Assuming it's preload; it's a wml event type executed when the player starts a scenario or loads a savegame.
projects (BfW 1.12):
A Simple Campaign: campaign draft for wml startersPlan Your Advancements: mp mod
The Earth's Gut: sp campaignSettlers of Wesnoth: mp scenarioWesnoth Lua Pack: lua tags and utils
updated to 1.8 and handed over: A Gryphon's Tale: sp campaign
User avatar
Elvish_Hunter
Posts: 1575
Joined: September 4th, 2009, 2:39 pm
Location: Lintanir Forest...

Re: Lua Questions

Post by Elvish_Hunter »

Anonymissimus wrote:
battlestar 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?
These are calls to the wml tags [redraw], [message] etc. (most likely). W is a shortcut for wesnoth.wml_actions

EDIT
Well, it could also be a shortcut for creating wml-table syntax. Depends on the context.
Just for completeness: it can be also a metatable created by using helper.set_wml_tag_metatable.
Anonymissimus wrote:
battlestar wrote:4. What are "prelude"'s referring to?
Assuming it's preload; it's a wml event type executed when the player starts a scenario or loads a savegame.
If you want to use a preload event, always use first_time_only=no. In past, I forgot this and nothing worked correctly. :P You can also load the Lua files in the _main.cfg, this will remove the need of using preload events.
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)
User avatar
battlestar
Posts: 690
Joined: January 1st, 2007, 7:12 am

Re: Lua Questions

Post by battlestar »

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

Code: Select all

    [args]
        text = _ "Hello world!"
    [/args]
put the string into t.text instead of t, or otherwise.)

Thanks.
LUA: Llama Under Apprenticeship
Hell faction: completed
User avatar
8680
Moderator Emeritus
Posts: 742
Joined: March 20th, 2011, 11:45 pm
Location: The past

Re: Lua Questions

Post by 8680 »

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)
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:Is the current syntax for Lua in Wesnoth changing rapidly or pretty much stable?
Pretty much stable.
battlestar wrote:Functions in Lua are pretty much similar to Classes in Java and macros in WML?
Lua functions are more like WML custom events than WML macros, and more like Java, well, functions (methods?) than Java classes.
battlestar wrote:Everything here: http://www.lua.org/manual/5.1/manual.html#2.3 applies?
Yes. lua-users.org is also a good resource.
battlestar wrote:What's a metatable?
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 an __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.
battlestar wrote:Was the purpose of "local t = ..." initializing the variable "t"?
Yes.
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
User avatar
Alarantalara
Art Contributor
Posts: 786
Joined: April 23rd, 2010, 8:17 pm
Location: Canada

Re: Lua Questions

Post by Alarantalara »

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.).
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.
User avatar
8680
Moderator Emeritus
Posts: 742
Joined: March 20th, 2011, 11:45 pm
Location: The past

Re: Lua Questions

Post by 8680 »

Apologies. I was tired. But my explanation is still true, if incomplete.
User avatar
battlestar
Posts: 690
Joined: January 1st, 2007, 7:12 am

Re: Lua Questions

Post by battlestar »

Are there good online dictionaries for programming related terminologies? Thanks.

Does

Code: Select all

[lua]
    code = << local t = ...>>
    [args]
        a=1
        b=2
        c=3
    [/args]
[/lua]
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,

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]
Does this mean the local function handler(t) is deleted at the end of ">>"?
LUA: Llama Under Apprenticeship
Hell faction: completed
User avatar
8680
Moderator Emeritus
Posts: 742
Joined: March 20th, 2011, 11:45 pm
Location: The past

Re: Lua Questions

Post by 8680 »

battlestar wrote:Does ... end up with t.a=1, t.b=2, and t.c=3?
Yes.
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?
If my explanation was insufficient, see [1], [2], and [3].
battlestar wrote:Local, ... Does this mean the local function handler(t) is deleted at the end of ">>"?
Understand first that when you copy a table or function, pass it as an argument, or assign it to a variable (and note that 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 >>.
User avatar
battlestar
Posts: 690
Joined: January 1st, 2007, 7:12 am

Re: Lua Questions

Post by battlestar »

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
Luther
Posts: 128
Joined: July 28th, 2007, 5:19 pm
Location: USA

Re: Lua Questions

Post by Luther »

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:

Code: Select all

H = wesnoth.require 'lua/helper.lua'
V = H.set_wml_var_metatable{}
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.
User avatar
Elvish_Hunter
Posts: 1575
Joined: September 4th, 2009, 2:39 pm
Location: Lintanir Forest...

Re: Lua Questions

Post by Elvish_Hunter »

battlestar wrote:Could someone give me an example of message with options? Thanks.
Complete with translation marks, it should be (untested):

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 {} } }
                     } }
                   }
Using wml_actions = wesnoth.wml_actions is a trick used in mainline to shorten the code; also, using helper.set_wml_tag_metatable allows you to use less brackets.
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)
User avatar
battlestar
Posts: 690
Joined: January 1st, 2007, 7:12 am

Re: Lua Questions

Post by battlestar »

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?
LUA: Llama Under Apprenticeship
Hell faction: completed
User avatar
8680
Moderator Emeritus
Posts: 742
Joined: March 20th, 2011, 11:45 pm
Location: The past

Re: Lua Questions

Post by 8680 »

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";
};
User avatar
battlestar
Posts: 690
Joined: January 1st, 2007, 7:12 am

Re: Lua Questions

Post by battlestar »

Thanks!
LUA: Llama Under Apprenticeship
Hell faction: completed
Post Reply