Support for Lua scripts in 1.7

The place to post your WML questions and answers.

Moderator: Forum Moderators

Forum rules
  • Please use [code] BBCode tags in your posts for embedding WML snippets.
  • To keep your code readable so that others can easily help you, make sure to indent it following our conventions.
silene
Posts: 1109
Joined: August 28th, 2004, 10:02 pm

Support for Lua scripts in 1.7

Post by silene »

Now that Wesnoth 1.6 has been released, a new development cycle starts. With respect to WML, one of the new features of the 1.7 version is support for scripts written in Lua. For now, the trunk version only handles WML event actions, but this is sufficient to start playing with this new way of writing scenarios.

See http://www.wesnoth.org/wiki/LuaWML for a draft of the documentation.

Disclaimer: The Lua language will not help you avoid learning WML. It is not a language much more powerful than WML for designing scenarios either. But it should considerably help you if you have a hard time coding complicated events that require lots of control structures.

Now is the time for a small example. The following snippet is taken from a partial conversion of the tutorial to Lua.

Code: Select all

local args = ...
if target_hex.is_set and (args.x1 ~= target_hex.x or args.y1 ~= target_hex.y) then
    W.redraw()
    narrator_says(_ "*Oops!\nYou moved to the wrong place! After this message, you can press 'u' to undo, then try again.")
end
You can compare it with the WML version below.
Original WML version:
User avatar
DEATH_is_undead
Posts: 960
Joined: March 4th, 2007, 3:00 pm
Location: Northern United States

Re: Support for Lua scripts in 1.7

Post by DEATH_is_undead »

I feel stupid to ask, but...

The old version of WML will remain intact? Meaning you can use the old WML along with the LUA script?
3P MP Scenario - Great Dwarves Escape
The best way to learn is to follow. In order to learn WML, you have to follow other's work, and check their codes.
Max
Posts: 1449
Joined: April 13th, 2008, 12:41 am

Re: Support for Lua scripts in 1.7

Post by Max »

DEATH_is_undead wrote:The old version of WML will remain intact? Meaning you can use the old WML along with the LUA script?
it would be a bit difficult otherwise, since LUA is currently embedded within WML^^
User avatar
DEATH_is_undead
Posts: 960
Joined: March 4th, 2007, 3:00 pm
Location: Northern United States

Re: Support for Lua scripts in 1.7

Post by DEATH_is_undead »

Max2008 wrote:
DEATH_is_undead wrote:The old version of WML will remain intact? Meaning you can use the old WML along with the LUA script?
it would be a bit difficult otherwise, since LUA is currently embedded within WML^^
I know, but I'm asking is this a transition to this.
3P MP Scenario - Great Dwarves Escape
The best way to learn is to follow. In order to learn WML, you have to follow other's work, and check their codes.
silene
Posts: 1109
Joined: August 28th, 2004, 10:02 pm

Re: Support for Lua scripts in 1.7

Post by silene »

DEATH_is_undead wrote:The old version of WML will remain intact? Meaning you can use the old WML along with the LUA script?
There is nothing like an old version of WML. Lua support is just the addition of a new tag simply called "[lua]", all the other tags are left unchanged. This isn't a transition to anything.

The goal is to add support for a real programming language for the situations that are currently hard or impossible to solve with plain WML syntax. For instance, the "[lua]" tag makes it trivial for designers to have dynamic display of objectives in scenarios, while it is impossible (as far as I know) in standard WML (see the "[show_if]" related threads).
User avatar
Sapient
Inactive Developer
Posts: 4453
Joined: November 26th, 2005, 7:41 am
Contact:

Re: Support for Lua scripts in 1.7

Post by Sapient »

silene wrote: while it is impossible (as far as I know) in standard WML (see the "[show_if]" related threads).
For the record, it's not impossible to have dynamic objectives with WML. One must simply use the [set_variables] and [insert_tag] features to modify and then insert the desired objectives.

However, some authors find that method too complex and would prefer support for Conditional(s) that are built directly into the original structure.

... on an unrelated note: string manipulations (other than concatenation) and string comparisons (other than simple equality tests) are very difficult currently in WML. I think that you could make a strong case for the superiority of Lua in that area ...
http://www.wesnoth.org/wiki/User:Sapient... "Looks like your skills saved us again. Uh, well at least, they saved Soarin's apple pie."
silene
Posts: 1109
Joined: August 28th, 2004, 10:02 pm

Re: Support for Lua scripts in 1.7

Post by silene »

Update: It is now possible for Lua scripts to define custom WML tags. These new tags can then be used like any other WML action tags. This may be useful for times when firing specially crafted events would be too convoluted or using the preprocessor would be ugly.
Lord_Aether
Posts: 127
Joined: November 11th, 2005, 9:06 am
Location: California

Re: Support for Lua scripts in 1.7

Post by Lord_Aether »

