Lua & WML, when to use what?

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

Moderators: Forum Moderators, Developers

dracut
Posts: 7
Joined: September 29th, 2018, 4:06 pm

Lua & WML, when to use what?

Post by dracut » September 29th, 2018, 8:33 pm

What is Lua used for exactly? I was under the impression that WML was the sole scripting language of wesnoth. Is the language an alternative to WML? How do the two relate exactly?

User avatar
Pentarctagon
Forum Administrator
Posts: 3623
Joined: March 22nd, 2009, 10:50 pm
Location: Earth (occasionally)

Re: Lua & WML, when to use what?

Post by Pentarctagon » September 29th, 2018, 8:55 pm

WML is the original, homegrown markup-language-turned-scripting-language that Wesnoth uses. Lua support was added later in (IIRC) 1.7, and in many ways they are interchangeable - in fact, many WML tags are now implemented in lua themselves(see here), and you can define your own action and conditional WML tags via lua if you want.
99 little bugs in the code, 99 little bugs
take one down, patch it around
-2,147,483,648 little bugs in the code

dracut
Posts: 7
Joined: September 29th, 2018, 4:06 pm

Re: Lua & WML, when to use what?

Post by dracut » September 29th, 2018, 9:06 pm

Awesome! So will WML become deprecated eventually in favor of lua in future releases?

User avatar
Pentarctagon
Forum Administrator
Posts: 3623
Joined: March 22nd, 2009, 10:50 pm
Location: Earth (occasionally)

Re: Lua & WML, when to use what?

Post by Pentarctagon » September 29th, 2018, 9:10 pm

I won't say that's impossible, but I also wouldn't make any choices on which to use in a particular situation right now based on the assumption that WML might go away eventually either.
99 little bugs in the code, 99 little bugs
take one down, patch it around
-2,147,483,648 little bugs in the code

User avatar
Celtic_Minstrel
Developer
Posts: 1275
Joined: August 3rd, 2012, 11:26 pm
Contact:

Re: Lua & WML, when to use what?

Post by Celtic_Minstrel » September 29th, 2018, 10:56 pm

It's possible that ActionWML (the scripting portion of WML) will become deprecated in favour of Lua in a future release. It's far less likely that other uses of WML will be deprecated though.
Author of The Black Cross of Aleron campaign and Default++ era.
Maintainer of Steelhive.

Tad_Carlucci
Developer
Posts: 327
Joined: April 24th, 2016, 4:18 pm

Re: Lua & WML, when to use what?

Post by Tad_Carlucci » October 29th, 2018, 6:55 pm

Notice he said, "in many ways they are interchangeable."

One way in which you cannot use Lua, and must use WML, is when you want a variable to persist from scene to scene, or in a saved game. It's possible to do it in Lua but it's a painful operation. This is being worked on, but it's only one of several problems.

If you're thinking of working on a new scenario or campaign, your best bet at present is to stick with WML. Lua is there for when you need it, but most of the time you won't need it. If you find you're doing something which is too slow in WML, **then** is the time to take a look at using Lua.

For example, in HttT there was a desire to add random snow for one scene, based upon how long it took to make it through the previous scene. My first implementation was totally in WML. While it worked, there was a noticeable slow-down of several seconds while the snow was being placed. Replacing that WML with Lua made the entire process unnoticeable.

Yes, I could rewrite the entirety of HttT using Lua as the functional language, only using WML to set up each scene. But, to be honest, that would be a lot of work for no appreciable gain. In addition, it would mean that I am limiting the number of people who can help maintain it to only those who are well-versed in Lua, abandoning all those who work primarily in WML.

My advise on WML vs Lua is the same as on any other language choice: know your market (both supplies and consumers) and choose the most appropriate tool for each task.
I forked real life and now I'm getting merge conflicts.

User avatar
Ravana
Moderator
Posts: 1887
Joined: January 29th, 2012, 12:49 am
Location: Estonia
Contact:

Re: Lua & WML, when to use what?

Post by Ravana » October 29th, 2018, 8:09 pm

Lua gives additional tools like possibility to add more elements to user interface, but for normal use WML is good. If you have previous programming experience, then you can use Lua for event actions, otherwise better to start with WML there too.

