Lua question

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

Moderators: Forum Moderators, Developers

Post Reply
User avatar
Captain_Wrathbow
Posts: 1664
Joined: June 30th, 2009, 2:03 pm
Location: Guardia

Lua question

Post by Captain_Wrathbow » September 7th, 2010, 10:17 pm

The following code is taken from the 1.7 addon "Wesnoth Lua Pack", published by Melinath. In case you don't know, this was a collection of useful snippets of lua code for people to use.
code:
I want to be able to use these two tags in my campaign, but unfortunately I don't know the first thing about lua, how it works, syntax, or how to embed it in my WML. I'm really not that interested in learning lua right now, (although I's like to get around to it sometime) I just want to be able to use this code. What do I have to do to make those two tags usable in my campaign? Do I have to put that code in a certain place in my campaign's files? Do I need to put it within some other WML tags? Do I need to add anything to my _main.cfg file?
Forgive my ignorance, but I know absolutely nothing about lua... any help is appreciated. Thanks. :)

User avatar
Dixie
Posts: 1755
Joined: February 10th, 2010, 1:06 am
Location: $x1,$y1

Re: Lua question

Post by Dixie » September 7th, 2010, 10:27 pm

You need to place them in a preload event, like this:

Code: Select all

[event]
  name=preload
  first_time_only=no
  [lua]
    code=<<

        -- The lua snippets here.

    >>
  [/lua]
[/event]
Of course, the event must be in a place that will launch anywhere the new tags are used. So, an [era], or maybe in all your scenarios (if it's a campaign), or an ever-present [unit_type]. You can also make a macro out of the whole event so it's easier to call in all scenarios or something. Anyway, I think you know how that works at that point :)
Jazz is not dead, it just smells funny - Frank Zappa
Current projects: Internet meme Era, The Settlers of Wesnoth

User avatar
Captain_Wrathbow
Posts: 1664
Joined: June 30th, 2009, 2:03 pm
Location: Guardia

Re: Lua question

Post by Captain_Wrathbow » September 7th, 2010, 10:50 pm

And then I can use [store_shroud] and [set_shroud] as if they were normal WML tags?
Huh, that's more simple than I was expecting... :o
Neat, thanks! :D


Also, just out of curiosity, why does it have to be first_time_only=no? If it's "preload", wouldn't it only happen once no matter what? :hmm:

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

Re: Lua question

Post by Pentarctagon » September 7th, 2010, 10:59 pm

wiki wrote:Note: Unlike prestart and start, the preload event must be able to fire more than once! This is because it is triggered each time a savegame is loaded in addition to the initial time when it loads before the scenario 'prestart'. This means that it is effectively mandatory to have the first_time_only=no key value in a preload event.
taken from EventWML.
99 little bugs in the code, 99 little bugs
take one down, patch it around
-2,147,483,648 little bugs in the code

Anonymissimus
Inactive Developer
Posts: 2458
Joined: August 15th, 2008, 8:46 pm
Location: Germany

Re: Lua question

Post by Anonymissimus » September 7th, 2010, 11:16 pm

A better strategy would be to put the above lua code in a file named (e.g.) wml_tags.lua and to load it like this (untested):

Code: Select all

[event]
  name=preload
  first_time_only=no
  [lua]
    code=<<
    helper = wesnoth.require("lua/helper.lua")--you'll also need to load this since those lua functions use it
    wesnoth.dofile("~add-ons/Your_Addon_Directory_Name/lua/wml_tags.lua")
    >>
  [/lua]
[/event]
This has the (absolutely fantastic!!!) advantage that the lua code is not loaded during wml preprocessing time meaning that when you (may) want to start editing lua you don't need to go back to the mainscreen to test changes and a lot more goodies. (syntax highlight, wesnoth-independant stack traceback,...)
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
Captain_Wrathbow
Posts: 1664
Joined: June 30th, 2009, 2:03 pm
Location: Guardia

Re: Lua question

Post by Captain_Wrathbow » September 7th, 2010, 11:38 pm

@Pentarctagon: Ah ha. I see.

@Anonymissimus: Interesting. I'll have to try that too.

Thanks, everyone! :)

Post Reply