The Gnats Lua questions (creating an event using lua)

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

Moderator: Forum Moderators

User avatar
The_Gnat
Posts: 2215
Joined: October 10th, 2016, 3:06 am
Contact:

The Gnats Lua questions (creating an event using lua)

Post by The_Gnat »

I have now decided to write some lua code (as recommended by Tad_Carlucci) and i bet i will have a LOT of questions so i have created a new thread.

(please note i currently know almost nothing about lua, so unfortunately i will probably need examples to understand you, i apologize in advance)

Question 1. Currently i want to create (inside a start event) a moveto event using Lua.

Basically i am wondering if someone would provide a simple Lua moveto event that would go here:

Code: Select all

[event]
    name=start

   {VARIABLE counter 0}

   [while]
       [variable]
            name=counter
            not_equals=100
       [/variable]
    [do]

        [lua]
            code=<<
      
                    --A LUA EVENT IS CREATED

            >>
        [/lua]

        {VARIABLE_OP counter add 1}
    [/do]
   [/while]

[/event]
Thank you very much
gfgtdf
Developer
Posts: 1432
Joined: February 10th, 2013, 2:25 pm

Re: The Gnats Lua questions (creating an event using lua)

Post by gfgtdf »

there is a lua page https://wiki.wesnoth.org/LuaWML that explains how to use lua inside wml. Generally, lua can do anything that wml does by calling wesnoth.wml_actions.<tagname> for example

Code: Select all

[message]
  mesage="Hello"
  speaker="narrator"
[/message]
does the same as

Code: Select all

[lua]
code = <<
wesnoth.wml_actions.message({
  mesage="Hello",
  speaker="narrator",
})
>>
[/lua]
But often there is a better way, specially when it comes to programm flow (if/for/while etc.) you should't use te corresping wml tag when coding in lua. for example

Code: Select all


[event]
    name=start

   {VARIABLE counter 0}

   [while]
       [variable]
            name=counter
            not_equals=100
       [/variable]
    [do]

        [lua]
            code=<<
      
                    --YOUR LUA CODE

            >>
        [/lua]

        {VARIABLE_OP counter add 1}
    [/do]
   [/while]

[/event]

is quite silly, its better to write

Code: Select all


[event]
    name=start
        [lua]
            code=<<
                    for counter = 1, 100 do
                       --YOUR LUA CODE
                    end 
            >>
        [/lua]
[/event]

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
The_Gnat
Posts: 2215
Joined: October 10th, 2016, 3:06 am
Contact:

Re: The Gnats Lua questions (creating an event using lua)

Post by The_Gnat »

Thank you! So as i understand it i could write (as my lua event):

Code: Select all

[lua]
code = <<
local myXcoordinates wesnoth.get_variable(xLocations)
local myYcoordinates wesnoth.get_variable(yLocations)

wesnoth.wml_actions.event({
  name="moveto",
  filter , {
        x="myXcoordinates"
        y="myYcoordinates"
  }
})
>>
[/lua]
gfgtdf
Developer
Posts: 1432
Joined: February 10th, 2013, 2:25 pm

Re: The Gnats Lua questions (creating an event using lua)

Post by gfgtdf »

The idea is correct but the syntax is not, when creating wml table you in you have to encode them properly, usually using the T.<tagname> syntax, see https://wiki.wesnoth.org/LuaWML#Encodin ... Lua_tables

EDIT: and ideatd of x="myXcoordinates" you ahve to use x=myXcoordinates otherwise it will just put the myXcoordinates string there
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
The_Gnat
Posts: 2215
Joined: October 10th, 2016, 3:06 am
Contact:

Re: The Gnats Lua questions (creating an event using lua)

Post by The_Gnat »

Of course :D Thank you for the link! So this would solve my problem?

Code: Select all

[lua]
    code = <<
    local myXcoordinates wesnoth.get_variable(xLocations)
    local myYcoordinates wesnoth.get_variable(yLocations)

    wesnoth.wml_actions.event({
      name="moveto",
      { "filter" , {
            x=myXcoordinates,
            y=myYcoordinates
      }
    })
    >>
[/lua]

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

Re: The Gnats Lua questions (creating an event using lua)

Post by gfgtdf »

hmm those local myXcoordinates wesnoth.get_variable(xLocations) lines are still wrong, since 1) its syntaticalla uncorrect, 2) get_variable expects a string
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
The_Gnat
Posts: 2215
Joined: October 10th, 2016, 3:06 am
Contact:

