LUA Resources, Help and a tiny Beginners Guide by enclave

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

Moderator: Forum Moderators

enclave
Posts: 936
Joined: December 15th, 2007, 8:52 am

LUA Resources, Help and a tiny Beginners Guide by enclave

Post by enclave »

Hi all, I was messing around with some lua today.. User Interface to be more precise..
It's quite a nightmare to be honest.. so I thought every little drop of information helps.. in this area..
So I will be putting here my little drops of successful codes.. even if they are tiny..

First of all, if you are starting lua, you need to add the lua helper code somewhere in preload event (it might be possible that you can add it somewhere else, for example in main.cfg, but I'm not sure.. I'm a big lua noob!!!! keep it in mind when reading me.. all I want from my resources is to show you the codes that did work for me.. and therefore might work for you.. since wesnoth lua help is quite miserable and I hardly can even make the shown examples to work. So I hope I can save some time to somebody by displaying my totally noobish but working codes.)

I assume you already know WML and how add-ons work.. so I won't be much informative on that subject.. If you don't know how WML or add-ons work, just better don't read further.. it will make no sense. Save your time..
So in short.. when you begin lua, you need to either
1) find your main.cfg of your add-on and add there a code like this (if you want to write purely in lua I guess):

Code: Select all

[lua]
    code = "wesnoth.dofile '~add-ons/Your_Fabulous_Addon/macros/your_first.lua'"
[/lua]
just before the #endif should work fine..
Then create file in "Your_Fabulous_Addon" , "macros" folder.. with "your_first.lua" name..
And start this file with the following code to avoid any problems.. otherwise something may just not work.

Code: Select all

helper = wesnoth.require "lua/helper.lua"
T = helper.set_wml_tag_metatable {}
2) If you want to mainly write in WML like me and only sometimes in lua.. then what I do is just not use any ".lua" extensions.. but use .cfg
so in my .cfg that contains [era] i put the preload event like this:

Code: Select all

[event]
	name=preload
	{LUA_PRELOAD_HELPER}
[/event]

then I create the any name cfg (for example lua_preload.cfg) with LUA_PRELOAD_HELPER macro in it like so:

Code: Select all

#define LUA_PRELOAD_HELPER
[lua] 
code = <<
helper = wesnoth.require "lua/helper.lua"
T = helper.set_wml_tag_metatable {}
>>
[/lua]
#enddef
And then anytime I need to switch to lua, I just add [lua] code =<< --your lua code here >>[/lua] to my cfgs.. and not bothered about anything else..

Ok lets go..
User avatar
Ravana
Forum Moderator
Posts: 2952
Joined: January 29th, 2012, 12:49 am
Location: Estonia
Contact:

Re: LUA Resources, Help and a tiny Beginners Guide by enclav

Post by Ravana »

dofile asks for file from each player separately, so everyone in game should have it.

<< and >> are used to exclude text from preprocessing. For some statements you might not want to use them.
enclave
Posts: 936
Joined: December 15th, 2007, 8:52 am

Re: LUA Resources, Help and a tiny Beginners Guide by enclav

Post by enclave »

A basic thing like writing comments in lua..
There are two ways known to me.. (I believe they work, correct me if im wrong)
Spoiler:
This is best used if you want to copy your whole function, change something, try it.. and would not need to put something like # before every single line.. just take it in brackets instead.. simple.

Now something real.. A bit of user interface.. How to change status of the unit (only what is displayed.. its NOT real, its fake, it does NOT do anything, I believe, it is only for graphical reasons.. if you would like to add some colors to your right hand side panel..)
I assume you would need to put the following code in your preload, start or prestart event.. I experiment with it putting it in [event] name=select so that I could see the difference before I click the unit.. and after..
Spoiler:
If you like to change only units that have your custom status, then the code below is untested, but I assume it could work:
Spoiler:
to change status of the unit to some custom status with name "ranger" use either
Spoiler:
Alternatively to image you could have colored text in your unit status..
Spoiler:
Now if you would like to replace IMAGE of the unit in right hand side display thing..
Spoiler:
I still don't know how to use insert.table command.. but eventually I will figure it out and post something nice here..

Thanks for reading, hope it helped somebody.. it definitly helped me.. as I haven't tried all this stuff before starting this post..
enclave
Posts: 936
Joined: December 15th, 2007, 8:52 am

Re: LUA Resources, Help and a tiny Beginners Guide by enclav

Post by enclave »

Ravana wrote:dofile asks for file from each player separately, so everyone in game should have it.

<< and >> are used to exclude text from preprocessing. For some statements you might not want to use them.
regarding dofile, thanks very much, its very important for me.. however I didn't have any problems when tried it in multiplayer.. but I will have a closer eye on it from now on.. and if enything goes wrong I will know where troubles come from..
I think in my case these dofile luas were never used, maybe that's why was no problems.. Thanks again!

