Using Lua to add and remove abilities
Moderator: Forum Moderators
- Lord-Knightmare
- Discord Moderator
- Posts: 2276
- Joined: May 24th, 2010, 5:26 pm
- Location: Somewhere in the depths of Irdya, gathering my army to eventually destroy the known world.
- Contact:
Using Lua to add and remove abilities
Hello,
I am trying to use Lua to check for and then replace an ability with another one but I ran into some problems which the wiki doesn't seem to have answers to.
I added where my confusion started since I am skeptical if I can add or remove abilities like this. This can be done with WML, but I prefer to do stuff in Lua these days.
I am trying to use Lua to check for and then replace an ability with another one but I ran into some problems which the wiki doesn't seem to have answers to.
Code: Select all
local effects = {
id = "WOL_add_allied_leadership",
wml.tag.effect {
apply_to = "remove_ability",
wml.tag.abilities {
-- I am confused here
-- please clarify if this would
-- work or not
wml.tag.leadership {
id = "leadership",
},
},
},
-- I am not sure about this part
-- not sure if any other way to add
-- a new ability
wml.tag.effect {
apply_to = "new_ability",
wml.tag.abilities {
wml.tag.leadership {
id = "allied_leadership",
}
}
}
}
Creator of "War of Legends"
Creator of the Isle of Mists survival scenario.
Maintainer of Forward They Cried
User:Knyghtmare-:-Friend me on Facebook-:-Follow me on Twitter
Creator of the Isle of Mists survival scenario.
Maintainer of Forward They Cried
User:Knyghtmare-:-Friend me on Facebook-:-Follow me on Twitter
Re: Using Lua to add and remove abilities
Right now you are just creating a local variable which by itself doesn't do anything, your variable seems to describe an [object] wml table. do Maybe you want to use it as a parameter to the unit:add_modification function ? I'm not sure whether you understood your question directly.
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.
- Lord-Knightmare
- Discord Moderator
- Posts: 2276
- Joined: May 24th, 2010, 5:26 pm
- Location: Somewhere in the depths of Irdya, gathering my army to eventually destroy the known world.
- Contact:
Re: Using Lua to add and remove abilities
Tried it in game just now...doesn't work one bit
full code
full code
Creator of "War of Legends"
Creator of the Isle of Mists survival scenario.
Maintainer of Forward They Cried
User:Knyghtmare-:-Friend me on Facebook-:-Follow me on Twitter
Creator of the Isle of Mists survival scenario.
Maintainer of Forward They Cried
User:Knyghtmare-:-Friend me on Facebook-:-Follow me on Twitter
Re: Using Lua to add and remove abilities
I assume the talk in discord has resolved this question?
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.
- Lord-Knightmare
- Discord Moderator
- Posts: 2276
- Joined: May 24th, 2010, 5:26 pm
- Location: Somewhere in the depths of Irdya, gathering my army to eventually destroy the known world.
- Contact:
Re: Using Lua to add and remove abilities
I tried and...
well, it does not work...at all.
I guess I have to try more questionable methods to strangle it to work
well, it does not work...at all.

