[solved] How to store unit using custom wml tag
Moderator: Forum Moderators
- ZombieKnight
- Posts: 235
- Joined: June 27th, 2022, 2:26 pm
- Location: Czech Republic
[solved] How to store unit using custom wml tag
Hi
How to store unit using custom wml tag?
(Same way as [message] does, without [filter tag])
Thanks
How to store unit using custom wml tag?
(Same way as [message] does, without [filter tag])
Thanks
Last edited by ZombieKnight on April 28th, 2024, 6:53 am, edited 1 time in total.
I had saurian in profile before, but I've merged my discord profile with forum one...
Working on campaign Bandits from Brown Hills
Working on campaign Bandits from Brown Hills
- Celtic_Minstrel
- Developer
- Posts: 2260
- Joined: August 3rd, 2012, 11:26 pm
- Location: Canada
- Contact:
Re: How to store unit using custom wml tag
If you're asking how to store a unit in a WML variable from Lua, like
If you're asking how to find a unit based on the content of the ActionWML tag, like
Note that
[store_unit]
does:Code: Select all
local unit = wesnoth.units.find "unit_id" -- get the unit however you want, this example looks it up by its ID
wml.variables["your.variable[2].path"] = unit.__cfg
[message]
does:Code: Select all
function wesnoth.wml_actions.your_tag(cfg)
local unit = wesnoth.units.find(cfg)[1] -- this assumes you want to take the first match – find returns all matching units as an array
-- do stuff with unit here
end
[message]
is more complicated than that in order to handle the speaker=
key, which isn't part of StandardUnitFilter. So, if you use the above example, you can't use speaker=
. If you want, you can look at the implementation of the [message]
tag in data/lua/wml/message.lua, but be warned that it is very complicated in order to handle a wide variety of situations.
Last edited by Celtic_Minstrel on April 28th, 2024, 5:08 am, edited 1 time in total.
- ZombieKnight
- Posts: 235
- Joined: June 27th, 2022, 2:26 pm
- Location: Czech Republic
Re: How to store unit using custom wml tag
But how to remove some values from the cfg (like message=)?
I had saurian in profile before, but I've merged my discord profile with forum one...
Working on campaign Bandits from Brown Hills
Working on campaign Bandits from Brown Hills
- Celtic_Minstrel
- Developer
- Posts: 2260
- Joined: August 3rd, 2012, 11:26 pm
- Location: Canada
- Contact:
Re: How to store unit using custom wml tag
Code: Select all
function wesnoth.wml_actions.your_tag(cfg)
local filter = wml.shallow_literal(cfg)
filter.key_you_want_to_remove = nil
-- repeat for other keys
wml.remove_children(filter, 'tag_you_want_to_remove', 'another_tag_you_want_to_remove')
filter = wml.tovconfig(filter)
-- now just use filter instead of cfg like in the previous example.
end
shallow_literal
and tovconfig
ensures that any use of $this_unit
in the filter will still function correctly. If you didn't care about that for some reason, you could substitute shallow_parsed
and omit the tovconfig
line. I don't really recommend doing that however.- ZombieKnight
- Posts: 235
- Joined: June 27th, 2022, 2:26 pm
- Location: Czech Republic
Re: How to store unit using custom wml tag
I guess, I'll have to look into the wiki for those two new words...
Thanks that's exactly what I've been looking for!
Thanks that's exactly what I've been looking for!
I had saurian in profile before, but I've merged my discord profile with forum one...
Working on campaign Bandits from Brown Hills
Working on campaign Bandits from Brown Hills