Re: The Gnats Lua questions (creating an event using lua)

Post by The_Gnat »

Does adding a = fix 1). And :doh: So what exactly should i do about 2) in order to get an integer? Can i load a wml variable directly into a lua event?
User avatar
ForestDragon
Posts: 1769
Joined: March 6th, 2014, 1:32 pm
Location: Ukraine

Re: The Gnats Lua questions (creating an event using lua)

Post by ForestDragon »

The_Gnat wrote:Does adding a = fix 1). And :doh: So what exactly should i do about 2) in order to get an integer? Can i load a wml variable directly into a lua event?
ummm, i think he meant a STRING, not an A string :lol:
My active add-ons: The Great Steppe Era,XP Bank,Alliances Mod,Pestilence,GSE+EoMa,Ogre Crusaders,Battle Royale,EoMaifier,Steppeifier,Hardcoreifier
My inactive add-ons (1.12): Tale of Alan, The Golden Age
Co-creator of Era of Magic
gfgtdf
Developer
Posts: 1432
Joined: February 10th, 2013, 2:25 pm

Re: The Gnats Lua questions (creating an event using lua)

Post by gfgtdf »

The_Gnat wrote:Does adding a = fix 1).
The_Gnat wrote: And :doh: So what exactly should i do about 2) in order to get an integer? Can i load a wml variable directly into a lua event?
the problen is that your coide us3es the undeclares local variable xLocations
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
The_Gnat
Posts: 2215
Joined: October 10th, 2016, 3:06 am
Contact:

Re: The Gnats Lua questions (creating an event using lua)

Post by The_Gnat »

gfgtdf wrote:the problen is that your coide us3es the undeclares local variable xLocations
Thank you, yes i will declare the variable using a {VARIABLE xLocations 10}.

Has anyone written any scenarios or events in Lua that i can look at in order to help me write the syntax correctly?
gfgtdf
Developer
Posts: 1432
Joined: February 10th, 2013, 2:25 pm

Re: The Gnats Lua questions (creating an event using lua)

Post by gfgtdf »

The_Gnat wrote:
gfgtdf wrote:the problen is that your coide us3es the undeclares local variable xLocations
Thank you, yes i will declare the variable using a {VARIABLE xLocations 10}.
No that would be a wml variable, not a lua varible... , maybe you shodul read some (wesnoth-unrelated) lua tutorial first.
The_Gnat wrote:
Has anyone written any scenarios or events in Lua that i can look at in order to help me write the syntax correctly?
There are many lua codes, for example in wesnoth 1.13 the mainline scenario "2p Dark Forecast" ist written nearly complateley in lua, see https://github.com/wesnoth/wesnoth/blob ... recast.lua (loaded here https://github.com/wesnoth/wesnoth/blob ... st.cfg#L82) although it might not work as-is on wesnoth 1.12
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
The_Gnat
Posts: 2215
Joined: October 10th, 2016, 3:06 am
Contact:

Re: The Gnats Lua questions (creating an event using lua)

Post by The_Gnat »

Thank you i will have a look at that! :D
User avatar
The_Gnat
Posts: 2215
Joined: October 10th, 2016, 3:06 am
Contact:

Re: The Gnats Lua questions (creating an event using lua)

Post by The_Gnat »

Here i am again wrestling with LUA ;) And i am wondering if this works (syntaxically)?

Code: Select all

        [lua]
            code=<<
		    local victoriousSide wesnoth.get_variable($the_killer.side)

                    for counter = 1, 3 do

			if counter != victoriousSide then
				wesnoth.sides[wesnoth.current.side].defeat_condition = "always"
			end

                    end
            >>
        [/lua]
User avatar
Ravana
Forum Moderator
Posts: 2949
Joined: January 29th, 2012, 12:49 am
Location: Estonia
Contact:

Re: The Gnats Lua questions (creating an event using lua)

Post by Ravana »

No.

local victoriousSide =

$ wont work. For scalar variables it just needs quoted variable name, not sure how it works with objects.

I believe Lua does not use !=, rather ~=
User avatar
The_Gnat
Posts: 2215
Joined: October 10th, 2016, 3:06 am
Contact:

Re: The Gnats Lua questions (creating an event using lua)

Post by The_Gnat »

Thank you! (this shows how little i know about lua) :D
Post Reply