Support for Lua scripts in 1.7
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.
Support for Lua scripts in 1.7
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.
You can compare it with the WML version below.
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
Original WML version:
- DEATH_is_undead
- Posts: 960
- Joined: March 4th, 2007, 3:00 pm
- Location: Northern United States
Re: Support for Lua scripts in 1.7
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?
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.
The best way to learn is to follow. In order to learn WML, you have to follow other's work, and check their codes.
Re: Support for Lua scripts in 1.7
it would be a bit difficult otherwise, since LUA is currently embedded within WML^^DEATH_is_undead wrote:The old version of WML will remain intact? Meaning you can use the old WML along with the LUA script?
- DEATH_is_undead
- Posts: 960
- Joined: March 4th, 2007, 3:00 pm
- Location: Northern United States
Re: Support for Lua scripts in 1.7
I know, but I'm asking is this a transition to this.Max2008 wrote:it would be a bit difficult otherwise, since LUA is currently embedded within WML^^DEATH_is_undead wrote: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.
The best way to learn is to follow. In order to learn WML, you have to follow other's work, and check their codes.
Re: Support for Lua scripts in 1.7
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.DEATH_is_undead wrote:The old version of WML will remain intact? Meaning you can use the old WML along with the LUA script?
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).
Re: Support for Lua scripts in 1.7
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.silene wrote: while it is impossible (as far as I know) in standard WML (see the "[show_if]" related threads).
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."
Re: Support for Lua scripts in 1.7
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.
-
- Posts: 127
- Joined: November 11th, 2005, 9:06 am
- Location: California
Re: Support for Lua scripts in 1.7
Something like custom abilities, using actual tags? That's pretty cool.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.
Mac OS X 10.5.7
Wesnoth 1.6.2
Wesnoth 1.6.2
Re: Support for Lua scripts in 1.7
*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
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.
- 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
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.
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?
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?
Re: Support for Lua scripts in 1.7
It seems to me Lua code in here doesn't do file I/O.
Or does it?
Or does it?
Wesnoth Bestiary ( PREVIEW IT HERE )
Unit tree and stat browser
Canvas ( PREVIEW IT HERE )
Exp. map viewer
Unit tree and stat browser
Canvas ( PREVIEW IT HERE )
Exp. map viewer
- 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
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?
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?
Re: Support for Lua scripts in 1.7
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.solsword wrote:So perhaps the Lua functionality that they have is already safe .
- 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
Yay! I continue to be impressed by the knowledge, skill, and dedication of the Wesnoth team. Keep up the great work, guys .silene wrote:It is supposed to be 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?
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?
Re: Support for Lua scripts in 1.7
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
see data/ai/scenarios/scenario-lua-ai.cfg and Customizing_AI_in_Wesnoth_1.8#Lua_AI_syntax