didn't quite understand about <<>>, I thought it doesn't work without them?? You mean I can just use [lua] code = [/lua] ??
User avatar
Ravana
Forum Moderator
Posts: 2952
Joined: January 29th, 2012, 12:49 am
Location: Estonia
Contact:

Re: LUA Resources, Help and a tiny Beginners Guide by enclav

Post by Ravana »

Simple statements can work without <<>> as well, like https://github.com/ProditorMagnus/Oroci ... ves.cfg#L7
gfgtdf
Developer
Posts: 1432
Joined: February 10th, 2013, 2:25 pm

Re: LUA Resources, Help and a tiny Beginners Guide by enclav

Post by gfgtdf »

There is rareley a reason not to use << >> for lua code, in the example above it would've most likeley been easier to define a custom wml tag [orcia_random_unit] in lua.
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.
enclave
Posts: 936
Joined: December 15th, 2007, 8:52 am

Re: LUA Resources, Help and a tiny Beginners Guide by enclav

Post by enclave »

A message with options.. And concatenation of strings.. span color.. etc
1) The message with options:
Spoiler:
2) Lua is very intolerant to type mismatches.. so you need to make sure string is string.. integer is integer.. or convert them to what is required.. And it could be problematic for beginner to form a string from couple of variables..
Spoiler:
To concatenate string in Lua you would use double dots ".." not "+" or space or anything..
To start text from new line you use "\n"
to convert integer into string would use tostring()
enclave
Posts: 936
Joined: December 15th, 2007, 8:52 am

Re: LUA Resources, Help and a tiny Beginners Guide by enclav

Post by enclave »

Ravana wrote:Simple statements can work without <<>> as well, like https://github.com/ProditorMagnus/Oroci ... ves.cfg#L7
So you mean I can write:

Code: Select all

[lua]
		code=randomUnit({LEVEL},{X},{Y},1)
[/lua]
But I need << >> if I would want to write it in 2 lines:

Code: Select all

[lua]
		code= <<
randomUnit1({LEVEL},{X},{Y},1)
randomUnit2({LEVEL},{X},{Y},2)
>>
[/lua]
?

I got a reply saying that if I don't use <<>> then I am able to use macro values in lua.. so in between [lua] tags I could still use {X} from a macro... if I would use <<>> then {X} would literarily be {X} (and not some value behind it).

By the way could you tell me what a thing like this exactly means?? Is it an array??

Code: Select all

local icecream = {}
Last edited by enclave on October 14th, 2016, 6:41 pm, edited 1 time in total.
enclave
Posts: 936
Joined: December 15th, 2007, 8:52 am

Re: LUA Resources, Help and a tiny Beginners Guide by enclav

Post by enclave »

gfgtdf wrote:There is rareley a reason not to use << >> for lua code, in the example above it would've most likeley been easier to define a custom wml tag [orcia_random_unit] in lua.
I believe to define custom wml tag you would do something like this (just a cut of a zookeeper diplomacy era code):
Spoiler:
But a simple example can be found here https://wiki.wesnoth.org/LuaWML/Events# ... ml_actions
LUA:

Code: Select all

function wesnoth.wml_actions.freeze_unit(cfg)
    local unit_id = cfg.id or helper.wml_error "[freeze_unit] expects an id= attribute."
    helper.modify_unit({ id = unit_id }, { moves = 0 })
end
WML:

Code: Select all

[freeze_unit]
    id=Delfador
[/freeze_unit]
enclave
Posts: 936
Joined: December 15th, 2007, 8:52 am

Re: LUA Resources, Help and a tiny Beginners Guide by enclav

Post by enclave »

Back to user interface and wesnoth.theme_items
With following code you could replace all unit weapons (attacks) with your custom things.. defined in unit.variables
Spoiler:
I just don't think this way you would be able to get proper tooltips for every weapon (not in my example).. to make it properly you would probably need to use insert.table (but I have no idea how, couldn't get it to work yet).
gfgtdf
Developer
Posts: 1432
Joined: February 10th, 2013, 2:25 pm

Re: LUA Resources, Help and a tiny Beginners Guide by enclav

Post by gfgtdf »

I know how to define custom wml tags, my message was an answer to ravannas code example.
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.
enclave
Posts: 936
Joined: December 15th, 2007, 8:52 am

Re: LUA Resources, Help and a tiny Beginners Guide by enclav

Post by enclave »

gfgtdf wrote:I know how to define custom wml tags, my message was an answer to ravannas code example.
from your message it was very clear that you knew how to define custom wml tags but this topic of mine is a resource for beginners, people who potentially don't know much about lua. So I wrote an explanation to what you meant with custom wml tags as otherwise it would be an off-topic.. and my current message is also an off-topic. So I will later edit it with some useful code. Thanks very much for your replies, most of the time they are very useful and contain useful information!
User avatar
Ravana
Forum Moderator
Posts: 2952
Joined: January 29th, 2012, 12:49 am
Location: Estonia
Contact:

Re: LUA Resources, Help and a tiny Beginners Guide by enclav

Post by Ravana »

