reference of last unit created

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

reference of last unit created

Post by vghetto »

Hi,

I'm wondering how can I get the reference .id of the last unit created with [unit]?
I want that unit to say something without giving it an explicit ID or role, because there could be more of them later on.
The location of the created unit is not always guaranteed, so can't use $x1,$y1 for that.
The way I'm currently doing it is by creating it into a variable with to_variable= then unstoring with [unstore_unit] and grabbing the .id from the variable.

Is there a shortcut for doing that?
User avatar
Ravana
Forum Moderator
Posts: 2952
Joined: January 29th, 2012, 12:49 am
Location: Estonia
Contact:

Re: reference of last unit created

Post by Ravana »

I would proxy wesnoth.wml_actions.unit to save flag of unit creation, and in unit placed event collect the id if flag is set. That is if the [unit] calls are not from your own code.
vghetto
Posts: 755
Joined: November 2nd, 2019, 5:12 pm

Re: reference of last unit created

Post by vghetto »

I thought I'd share how I resolved this problem for my use case.
I didn't try Ravana's method, but I think that it would work too. Thank you for the suggestion.

Obviously when I asked this I thought role= and to_variable= were the only outs at the time.
I was either unaware or completely forgot about variables and status.

Downside of to_variable= is that animate=yes in unstore_unit doesn't give the creation animation.
Or maybe I needed to have animate=yes in [unit] too, I don't know, I didn't test.

This is how I'm doing it with status.

Code: Select all

# add status=last to [unit] when creating.
[unit]
...
    [status]
        last=yes
    [/status]
[/unit]

# after the [unit] block store the last created unit by filtering on the status.
# the created unit id will be in last_unit.id
    [store_unit]
        variable=last_unit
        kill=no
        mode=always_clear
        [filter]
            status=last
        [/filter]
    [/store_unit]

# do the message
[message]
    id=$last_unit.id
    message="It's a boy!"
[/message]

# Once done with the id, remove the last status and clear last_unit.
    [modify_unit]
        [filter]
            status=last
        [/filter]
        [effect]
            apply_to=status
            remove=last
        [/effect]
    [/modify_unit]
    {CLEAR_VARIABLE last_unit}
Seriously why didn't I think of this earlier :oops:
Pilauli
Posts: 115
Joined: August 18th, 2020, 12:56 pm

Re: reference of last unit created

Post by Pilauli »

I haven't tested this but you could try using [animate_unit] to play a "recruited" animation.
https://wiki.wesnoth.org/InterfaceActio ... te_unit.5D
https://wiki.wesnoth.org/AnimationWML#recruited
User avatar
Pentarctagon
Project Manager
Posts: 5531
Joined: March 22nd, 2009, 10:50 pm
Location: Earth (occasionally)

Re: reference of last unit created

Post by Pentarctagon »

While not exactly what you're looking for, there's also the unit placed event.
99 little bugs in the code, 99 little bugs
take one down, patch it around
-2,147,483,648 little bugs in the code
vghetto
Posts: 755
Joined: November 2nd, 2019, 5:12 pm

Re: reference of last unit created

Post by vghetto »

Pilauli wrote: February 11th, 2021, 11:51 pm I haven't tested this but you could try using [animate_unit] to play a "recruited" animation.
https://wiki.wesnoth.org/InterfaceActio ... te_unit.5D
https://wiki.wesnoth.org/AnimationWML#recruited
Yeah, I guess that would solve the to_variable problem.
Pentarctagon wrote: February 11th, 2021, 11:57 pm While not exactly what you're looking for, there's also the unit placed event.
Hmm, yeah. If it fires right away after the [unit] is created, it could work as well I guess.

Code: Select all

[event]
    name=unit placed
    first_time_only=no
    [filter]
    [/filter]
    {VARIABLE last_unit_id $unit.id}
[/event]
Edit: Just tried unit placed, and it works for my needs perfectly! thank you.
Post Reply