[solved] How to store unit using custom wml tag

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

Moderator: Forum Moderators

Post Reply
User avatar
ZombieKnight
Posts: 220
Joined: June 27th, 2022, 2:26 pm
Location: Czech Republic

[solved] How to store unit using custom wml tag

Post by ZombieKnight »

Hi
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...
User avatar
Celtic_Minstrel
Developer
Posts: 2258
Joined: August 3rd, 2012, 11:26 pm
Location: Canada
Contact:

Re: How to store unit using custom wml tag

Post by Celtic_Minstrel »

If you're asking how to store a unit in a WML variable from Lua, like [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
If you're asking how to find a unit based on the content of the ActionWML tag, like [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
Note that [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.
Author of The Black Cross of Aleron campaign and Default++ era.
Former maintainer of Steelhive.
User avatar
ZombieKnight
Posts: 220
Joined: June 27th, 2022, 2:26 pm
Location: Czech Republic

Re: How to store unit using custom wml tag

Post by ZombieKnight »

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...
User avatar
Celtic_Minstrel
Developer
Posts: 2258
Joined: August 3rd, 2012, 11:26 pm
Location: Canada
Contact:

Re: How to store unit using custom wml tag

Post by Celtic_Minstrel »

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
As long as everything you want to remove is directly in the tag (and not in some subtag), that should be sufficient. The use of 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.
Author of The Black Cross of Aleron campaign and Default++ era.
Former maintainer of Steelhive.
User avatar
ZombieKnight
Posts: 220
Joined: June 27th, 2022, 2:26 pm
Location: Czech Republic

Re: How to store unit using custom wml tag

Post by ZombieKnight »

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!
I had saurian in profile before, but I've merged my discord profile with forum one...
Post Reply