LUA Resources, Help and a tiny Beginners Guide by enclave
Moderator: Forum Moderators
LUA Resources, Help and a tiny Beginners Guide 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):
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.
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:
then I create the any name cfg (for example lua_preload.cfg) with LUA_PRELOAD_HELPER macro in it like so:
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..
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]
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 {}
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
Ok lets go..
Re: LUA Resources, Help and a tiny Beginners Guide by enclav
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.
<< and >> are used to exclude text from preprocessing. For some statements you might not want to use them.
Re: LUA Resources, Help and a tiny Beginners Guide by enclav
A basic thing like writing comments in lua..
There are two ways known to me.. (I believe they work, correct me if im wrong)
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..
If you like to change only units that have your custom status, then the code below is untested, but I assume it could work:
to change status of the unit to some custom status with name "ranger" use either
Alternatively to image you could have colored text in your unit status..
Now if you would like to replace IMAGE of the unit in right hand side display thing..
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..
There are two ways known to me.. (I believe they work, correct me if im wrong)
Spoiler:
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:
Spoiler:
Spoiler:
Spoiler:
Spoiler:
Thanks for reading, hope it helped somebody.. it definitly helped me.. as I haven't tried all this stuff before starting this post..
Re: LUA Resources, Help and a tiny Beginners Guide by enclav
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..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.
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] ??
Re: LUA Resources, Help and a tiny Beginners Guide by enclav
Simple statements can work without <<>> as well, like https://github.com/ProditorMagnus/Oroci ... ves.cfg#L7
Re: LUA Resources, Help and a tiny Beginners Guide by enclav
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.
Re: LUA Resources, Help and a tiny Beginners Guide by enclav
A message with options.. And concatenation of strings.. span color.. etc
1) The message with options:
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..
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()
1) The message with options:
Spoiler:
Spoiler:
To start text from new line you use "\n"
to convert integer into string would use tostring()
Re: LUA Resources, Help and a tiny Beginners Guide by enclav
So you mean I can write:Ravana wrote:Simple statements can work without <<>> as well, like https://github.com/ProditorMagnus/Oroci ... ves.cfg#L7
Code: Select all
[lua]
code=randomUnit({LEVEL},{X},{Y},1)
[/lua]
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.
Re: LUA Resources, Help and a tiny Beginners Guide by enclav
I believe to define custom wml tag you would do something like this (just a cut of a zookeeper diplomacy era code):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.
Spoiler:
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
Code: Select all
[freeze_unit]
id=Delfador
[/freeze_unit]
Re: LUA Resources, Help and a tiny Beginners Guide by enclav
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
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).
With following code you could replace all unit weapons (attacks) with your custom things.. defined in unit.variables
Spoiler:
Re: LUA Resources, Help and a tiny Beginners Guide by enclav
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.
Re: LUA Resources, Help and a tiny Beginners Guide by enclav
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!gfgtdf wrote:I know how to define custom wml tags, my message was an answer to ravannas code example.
Re: LUA Resources, Help and a tiny Beginners Guide by enclav
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.
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.
Re: LUA Resources, Help and a tiny Beginners Guide by enclav
the wesnoth theme_items function returns wml tables encoded in lua as documentien in the main LuaWML wiki page,
so this code
will make the unit staus report in sidebar always have this wml value:
of course you can return a wml table with multiple subtags:
returns
Usually you want to add an element instead of just overwriting the retun value so you do:
returns
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
Code: Select all
[element]
image="items/archery-target-right.png~CROP(25,28,25,28)~SCALE(25,25)"
[/element]
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
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]
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
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.
Re: LUA Resources, Help and a tiny Beginners Guide by enclav
good idea... will edit my first post after.. thanks!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.
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
Code: Select all
if result_string == wesnoth.get_variable("side_number") then
wesnoth.message("lua_message","Current side number is 2")
end
Code: Select all
if result_string ~= wesnoth.get_variable("side_number") then
wesnoth.message("lua_message","Current side number is NOT 2")
end
Code: Select all
if not result_string == wesnoth.get_variable("side_number") then
wesnoth.message("lua_message","Current side number is NOT 2")
end
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
Code: Select all
if tostring(some_number) == wesnoth.get_variable("side_number") then
wesnoth.message("lua_message","Current side number is 2")
end
Last edited by enclave on October 21st, 2016, 10:40 pm, edited 3 times in total.