The Gnats Lua questions (creating an event using lua)

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

Moderators: Forum Moderators, Developers

The Gnats Lua questions (creating an event using lua)

Postby The_Gnat » January 19th, 2017, 10:42 pm

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

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

Postby gfgtdf » January 19th, 2017, 11:15 pm

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.
gfgtdf
Developer
 
Posts: 843
Joined: February 10th, 2013, 2:25 pm

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

Postby The_Gnat » January 19th, 2017, 11:22 pm

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]
User avatar
The_Gnat
 
Posts: 996
Joined: October 10th, 2016, 3:06 am

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

Postby gfgtdf » January 19th, 2017, 11:24 pm

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.
gfgtdf
Developer
 
Posts: 843
Joined: February 10th, 2013, 2:25 pm

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

Postby The_Gnat » January 19th, 2017, 11:33 pm

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]

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

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

Postby gfgtdf » January 20th, 2017, 12:11 am

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.
gfgtdf
Developer
 
Posts: 843
Joined: February 10th, 2013, 2:25 pm

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

Postby The_Gnat » January 20th, 2017, 12:33 am

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

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

Postby ForestDragon » January 20th, 2017, 11:17 am

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:
maintainer of add-ons made by inferno8: Era of Magic, and an extremely visually beautiful campaign: To Lands Unknown
Creator of Tale of Alan (abandoned) and The Golden Age (my era, also rather abandoned),XP Bank
"Join the code side, we have COOKIES!!!" :)
User avatar
ForestDragon
 
Posts: 1106
Joined: March 6th, 2014, 1:32 pm

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

Postby gfgtdf » January 21st, 2017, 3:40 pm

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.
gfgtdf
Developer
 
Posts: 843
Joined: February 10th, 2013, 2:25 pm

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

Postby The_Gnat » January 21st, 2017, 10:44 pm

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?
User avatar
The_Gnat
 
Posts: 996
Joined: October 10th, 2016, 3:06 am

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

Postby gfgtdf » January 21st, 2017, 10:55 pm

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.
gfgtdf
Developer
 
Posts: 843
Joined: February 10th, 2013, 2:25 pm


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

Postby The_Gnat » May 5th, 2017, 10:03 pm

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

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

Postby Ravana » May 5th, 2017, 10:11 pm

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
Ravana
Moderator
 
Posts: 1381
Joined: January 29th, 2012, 12:49 am
Location: Estonia


Next

Return to Lua Labs

Who is online

Users browsing this forum: No registered users and 2 guests