User avatar
Celtic_Minstrel
Developer
Posts: 1275
Joined: August 3rd, 2012, 11:26 pm
Contact:

Re: Lua & WML, when to use what?

Post by Celtic_Minstrel » October 30th, 2018, 2:27 am

Tad_Carlucci wrote:
October 29th, 2018, 6:55 pm
One way in which you cannot use Lua, and must use WML, is when you want a variable to persist from scene to scene, or in a saved game. It's possible to do it in Lua but it's a painful operation. This is being worked on, but it's only one of several problems.
This is what I added wesnoth.persistent_tags for. It's at least an improvement on the previous method of persisting Lua data in saved games.
Author of The Black Cross of Aleron campaign and Default++ era.
Maintainer of Steelhive.

enclave
Posts: 909
Joined: December 15th, 2007, 8:52 am

Re: Lua & WML, when to use what?

Post by enclave » October 30th, 2018, 7:15 am

Lua & WML, when to use what?
You have received some answers from mainline developers mostly, I will just confirm what they already said from my amateur add-on maker point of view..
I have been writing some add-ons for 2-3 years, simple and advanced ones, and based on that I could tell...

You don't need lua in 99% of cases.. you just don't need it.. Only time you need lua is (a) when you want to optimize your code to run faster (your FOREACH loop is slowing down the game somewhere... WML loops may be slow and make your game lag in huge calculations. If you dont have lags or slow downs then you dont need lua.. and it's 99% time..) and then (b) when there is no WML alternative for the function... it's also rare... WML has most things any new add-on creator may need... 99% of time u can use WML alternative..

If you are an advanced programmer you may just find WML too long (sometimes writing in lua is much more simple... you can write 2-3 lines in lua, same thing that WML could take 10 lines or more.. because you opening each tag, then you closing it.. etc). As an amateur programmer I don't find WML unusable, it's very nice scripting language or whatever it is.. I like it. I think it should never be deprecated, there are a lot of add-on makers without programming background and lua would be unusable for such.. while WML can be understood by even a kid and it may be a good step to get some people to start learning proper programming languages.. and it might be lua... wesnoth is great for making people learn programming.. or it could be... if it had more people involved.

during my 2-3 years of add-on making I only was using lua when I needed it... and it was only when there was no alternative or when WML was too slow.. I still stick to WML 99% of time.. there is nothing wrong with it. WML is NOT slow in general, it is only slow when you are doing massive loops of calculations.. some WML functions may even be faster than lua when they are used separate (not in loops). Most of all I love the freedom of WML, you dont need to convert strings to numbers.. your arrays are simple to use and neverending.. I really think developers could go further with WML and make it some kind of international programming learning language for beginners lol.. although it might be hard to go from WML to proper languages.. because then you would meet a sad reality... where integer and string can not mix, you need to declare every variable before use, etc etc etc lol..

PS. one last thing to say.. you can make an add-on without lua, using just WML, but I very very very very very much doubt that you can make any add-on with just lua, without WML. So from my point of view, lua is only a helping language for add-on making, while WML is the general use one.
PPS. well actually u probably could make add-on with only lua, but it would probably look stupid.. from programmer point of view. no idea really.. wouldn't even try.

Tad_Carlucci
Developer
Posts: 327
Joined: April 24th, 2016, 4:18 pm

Re: Lua & WML, when to use what?

Post by Tad_Carlucci » October 30th, 2018, 12:51 pm

enclave wrote:
October 30th, 2018, 7:15 am
or whatever it is
That's my biggest complaint about WML: the Wesnoth Mishmash Language /s
:lol:
I forked real life and now I'm getting merge conflicts.

gfgtdf
Developer
Posts: 1110
Joined: February 10th, 2013, 2:25 pm

Re: Lua & WML, when to use what?

Post by gfgtdf » October 30th, 2018, 11:10 pm

I actually disagree with most peolpe here, Of course it depends on what progrmming experience you have already and how complex the code is that you want to have in sour scenario. But only using luad for specific taks that are slow is not what i'd reccomend.

If i would create a campaign now, i'd probably do as much as possible (this is: everything that goes into [scenario], usually that is: everythiung except unit and terraintype definitions) in lua. (I have to say though that i also have programmign experience, and usually write complex addons)

Some advantages of using lua:
1) It's faster, always (unless your write bad code. Of course a badly written lua code will be slower than a wml code written by a wml expert) this is easy to proof becasue most of wml itself is implemented in lua. Of course there are a lot of places where the perfomance gain is unnoticable, but is generally, in particular for beginner hard to tell without testing whether your code is actualyl efects on this or not. In general, things that are slow in wml are loops and [unstore_unit]. And well, why should i spend time on think about which code is how much faster when i could just write all my code in lua in the first place.
1.1) Not only excuting the code is after also parsing the code is faster, in wml many people use macros that can bloat up your code and heavily increase loading of your addon. Also with dofile/require you have goood control over what code is actualyl loaded while wml will always parse everythign in your campaign.

