Clone unit in WML

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
white_haired_uncle
Posts: 1226
Joined: August 26th, 2018, 11:46 pm
Location: A country place, far outside the Wire

Clone unit in WML

Post by white_haired_uncle »

I have to be missing something here. Without using lua, how do I simply clone a unit? I want to make a copy of a leader, and then change a few things (like canrecruit).

I could make a new unit and copy all its attributes. I guess that's simple, but tedious.

I could store a unit and unstore it. I think if I unstore it on top of the original (probably with find_vacant=yes), I'll get a clone with a new id and x,y. This looks promising, but how then do I refer to it? It has a new id, x, y, but AFAIK unstore does not return anything like that to me. I guess I could manipulate the id while it's stored, but this is not truly deterministic and so I'd rather have wesnoth assign a new one that is guaranteed unique (at least I think it is), and it's venturing away from the simple option that I suspect exists but eludes me.
Speak softly, and carry Doombringer.
User avatar
Ravana
Forum Moderator
Posts: 3018
Joined: January 29th, 2012, 12:49 am
Location: Estonia
Contact:

Re: Clone unit in WML

Post by Ravana »

Store, change id, unstore. If you do not trust yourself to avoid id conflicts then generate new [unit], save its id, kill it, and assign that id to copy.
white_haired_uncle
Posts: 1226
Joined: August 26th, 2018, 11:46 pm
Location: A country place, far outside the Wire

Re: Clone unit in WML

Post by white_haired_uncle »

Ravana wrote: April 11th, 2024, 3:36 pm Store, change id, unstore. If you do not trust yourself to avoid id conflicts then generate new [unit], save its id, kill it, and assign that id to copy.
If I knew how to do that, couldn't I just unstore the original unit over itself (with find_vacant=yes) and let a new id be assigned?

Of course, I could just assign an id by randomly punching my keyboard a few times and almost certainly be good, this is basically an academic question.

Or I could create/id/kill a unit on an impassible tile so I knew the x,y, but that's a little ugly.
Speak softly, and carry Doombringer.
User avatar
Ravana
Forum Moderator
Posts: 3018
Joined: January 29th, 2012, 12:49 am
Location: Estonia
Contact:

Re: Clone unit in WML

Post by Ravana »

[unit]to_variable

It might happen that if you delete id completely then it is generated for copy too.
User avatar
Celtic_Minstrel
Developer
Posts: 2241
Joined: August 3rd, 2012, 11:26 pm
Location: Canada
Contact:

Re: Clone unit in WML

Post by Celtic_Minstrel »

If I understand correctly, you want to make a unit that's a copy of the leader unit while avoiding ID conflicts? In that case, all you need to do is store the unit, delete the ID from the stored unit, modify anything else you want, and then unstore the unit. A stored unit is already a copy of the unit, after all.
Author of The Black Cross of Aleron campaign and Default++ era.
Former maintainer of Steelhive.
User avatar
Heindal
Posts: 1360
Joined: August 11th, 2011, 9:25 pm
Location: Germany, Karlsruhe
Contact:

Re: Clone unit in WML

Post by Heindal »

I'm doing so, when I spawn "shadows" uncle. You can use the code if you like.
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
Post Reply