silene wrote:Update: It is now possible for Lua scripts to define custom WML tags. These new tags can then be used like any other WML action tags. This may be useful for times when firing specially crafted events would be too convoluted or using the preprocessor would be ugly.
Something like custom abilities, using actual tags? That's pretty cool.
Mac OS X 10.5.7
Wesnoth 1.6.2
User avatar
appleide
Posts: 1003
Joined: November 8th, 2003, 10:03 pm
Location: Sydney,OZ

Re: Support for Lua scripts in 1.7

Post by appleide »

*doing the thing Mr. Burns does with his hands* excellent.....

Haven't been doing any WML whatsoever for a long time.... Time to take a look again now that Lua WML is even better equipped. =D
Why did the fish laugh? Because the sea weed.
User avatar
solsword
Code Contributor
Posts: 291
Joined: January 12th, 2009, 10:21 pm
Location: Santa Cruz, CA
Contact:

Re: Support for Lua scripts in 1.7

Post by solsword »

I sort of hate to bring this up, what with all the excitement going on about Lua in WML right now, but how safe is this? Could I put a Lua tag in my campaign that runs code that, for example, scans the user's computer for certain files and email them to my email address?

I'll admit that I've only just read the thread about Lua introduction on the closed dev forum and this thread here, and I haven't looked at the patch, but I didn't see this issue brought up, and I feel that it's a reasonably important one.

As far as I know, WML itself is pretty safe (although I haven't really tried to mess around with python AIs or the new WML custom functions or anything...), so the addition of Lua seems like something that could make it unsafe.

On the other hand, safety may or may not be a big issue for the community. I'm reasonably confident that the day that someone writes a WML-based virus is still far off, so perhaps it's okay that it's possible. It's also possible to police the add-on server to some degree if this becomes an issue.

But anyway, I do kinda like having Lua in WML, even though I'll probably try to avoid it as much as possible so that others can better understand my code.
The Knights of the Silver Spire campaign.

http://www.cs.hmc.edu/~pmawhorter - my website.

Teamcolors for everyone! PM me for a teamcolored version of your sprite, or you can do it yourself. If you just happen to like magenta, no hard feelings?
User avatar
ancestral
Inactive Developer
Posts: 1108
Joined: August 1st, 2006, 5:29 am
Location: Motion City

Re: Support for Lua scripts in 1.7

Post by ancestral »

It seems to me Lua code in here doesn't do file I/O.

Or does it?
Wesnoth BestiaryPREVIEW IT HERE )
Unit tree and stat browser
CanvasPREVIEW IT HERE )
Exp. map viewer
User avatar
solsword
Code Contributor
Posts: 291
Joined: January 12th, 2009, 10:21 pm
Location: Santa Cruz, CA
Contact:

Re: Support for Lua scripts in 1.7

Post by solsword »

Yeah, I just spent ~10 minutes looking at what's available at the Wesnoth command line and it seems that require isn't allowed, and of course the io library isn't there. So perhaps the Lua functionality that they have is already safe :) . Of course, one person spending 10 minutes doesn't mean anything; I'd like to hear some of the developers involved talk to this point. But it certainly at least "looks safe".
The Knights of the Silver Spire campaign.

http://www.cs.hmc.edu/~pmawhorter - my website.

Teamcolors for everyone! PM me for a teamcolored version of your sprite, or you can do it yourself. If you just happen to like magenta, no hard feelings?
silene
Posts: 1109
Joined: August 28th, 2004, 10:02 pm

Re: Support for Lua scripts in 1.7

Post by silene »

solsword wrote:So perhaps the Lua functionality that they have is already safe :) .
It is supposed to be safe. As a matter of fact, the following libraries are disabled: coroutine, debug, io, os, and package; and so are the functions general.dofile and general.loadfile. (Coroutines are not disabled for security reasons, it's just that I couldn't find a use for them right now; they may well be reenabled once the GUI exports some entry points.) Obviously, you can use Lua to cause infinite loops and things like that. But this is nothing new: it's already possible with pure WML.
User avatar
solsword
Code Contributor
Posts: 291
Joined: January 12th, 2009, 10:21 pm
Location: Santa Cruz, CA
Contact:

Re: Support for Lua scripts in 1.7

Post by solsword »

silene wrote:It is supposed to be safe.
Yay! I continue to be impressed by the knowledge, skill, and dedication of the Wesnoth team. Keep up the great work, guys 8) .
The Knights of the Silver Spire campaign.

http://www.cs.hmc.edu/~pmawhorter - my website.

Teamcolors for everyone! PM me for a teamcolored version of your sprite, or you can do it yourself. If you just happen to like magenta, no hard feelings?
User avatar
Crab
Inactive Developer
Posts: 200
Joined: March 18th, 2009, 9:42 pm

Re: Support for Lua scripts in 1.7

Post by Crab »

Since 1.7.14, we have basic support for writing parts of AI turn sequence using Lua.
see data/ai/scenarios/scenario-lua-ai.cfg and Customizing_AI_in_Wesnoth_1.8#Lua_AI_syntax
Post Reply