2) Its easier to debug, for multiple reasons:
2.1) It has local variables, in wml is often hard to tell where a variable is used in particular when macros are involved, the presence of macro often doesn't tell you anythign about what variables, it actualyl uses, in lua you have functions and local varible making your code easier to read.
2.2) Lua code is usually not stored in wml files which makes it independent, of wesnoth wml loading. Meaning: when you change your wml code you have to press F5 then restart your scenario from the start etc, to have an effect. In lua however, you just change the lua files, save and reload the scenario and the changes will be effective immidiately, making testing your your code much faster.
2.3) Wml has very bad error messages, in the _best_ case you get something like '[unstore_unit] variable contains invalid unit data' and you still have to figure out which of popssibly many [unstore_unit] call this is. In lua however you will get stacktraces with linenumbers and files as one woud expect.


3) Lua is a oftenused language that is not specific to wesnoth. this has multiple advantages
3.1) There exists maintained editors that explicitly support lua in terms of autoindentation, syntaxhighlighting and even autocompletion. Making lua code much easier to write.
3.2) For a specific problem that is not specific to wesnoth, take for exampel sorting, you will often find good code or libaries on the internet that do this stuff.
3.3) Forgot this one point will probably edit it in later.

3) Some things are simpyl not possible in lua (for example custom dialogs)

Some disadvantages of using lua:

1) You still need a bit of wml, even if you do the extreme 'all lua' approach you still have to write your unittypes in wml, and you need a minimalstic [scenario] skeleton that contains at least this:
Spoiler:
In particular you are forced to put scenario 'data' and 'code' into differnt files, if your scenario contains very little code, is probably not worth it.

2) The persistent storage expects wml. This can actually be worked around by writing a serialising function with on_load/on_save . It's a little work but it is something that only needs to be done once, then you have your persistent lua storage for all addons your write.

3) A lot of other wesnoth api expects wml, This can most of the time be worked around by adding a wrapper around those function that uses shema-based table conversion to turn normal lua tablers into wml object tables and vice versa, creating easy-to-use apis. It's a little work but it is something that only needs to be done once, then you have your nicer api for all addons your write.
Scenario with Robots SP scenario (1.11/1.12), allows you to build your units with components, PYR No preperation turn 1.12 mp-mod that allows you to select your units immideately after the game begins.

User avatar
Pentarctagon
Forum Administrator
Posts: 3623
Joined: March 22nd, 2009, 10:50 pm
Location: Earth (occasionally)

Re: Lua & WML, when to use what?

Post by Pentarctagon » October 30th, 2018, 11:49 pm

As far as performance goes, the large majority of the time I doubt this is a compelling reason to use one or the other. If you do end up needing the extra performance that lua can give, then of course use it, but unless you're doing something particularly complex it's unlikely anyone would notice a difference.

Lua's stack traces are actually usually quite a bit more wordy than they really need to be, and I think would be rather intimidating to look at for people new to coding in general.

Using GUI2 dialogs in add-ons, which you can only invoke from lua(ironically), are much easier to for me to read when written as WML rather a giant lua table.
99 little bugs in the code, 99 little bugs
take one down, patch it around
-2,147,483,648 little bugs in the code

User avatar
Celtic_Minstrel
Developer
Posts: 1275
Joined: August 3rd, 2012, 11:26 pm
Contact:

Re: Lua & WML, when to use what?

Post by Celtic_Minstrel » October 31st, 2018, 12:35 am

