Lua newbie custom tag query
Moderator: Forum Moderators
- Spannerbag
- Posts: 549
- Joined: December 18th, 2016, 6:14 pm
- Location: Yes
Lua newbie custom tag query
Hi,
I'm trying to learn some basic lua by creating a custom tag but am finding clear information regarding very basic language elements difficult to find (but then my google-fu is terrible) and after perusing the web I now have more questions than ever, hence this post.
Lua syntax
... OK, on to my problem ...
What I am trying to do
Define a custom WML tag, mainly to learn lua basics but also to slightly reduce bloat as I presently use this logic a lot as a macro.
This tag, arbitrarily called
If
If
then the value of the amount key is the minimum gold the side(s) specified can have.
Whenever I run the script I get the on screen error:
I have embedded the actual code in the scenario for now rater than using
I asume the
Here's (one version of) the actual lua in the scenario with the WML below (I have used nested
Line 4 would seem to be
Weirdly (to me at least) if I change the equality test to:
then the error is on line 5? (
Reverting back to
I get the same error whether
I have spent a fair bit of time on this and would like to ask what I am doing wrong? (and there's probably more than one answer!) as I seem to be going round in circles...
Aside
Oh, and an exampe of how to code a numbered list in this forum using the
Note: posted regarding this some time ago on development-ideas.
Any thoughts/comments greatly appreciated!
Cheers!
-- Spannerbag
I'm trying to learn some basic lua by creating a custom tag but am finding clear information regarding very basic language elements difficult to find (but then my google-fu is terrible) and after perusing the web I now have more questions than ever, hence this post.
Lua syntax
- Can someone please clarify the difference (if any) between assigning
var ::== something
vs.var = something
as the manual seems to swap between them without explanantion (to be fair I have not read the entire manual as it's huge).
- How do you structure conditional logic? Again, I found the documentation regarding
or
and()
consufing.
Isif ((var == value1) or (var == value2))
a valid syntax?
... OK, on to my problem ...
What I am trying to do
Define a custom WML tag, mainly to learn lua basics but also to slightly reduce bloat as I presently use this logic a lot as a macro.
This tag, arbitrarily called
[set_gold]
, is the same as [gold]
except it has another optional key: set
.If
set
is absent or false then [set_gold]
should behave identically to [gold]
- ideally I would like to call [gold]
from my lua code rather than replicate [gold]
's logic, but have not got that far yet.If
set
is true (specifically at present ==true
or ==yes
but will eventually test for is_false
rather than not_true
maybe)then the value of the amount key is the minimum gold the side(s) specified can have.
Whenever I run the script I get the on screen error:
Code: Select all
<Lua error> [string " function wesnoth.wml_actions.set_gold(cfg)..."]:4: variable 'yes' must be assigned before being used
stack traceback:
[C]: in function '.error'
lua/ilua.lua:131: in metamethod '__index'
[string " function wesnoth.wml_actions.set_gold(cfg)..."]:4: in local 'cmd'
lua/wml-utils.lua:145: in field 'handle_event_commands'
lua/wml-flow.lua:6: in function <lua/wml-flow.lua:5>
[load_resource]
in [campaign]
.I asume the
:4:
in [string " function wesnoth.wml_actions.set_gold(cfg)..."]:4: variable 'yes' must be assigned before being used
refers to a line number?Here's (one version of) the actual lua in the scenario with the WML below (I have used nested
if... thens...
to try and avoid conditional syntax errors):Code: Select all
[lua]
code = << function wesnoth.wml_actions.set_gold(cfg)
local set_amount = tonumber(cfg.amount) or wml.error "[set_gold] missing required amount= attribute."
local action_set = 0 -- Should include set omitted (=nil)
if cfg.set == yes or cfg.set == true then action_set = 1 end
for index, set_sides in ipairs(set_sides) do
set_sides[index].gold = set_sides[index].gold + set_amount
if action_set == 1 then -- Avoid conditional statements for now while debugging
if set_sides[index].gold < set_amount then set_sides[index].gold = set_amount end
end
end
end >>
[/lua]
# Turn 1
[event]
name=turn 1
{MSG ("-1- About to [set_gold] side=1, amount=33 set=no, so side 1 should have 133 gold on NORMAL")}
[set_gold]
side=1
amount=33
[/set_gold]
{MSG ("Done")}
[/event]
if cfg.set == yes or cfg.set == true then action_set = 1 end
so the equality test would seem to be the issue?Weirdly (to me at least) if I change the equality test to:
if ((cfg.set == "yes") or (cfg.set == "true")) then action_set = 1 end
or if (cfg.set == "yes" or cfg.set == "true") then action_set = 1 end
then the error is on line 5? (
for index, set_sides in ipairs(set_sides) do
)Reverting back to
if (cfg.set == yes or cfg.set == true) then action_set = 1 end
or if ((cfg.set == yes) or (cfg.set == true)) then action_set = 1 end
moves the error back to line 4, so quotes make a difference (so I guess the yes
ain't working right).I get the same error whether
set
is absent or present and =yes
or =no
.I have spent a fair bit of time on this and would like to ask what I am doing wrong? (and there's probably more than one answer!) as I seem to be going round in circles...
Aside
Oh, and an exampe of how to code a numbered list in this forum using the
list=a ... /list
format would help as I can do "a" but not "b"...Note: posted regarding this some time ago on development-ideas.
Any thoughts/comments greatly appreciated!
Cheers!
-- Spannerbag
- Pentarctagon
- Project Manager
- Posts: 5584
- Joined: March 22nd, 2009, 10:50 pm
- Location: Earth (occasionally)
Re: Lua newbie custom tag query
yes
or no
is allowed without quotes in WML as equivalent to true
and false
, but that is not the case in lua, where putting just yes
or no
without quotes means you're trying to use a variable that's named yes
or no
.99 little bugs in the code, 99 little bugs
take one down, patch it around
-2,147,483,648 little bugs in the code
take one down, patch it around
-2,147,483,648 little bugs in the code
- Spannerbag
- Posts: 549
- Joined: December 18th, 2016, 6:14 pm
- Location: Yes
Re: Lua newbie custom tag query
Hi,Pentarctagon wrote: ↑May 24th, 2021, 2:30 pmyes
orno
is allowed without quotes in WML as equivalent totrue
andfalse
, but that is not the case in lua, where putting justyes
orno
without quotes means you're trying to use a variable that's namedyes
orno
.
Thanks for the reply - I did try with and without (double) quotes and the error message didn't change except that the "4" became a "5"?
I'd got as far as realising that part of the problem at least is with the "yes" but am stuck. Maybe I should try a different delimiter?Spannerbag wrote: ↑May 24th, 2021, 1:42 pm ...
Line 4 would seem to beif cfg.set == yes or cfg.set == true then action_set = 1 end
so the equality test would seem to be the issue?
Weirdly (to me at least) if I change the equality test to:
if ((cfg.set == "yes") or (cfg.set == "true")) then action_set = 1 end
orif (cfg.set == "yes" or cfg.set == "true") then action_set = 1 end
then the error is on line 5? (for index, set_sides in ipairs(set_sides) do
)
Reverting back toif (cfg.set == yes or cfg.set == true) then action_set = 1 end
orif ((cfg.set == yes) or (cfg.set == true)) then action_set = 1 end
moves the error back to line 4, so quotes make a difference (so I guess theyes
ain't working right).
...
Again, thanks for taking the trouble to reply.
Cheers,
-- Spannerbag
- Pentarctagon
- Project Manager
- Posts: 5584
- Joined: March 22nd, 2009, 10:50 pm
- Location: Earth (occasionally)
Re: Lua newbie custom tag query
I don't understand what you mean by "delimiter", but using
yes
is simply incorrect because there is no variable by that name declared here and "yes"
won't actually do anything because when going from WML -> lua it will get changed to true
.99 little bugs in the code, 99 little bugs
take one down, patch it around
-2,147,483,648 little bugs in the code
take one down, patch it around
-2,147,483,648 little bugs in the code
Re: Lua newbie custom tag query
Here's a rule of thumb to avoid headaches.
In WML only use
In Lua only use
They will auto-magically translate between the two languages.
If the key is missing in WML, then its value will be
You don't have to test cfg.variable against yes or true, only the true/false/nil test is needed.
So
I'm looking at your original post and it said
set_amount in WML should have been
I didn't test it, but didn't what I write in the other post do exactly what you asked for and what you're trying to do here?
In WML only use
yes
/no
for bools, never true/false.In Lua only use
true
/false
for bools, never yes/noThey will auto-magically translate between the two languages.
If the key is missing in WML, then its value will be
nil
in lua.You don't have to test cfg.variable against yes or true, only the true/false/nil test is needed.
So
if (cfg.set == yes or cfg.set == true) then action_set = 1 end
should be if (cfg.set == true) then action_set = 1 end
I'm looking at your original post and it said
Code: Select all
[gold]
side=1
amount=100
set_amount=true
[/gold]
set_amount in WML should have been
set_amount=yes
instead.I didn't test it, but didn't what I write in the other post do exactly what you asked for and what you're trying to do here?
Re: Lua newbie custom tag query
Spannerbag wrote: ↑May 24th, 2021, 1:42 pm Lua syntax
- Can someone please clarify the difference (if any) between assigning
var ::== something
vs.var = something
as the manual seems to swap between them without explanantion (to be fair I have not read the entire manual as it's huge).
::=
is not Lua syntax. It's a notation for defining technical terms in Lua's grammar.That's correct.
- How do you structure conditional logic? Again, I found the documentation regarding
or
and()
consufing.
Isif ((var == value1) or (var == value2))
a valid syntax?
or
is an operator, similar to +
or -
. ()
is for grouping which operators to evaluate first.The WML strings "yes", "no", "true", and "false" get translated to Lua as booleans, which is its own data type whose values can be eitherIfset
is true (specifically at present==true
or==yes
but will eventually test foris_false
rather thannot_true
maybe)
then the value of the amount key is the minimum gold the side(s) specified can have.
true
or false
.An
if
condition can be any expression. It usually doesn't make sense to compare two booleans, because that just gives you another boolean. Just use cfg.set
by itself.Yes.I asume the:4:
in[string " function wesnoth.wml_actions.set_gold(cfg)..."]:4: variable 'yes' must be assigned before being used
refers to a line number?
When an error happens, there's no way for Lua to continue executing to find more errors. Fixing one error reveals the next one.Weirdly (to me at least) if I change the equality test to:
if ((cfg.set == "yes") or (cfg.set == "true")) then action_set = 1 end
orif (cfg.set == "yes" or cfg.set == "true") then action_set = 1 end
then the error is on line 5? (for index, set_sides in ipairs(set_sides) do
)
In this case, you're using
set_sides
as both a looping variable and the array to iterate through. I don't see it defined anywhere before that loop.- Elvish_Hunter
- Posts: 1580
- Joined: September 4th, 2009, 2:39 pm
- Location: Lintanir Forest...
Re: Lua newbie custom tag query
Spannerbag wrote: ↑May 24th, 2021, 1:42 pm Can someone please clarify the difference (if any) between assigning var ::== something vs. var = something as the manual seems to swap between them without explanantion (to be fair I have not read the entire manual as it's huge).
var = something
is the assignment operator, whilst var == something
is the equality test operator. This is one of the basics, not just with Lua but with every programming language, so I'd highly advise you to follow the Lua tutorials at http://lua-users.org/wiki/TutorialDirectory.It is valid, and you can even remove the outermost set of braces.Spannerbag wrote: ↑May 24th, 2021, 1:42 pm How do you structure conditional logic? Again, I found the documentation regarding or and () consufing.
Is if ((var == value1) or (var == value2)) a valid syntax?
That
action_set
variable is just confusing and useless; get rid of it and just use if cfg.set then
.In Lua, every value that isn't
false
or nil
casts as true, so a missing or =no
WML key won't pass this test; the downside is that the key will be true with any other value (not just with a WML =yes
!), but this can be fixed later if you really want to.
Spannerbag wrote: ↑May 24th, 2021, 3:10 pm I did try with and without (double) quotes and the error message didn't change except that the "4" became a "5"?
for index, set_sides in ipairs(set_sides) do
: you never defined set_sides
anywhere, and even if you did you're going to mask it in the for loop, so there are two bugs in a single line (well, the masking isn't a bug, but it's confusing, so avoid that).Current maintainer of these add-ons, all on 1.16:
The Sojournings of Grog, Children of Dragons, A Rough Life, Wesnoth Lua Pack, The White Troll (co-author)
The Sojournings of Grog, Children of Dragons, A Rough Life, Wesnoth Lua Pack, The White Troll (co-author)
- Spannerbag
- Posts: 549
- Joined: December 18th, 2016, 6:14 pm
- Location: Yes
Re: Lua newbie custom tag query
Hi again,vghetto wrote: ↑May 24th, 2021, 3:34 pm Here's a rule of thumb to avoid headaches...
You don't have to test cfg.variable against yes or true, only the true/false/nil test is needed...
I didn't test it, but didn't what I write in the other post do exactly what you asked for and what you're trying to do here?
Thanks for the reply, very helpful. Yes, your original post was very close and I used it as a starting point (and managed to move further away from where I wanted to get to...) because I wanted to learn how to do this stuff myself.
Had a lot of replies to my post so will keep this short, but thanks again for your help, much appreciated!
Cheers,
-- Spannerbag.
- Spannerbag
- Posts: 549
- Joined: December 18th, 2016, 6:14 pm
- Location: Yes
Re: Lua newbie custom tag query
Brilliant! That helped a lot! You've given me plenty to work on, thank you for taking the time and trouble to reply, really appreciated!Luther wrote: ↑May 24th, 2021, 3:58 pmSpannerbag wrote: ↑May 24th, 2021, 1:42 pm Lua syntax
...
I asume the:4:
in[string " function wesnoth.wml_actions.set_gold(cfg)..."]:4: variable 'yes' must be assigned before being used
refers to a line number?
...
In this case, you're usingset_sides
as both a looping variable and the array to iterate through. I don't see it defined anywhere before that loop.
Cheers!
-- Spannerbag
- Spannerbag
- Posts: 549
- Joined: December 18th, 2016, 6:14 pm
- Location: Yes
Re: Lua newbie custom tag query
Ah, yet another helpful resource I somehow managed to miss!Elvish_Hunter wrote: ↑May 24th, 2021, 4:18 pm ... I'd highly advise you to follow the Lua tutorials at http://lua-users.org/wiki/TutorialDirectory.
... you never definedset_sides
anywhere...
Thanks, will have a look.
Also thanks for taking the time and trouble to reply, much appreciated!
Cheers!
-- Spannerbag
- Spannerbag
- Posts: 549
- Joined: December 18th, 2016, 6:14 pm
- Location: Yes
Re: Lua newbie custom tag query *working*
Hi everyone,
It's finally working!
Many thanks to all who took the time and trouble to help me, it's really appreciated.
File
I now (kinda) understand the basics of doing lua in Wesnoth.
Thanks again!
Cheers,
-- Spannerbag
It's finally working!
Many thanks to all who took the time and trouble to help me, it's really appreciated.
File
lua/set_gold.lua
is loaded in _main.cfg
and in scenario 1 [set_gold]
works.I now (kinda) understand the basics of doing lua in Wesnoth.
Thanks again!
Cheers,
-- Spannerbag
- Celtic_Minstrel
- Developer
- Posts: 2258
- Joined: August 3rd, 2012, 11:26 pm
- Location: Canada
- Contact:
Re: Lua newbie custom tag query
I see your problem is solved, but one minor query never got an answer.
That produces the following list:
Here's an example of coding a list on this forum:Spannerbag wrote: ↑May 24th, 2021, 1:42 pm Aside
Oh, and an exampe of how to code a numbered list in this forum using thelist=a ... /list
format would help as I can do "a" but not "b"...
Code: Select all
[list=1]
[*]One
[*]Two
[*]Three
[/list]
- One
- Two
- Three
[list]
instead of [list=1]
, it'll give you a bulleted list; in all other respects, the code for a bulleted list is the same. There's no way that I know of to specify the starting number for the list, though. If you want to intersperse stuff between the list items, I'd suggest putting the between stuff inside the preceding list item.Not every language. Some languages useElvish_Hunter wrote: ↑May 24th, 2021, 4:18 pmvar = something
is the assignment operator, whilstvar == something
is the equality test operator. This is one of the basics, not just with Lua but with every programming language,
=
for equality and either don't have assignment (like WFL or SQL) or use :=
for assignment.- Spannerbag
- Posts: 549
- Joined: December 18th, 2016, 6:14 pm
- Location: Yes
Re: Lua newbie custom tag query
Aha!
Thanks for taking the trouble to clarify, much appreciated!
-- Spannerbag
Thanks for taking the trouble to clarify, much appreciated!
Cheers,Celtic_Minstrel wrote: ↑June 6th, 2021, 3:39 pm I see your problem is solved, but one minor query never got an answer.
Here's an example of coding a list on this forum:
That produces the following list:Code: Select all
[list=1] [*]One [*]Two [*]Three [/list]
If you open it with
- One
- Two
- Three
[list]
instead of[list=1]
, it'll give you a bulleted list; in all other respects, the code for a bulleted list is the same. There's no way that I know of to specify the starting number for the list, though. If you want to intersperse stuff between the list items, I'd suggest putting the between stuff inside the preceding list item.
-- Spannerbag