Force rebuild a unit

The place to post your WML questions and answers.

Moderator: Forum Moderators

Forum rules
  • Please use [code] BBCode tags in your posts for embedding WML snippets.
  • To keep your code readable so that others can easily help you, make sure to indent it following our conventions.
Post Reply
vghetto
Posts: 755
Joined: November 2nd, 2019, 5:12 pm

Force rebuild a unit

Post by vghetto »

Hi,
Is there a way of force rebuilding a unit from it's base type?
User avatar
Heindal
Posts: 1344
Joined: August 11th, 2011, 9:25 pm
Location: Germany, Karlsruhe
Contact:

Re: Force rebuild a unit

Post by Heindal »

Simple: kill it and spawn the unit again.

Code: Select all

[kill]
id=unit
[/kill]

[unit]
id=Unit
name=_"Unit"
type=Unit
x,y=$x1,$y1
[/unit]
The future belongs to those, who believe in the beauty of their dreams.
Developer of: Trapped, Five Fates, Strange Legacy, Epical, UR Epic Era
Dungeonmasters of Wesnoth, Wild Peasants vs Devouring Corpses, Dwarf Dwarfson Dwarvenminer
vghetto
Posts: 755
Joined: November 2nd, 2019, 5:12 pm

Re: Force rebuild a unit

Post by vghetto »

That's not what I was after. My question was unclear.
I don't want a fresh unit, I want the current modifications+amlas minus some [object]
The object might affect attack, abilities, resistance ....
So I want to remove that object manually and then force rebuild the unit, so any effects that the object might have applied to be removed.
User avatar
Lord-Knightmare
Discord Moderator
Posts: 2340
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: Force rebuild a unit

Post by Lord-Knightmare »

vghetto wrote: June 23rd, 2021, 7:05 pm That's not what I was after. My question was unclear.
I don't want a fresh unit, I want the current modifications+amlas minus some [object]
The object might affect attack, abilities, resistance ....
So I want to remove that object manually and then force rebuild the unit, so any effects that the object might have applied to be removed.
https://wiki.wesnoth.org/LuaWML/Units#w ... ifications
Creator of "War of Legends"
Creator of the Isle of Mists survival scenario.
Maintainer of Forward They Cried
User:Knyghtmare | My Medium
vghetto
Posts: 755
Joined: November 2nd, 2019, 5:12 pm

Re: Force rebuild a unit

Post by vghetto »

Thanks, remove_modifications is what I was looking for.

I didn't have an id set for the [object]s so I tried to remove them by name instead with the following:

Code: Select all

function wesnoth.wml_actions.remove_object_name(cfg)
        local obj_id = cfg.object_name
        for _,unit in ipairs(wesnoth.get_units(cfg)) do
                wesnoth.remove_modifications(unit, {name = obj_id}, "object")
        end
end
That didn't work. I suspect it has something to do with the _ textdomain thingy. name=_"Some Object"
Anyone knows why the lua snippet above did not work?

Eventually I bit the bullet and added an id to the objects and used vanilla [remove_object]. That did the job.
User avatar
Celtic_Minstrel
Developer
Posts: 2166
Joined: August 3rd, 2012, 11:26 pm
Location: Canada
Contact:

Re: Force rebuild a unit

Post by Celtic_Minstrel »

If object_name is a translatable string, that's not a good choice to match the object by. You should match on something that's not translatable. That said, my understanding is that it should have worked, so I'm not sure what went wrong. If there's an issue, it's in the WML filters themselves, because I tried the following code and it also didn't match:

Code: Select all

local _ = wesnoth.textdomain "wesnoth"
print(wml.matches_filter({foo = _'bar', bar = 'foo'}, {foo = _'bar'}))
It matches if the underscores are removed, of course.
Author of The Black Cross of Aleron campaign and Default++ era.
Former maintainer of Steelhive.
vghetto
Posts: 755
Joined: November 2nd, 2019, 5:12 pm

Re: Force rebuild a unit

Post by vghetto »

The object name in WML was like so name=_"Some Object"

I did std_print on object_name and the output was Some Object. It was without quotes and without the underscore.

The docs say that remove_modifications takes [filter_wml].
Maybe using tag.filter_wml or literal, how would I go about transcribing it in lua to look exactly like WML's [filter_wml]?
User avatar
Celtic_Minstrel
Developer
Posts: 2166
Joined: August 3rd, 2012, 11:26 pm
Location: Canada
Contact:

Re: Force rebuild a unit

Post by Celtic_Minstrel »

The filter in your example code is correct, assuming you had something like this:

Code: Select all

[remove_object_name]
	name=_"Some Object"
[/remove_object_name]
But it looks like something in WML filters prevents it from matching translatable strings.
Author of The Black Cross of Aleron campaign and Default++ era.
Former maintainer of Steelhive.
Post Reply