Pentarctagon wrote:
October 30th, 2018, 11:49 pm
Using GUI2 dialogs in add-ons, which you can only invoke from lua(ironically), are much easier to for me to read when written as WML rather a giant lua table.
This is why my way of using GUI2 dialogs is something like this:

Code: Select all

[lua]
    code=<<wesnoth.show_dialog(...)>>
    [args]
        # define the actual dialog here in standard GUI2 WML
    [/args]
[/lua]
(That's an oversimplification, usually you'll also need at least a pre-show function... but you get the general idea, hopefully.)
Author of The Black Cross of Aleron campaign and Default++ era.
Maintainer of Steelhive.

enclave
Posts: 909
Joined: December 15th, 2007, 8:52 am

Re: Lua & WML, when to use what?

Post by enclave » October 31st, 2018, 7:52 am

gfgtdf wrote:
October 30th, 2018, 11:10 pm
2.2) Lua code is usually not stored in wml files which makes it independent, of wesnoth wml loading. Meaning: when you change your wml code you have to press F5 then restart your scenario from the start etc, to have an effect. In lua however, you just change the lua files, save and reload the scenario and the changes will be effective immidiately, making testing your your code much faster.
this one would be for quite advanced lua writers.. i only use lua as a wml tag inclusion [lua] (for a reason i will specify in the end of this post)
gfgtdf wrote:
October 30th, 2018, 11:10 pm
2.3) Wml has very bad error messages, in the _best_ case you get something like '[unstore_unit] variable contains invalid unit data' and you still have to figure out which of popssibly many [unstore_unit] call this is. In lua however you will get stacktraces with linenumbers and files as one woud expect.
it's actually not that bad.. I think it gives you precise line of error too, at least most of the time I know where to look for mistake.
However! :) when macros inside macros are involved you will absolutely have no clue where to look for bug. It will not even be able to give you a hint.. or a hint would be like... 2% helpful or sometimes -2% helpful.. in WML it might make sense to do things little by little... because if you done a lot of changes and then something doesn't work it could be a nightmare to find it indeed. So yeah, lua has advantage here.. but to say that WML is very bad, I don't think so.. it's not that bad.. you may be looking for a while for a bug related to dollar sign... like if you put {VARIABLE $blabla 5} and you actually meant {VARIABLE blabla 5} or you may look for hours for bug related to macro inside macro.. but in general use it's quite not bad.. :)
gfgtdf wrote:
October 30th, 2018, 11:10 pm
If i would create a campaign now, i'd probably do as much as possible (this is: everything that goes into [scenario], usually that is: everythiung except unit and terraintype definitions) in lua. (I have to say though that i also have programmign experience, and usually write complex addons)
a single player campaign maybe.. for multiplayer I would still stick with WML, without any unit types, without any lua dofiles... etc.. because unfortunately, people still unlikely going to join a game where download is needed.. 1.14 is better than 1.12 because it downloads the add-on for you automatically.. but the way it is done is still quite not very helpful.. it's not running in background, it's refreshing the lobby, you need to find that game again and try to join.. then you end up having that add-on which u may never need again... and which may be badly coded and mess all your other add-ons.. and many new people will not even be able to identify the problem and get rid of that useless add-on. Plus if there is anything that needs downloading u will potentially have problems with versions, where 1 player has some images, another player doesn't because they on different versions.. and i think it makes life for a developer/player a bit more complicated than it should be... so my personal preference is 99% WML, only lua with [lua], no unit types, nothing that may need you download anything to join a game. I think the only way the things should idealy work is that every player would use 100% of code from the host, or something like that.. then there will be no version problems etc.. but I don't really have a 100% understanding of what it would look like in real world.. so it's just a strange opinion of incompetent person :)

User avatar
Ravana
Moderator
Posts: 1887
Joined: January 29th, 2012, 12:49 am
Location: Estonia
Contact:

Re: Lua & WML, when to use what?

Post by Ravana » October 31st, 2018, 11:41 am

I like middle ground for Lua files - https://github.com/ProditorMagnus/Oroci ... cfg#L1-L34 - for me it loads Lua code as file, for other people it loads as string. Then I get the benefit of instant reload still.

Post Reply