reference of last unit created
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.
reference of last unit created
Hi,
I'm wondering how can I get the reference .id of the last unit created with
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
Is there a shortcut for doing that?
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?
Re: reference of last unit created
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.
Re: reference of last unit created
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.
Seriously why didn't I think of this earlier
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}
Re: reference of last unit created
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
https://wiki.wesnoth.org/InterfaceActio ... te_unit.5D
https://wiki.wesnoth.org/AnimationWML#recruited
- Pentarctagon
- Project Manager
- Posts: 5566
- Joined: March 22nd, 2009, 10:50 pm
- Location: Earth (occasionally)
Re: reference of last unit created
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
take one down, patch it around
-2,147,483,648 little bugs in the code
Re: reference of last unit created
Yeah, I guess that would solve thePilauli 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
to_variable
problem.Hmm, yeah. If it fires right away after the [unit] is created, it could work as well I guess.Pentarctagon wrote: ↑February 11th, 2021, 11:57 pm While not exactly what you're looking for, there's also theunit placed
event.
Code: Select all
[event]
name=unit placed
first_time_only=no
[filter]
[/filter]
{VARIABLE last_unit_id $unit.id}
[/event]