I guess I have to try more questionable methods to strangle it to work
Creator of "War of Legends"
Creator of the Isle of Mists survival scenario.
Maintainer of Forward They Cried
User:Knyghtmare-:-Friend me on Facebook-:-Follow me on Twitter
Creator of the Isle of Mists survival scenario.
Maintainer of Forward They Cried
User:Knyghtmare-:-Friend me on Facebook-:-Follow me on Twitter
Re: Using Lua to add and remove abilities
The code
will just add an empty leadership ability like
with no
Code: Select all
add_modification( ....
-- apply_to = "new_ability",
-- wml.tag.abilities {
-- wml.tag.leadership {
-- id = "allied_leadership",
-- }
-- }
Code: Select all
[leadership]
id = allied_leadership
[/leadership]
value
or other attributes it is, really important to understand that the code generated by wml.tag etc. and that is passed to function like add_mofifiction
really behaves the same way as the same wml table would, using [object] ... [effect] ... [abilities][leadership] id = "allied_leadership"[/leadership] ...
Would also only add an empty and nonfunctional leadership ability,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.
- Celtic_Minstrel
- Developer
- Posts: 2056
- Joined: August 3rd, 2012, 11:26 pm
- Location: Canada
- Contact:
Re: Using Lua to add and remove abilities
I think what you need is something akin to this:
Explanation:
Code: Select all
[lua]
[args]
{LEADERSHIP}
{ALLIED_LEADERSHIP}
[/args]
code=<<
local leadership = wml.get_nth_child(..., "leadership", 1)
local allied_leadership = wml.get_nth_child(..., "leadership", 2)
local effects = {
id = "WOL_add_allied_leadership",
wml.tag.effect {
apply_to = "remove_ability",
wml.tag.abilities {
wml.tag.leadership(leadership)
}
},
wml.tag.effect {
apply_to = "new_ability",
wml.tag.abilities {
wml.tag.allied_leadership(allied_leadership)
}
}
}
local unit = wesnoth.unit.get "my_unit"
unit:add_modification("object", effects)
>>
[/lua]
- The
[args]
tag imports the contents of the macros so you can reference them from the Lua code. - The special variable
...
is a reference to the contents of[args]
, so grab the first and second[leadership]
tags from it. wml.tag.whatever
is a function, so you can pass the value from step 2 to it directly.- Nothing will actually happen if you don't call
add_modification
.
- Lord-Knightmare
- Discord Moderator
- Posts: 2276
- Joined: May 24th, 2010, 5:26 pm
- Location: Somewhere in the depths of Irdya, gathering my army to eventually destroy the known world.
- Contact:
Re: Using Lua to add and remove abilities
Code: Select all
20230413 23:08:25 error scripting/lua: In function lua_kernel::run(): Lua Error:
When executing, Lua runtime error: ...ns/War_of_Legends/lua/mechanics/rpg_utils_leadership.lua:9: Expected a table or wml object but got nil
stack traceback:
[C]: in function '.error'
lua/core/wml.lua:13: in upvalue 'ensure_config'
lua/core/wml.lua:47: in function 'wml.get_nth_child'
...ns/War_of_Legends/lua/mechanics/rpg_utils_leadership.lua:9: in main chunk
[C]: in field 'dofile'
[string " wesnoth.dofile "~add-ons/War_of_Legends/lua/..."]:1: in main chunk
Creator of "War of Legends"
Creator of the Isle of Mists survival scenario.
Maintainer of Forward They Cried
User:Knyghtmare-:-Friend me on Facebook-:-Follow me on Twitter
Creator of the Isle of Mists survival scenario.
Maintainer of Forward They Cried
User:Knyghtmare-:-Friend me on Facebook-:-Follow me on Twitter
- Celtic_Minstrel
- Developer
- Posts: 2056
- Joined: August 3rd, 2012, 11:26 pm
- Location: Canada
- Contact:
Re: Using Lua to add and remove abilities
Are you using my code verbatim? The message implies you're not, so that error message doesn't really help me. I would need to see the code it applies to. If your Lua code is in a separate file then I would also need to see the
To be perfectly clear: the code I posted (at least in theory) works because it's directly in a Lua tag. If you want it to work in any other context then it needs some adjustments.
[lua]
tag that calls it. (In fact the tag is probably the more important piece here, but both would be good to see.)To be perfectly clear: the code I posted (at least in theory) works because it's directly in a Lua tag. If you want it to work in any other context then it needs some adjustments.
- Lord-Knightmare
- Discord Moderator
- Posts: 2276
- Joined: May 24th, 2010, 5:26 pm
- Location: Somewhere in the depths of Irdya, gathering my army to eventually destroy the known world.
- Contact:
Re: Using Lua to add and remove abilities
Update. I got it to work.

Creator of "War of Legends"
Creator of the Isle of Mists survival scenario.
Maintainer of Forward They Cried
User:Knyghtmare-:-Friend me on Facebook-:-Follow me on Twitter
Creator of the Isle of Mists survival scenario.
Maintainer of Forward They Cried
User:Knyghtmare-:-Friend me on Facebook-:-Follow me on Twitter