Well yes, back when I did that, I did not know how to define tags either.

For improvement of this topic: I suggest having first post as short descriptions + links to other posts here, currently its difficult to see what here is.
Or using [section]s.
gfgtdf
Developer
Posts: 1432
Joined: February 10th, 2013, 2:25 pm

Re: LUA Resources, Help and a tiny Beginners Guide by enclav

Post by gfgtdf »

the wesnoth theme_items function returns wml tables encoded in lua as documentien in the main LuaWML wiki page,
so this code

Code: Select all

<<
wesnoth.theme_items.unit_status = function()
  return {
    T.element {
      image="items/archery-target-right.png~CROP(25,28,25,28)~SCALE(25,25)"
    }
  }
end
will make the unit staus report in sidebar always have this wml value:

Code: Select all

[element]
  image="items/archery-target-right.png~CROP(25,28,25,28)~SCALE(25,25)"
[/element]
of course you can return a wml table with multiple subtags:

Code: Select all

<<
wesnoth.theme_items.unit_status = function()
  return {
    T.element {
      image="items/archery-target-right.png~CROP(25,28,25,28)~SCALE(25,25)"
    },
    T.element {
      image="items/archery-target-left.png~CROP(25,28,25,28)~SCALE(25,25)"
    }
  }
end
returns

Code: Select all

[element]
  image="items/archery-target-right.png~CROP(25,28,25,28)~SCALE(25,25)"
[/element]
[element]
  image="items/archery-target-left.png~CROP(25,28,25,28)~SCALE(25,25)"
[/element]
Usually you want to add an element instead of just overwriting the retun value so you do:

Code: Select all

<<
local unit_status_old = wesnoth.theme_items.unit_status
wesnoth.theme_items.unit_status = function()
  --oldvalue is the original wml document describing the unit staus
  local oldvalue = unit_status_old()
  -- create a new [elment] tag
  local newtag = T.element { image="items/archery-target-right.png~CROP(25,28,25,28)~SCALE(25,25)" }
  --add the new tag to the status wml document
  table.insert(oldvalue, newtag )
  --return it.
  return oldvalue
end
returns

Code: Select all

... orginal staus ...
[element]
  image="items/archery-target-right.png~CROP(25,28,25,28)~SCALE(25,25)"
[/element]
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.
enclave
Posts: 936
Joined: December 15th, 2007, 8:52 am

Re: LUA Resources, Help and a tiny Beginners Guide by enclav

Post by enclave »

Ravana wrote: For improvement of this topic: I suggest having first post as short descriptions + links to other posts here, currently its difficult to see what here is.
Or using [section]s.
good idea... will edit my first post after.. thanks!

As for now I would like to show example of replacing parts of strings with other strings..
for example we have a string containing the following text <span foreground="#777777">2</span> and we want to work only with number inside this string "2", what could we do?
so we create a string named original_string and assign the text above to it:
local original_string = '<span foreground="#777777">2</span>'
then we are replacing the parts of string with "" (empty brackets)(or in other words with nothing) by using string.gsub (https://www.lua.org/pil/2.4.html) function and saving it into modified_string like so:
local modified_string = string.gsub(original_string, '<span foreground="#777777">', "")
so at this point of time our string turned into "2</span>" and is stored in modified_string lua string variable
so we need to remove "</span>" as well to be able to work with the number.. so we doing same thing again like so:
local result_string = string.gsub(modified_string, "</span>", "")
at this point of time we have result_string which is equal to "2" but it is not a number..
to be able to compare this string to number in lua we would need to use tonumber function like so:
local some_number = tonumber(result_string)
now we could use if then comparison to a number.. for example

Code: Select all

if some_number > 5 then
wesnoth.message("lua_message","The number is greater than 5")
end
Or if we wanted to see if this number is equal to some WML variable, we would need to work with string like so:

Code: Select all

if result_string == wesnoth.get_variable("side_number") then
wesnoth.message("lua_message","Current side number is 2")
end
Or I believe there are 2 ways to check if string not equals and both should probably have same meaning:

Code: Select all

if result_string ~= wesnoth.get_variable("side_number") then
wesnoth.message("lua_message","Current side number is NOT 2")
end
or

Code: Select all

if not result_string == wesnoth.get_variable("side_number") then
wesnoth.message("lua_message","Current side number is NOT 2")
end
however I may be wrong :D but shouldn't really be..
Now to compare wesnoth variable number to some number you would need to use tonumber like so:

Code: Select all

if some_number > tonumber(wesnoth.get_variable("side_number")) then
wesnoth.message("lua_message","The number is greater than current side number")
end
The opposite of tonumber is tostring.. you may turn a number into string like so:

Code: Select all

if tostring(some_number) == wesnoth.get_variable("side_number") then
wesnoth.message("lua_message","Current side number is 2")
end
More information about working with strings: http://wiki.roblox.com/index.php?title= ... nipulation
Last edited by enclave on October 21st, 2016, 10:40 pm, edited 3 times in total.
Post Reply