Using Lua to add and remove abilities

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

Moderator: Forum Moderators

Post Reply
User avatar
Lord-Knightmare
Discord Moderator
Posts: 2337
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

Post by Lord-Knightmare »

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.

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",
                }
            }
        }
    }
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.
Creator of "War of Legends"
Creator of the Isle of Mists survival scenario.
Maintainer of Forward They Cried
User:Knyghtmare | My Medium
gfgtdf
Developer
Posts: 1431
Joined: February 10th, 2013, 2:25 pm

Re: Using Lua to add and remove abilities

Post by gfgtdf »

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.
User avatar
Lord-Knightmare
Discord Moderator
Posts: 2337
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

Post by Lord-Knightmare »

Tried it in game just now...doesn't work one bit
full code
Creator of "War of Legends"
Creator of the Isle of Mists survival scenario.
Maintainer of Forward They Cried
User:Knyghtmare | My Medium
gfgtdf
Developer
Posts: 1431
Joined: February 10th, 2013, 2:25 pm

Re: Using Lua to add and remove abilities

Post by gfgtdf »

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.
User avatar
Lord-Knightmare
Discord Moderator
Posts: 2337
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

Post by Lord-Knightmare »

I tried and...
well, it does not work...at all. :cry:

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

Re: Using Lua to add and remove abilities

Post by gfgtdf »

The code

Code: Select all

add_modification( ....
        --     apply_to = "new_ability",
        --     wml.tag.abilities {
        --         wml.tag.leadership {
        --             id = "allied_leadership",
        --         }
        --     }
will just add an empty leadership ability like

Code: Select all

[leadership]
  id = allied_leadership
[/leadership]
with no 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.
User avatar
Celtic_Minstrel
Developer
Posts: 2158
Joined: August 3rd, 2012, 11:26 pm
Location: Canada
Contact:

Re: Using Lua to add and remove abilities

Post by Celtic_Minstrel »

I think what you need is something akin to this:

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]
Explanation:
  • 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.
Author of The Black Cross of Aleron campaign and Default++ era.
Former maintainer of Steelhive.
User avatar
Lord-Knightmare
Discord Moderator
Posts: 2337
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

Post by Lord-Knightmare »

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
Nope. We're back to ground zero.
Creator of "War of Legends"
Creator of the Isle of Mists survival scenario.
Maintainer of Forward They Cried
User:Knyghtmare | My Medium
User avatar
Celtic_Minstrel
Developer
Posts: 2158
Joined: August 3rd, 2012, 11:26 pm
Location: Canada
Contact:

Re: Using Lua to add and remove abilities

Post by Celtic_Minstrel »

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 [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.
Author of The Black Cross of Aleron campaign and Default++ era.
Former maintainer of Steelhive.
User avatar
Lord-Knightmare
Discord Moderator
Posts: 2337
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

Post by Lord-Knightmare »

Update. I got it to work.

:fire: :fire:
Creator of "War of Legends"
Creator of the Isle of Mists survival scenario.
Maintainer of Forward They Cried
User:Knyghtmare | My Medium
Post Reply