Teleport Function
Moderator: Forum Moderators
Teleport Function
About the [teleport] function.
I believe this is the code of the teleport tag.
Would anyone here be able to create a teleport code that could teleport more than one unit at the same time? Similar to the [animate_unit] tag, where we can animate multiple units at once using [animate].
Is it possible?
Code: Select all
wml_actions.teleport = function(cfg)
local context = wesnoth.current.event_context
local filter = wml.get_child(cfg, "filter") or { x = context.x1, y = context.y1 }
local unit = wesnoth.units.find_on_map(filter)[1]
if not unit then
-- No error if no unit matches.
return
end
local x,y = cfg.x, cfg.y
if cfg.location_id then
x,y = table.unpack(wesnoth.current.map.special_locations[cfg.location_id])
end
unit:teleport(x, y, cfg.check_passability == false, cfg.clear_shroud ~= false, cfg.animate)
end
Would anyone here be able to create a teleport code that could teleport more than one unit at the same time? Similar to the [animate_unit] tag, where we can animate multiple units at once using [animate].
Is it possible?

Re: Teleport Function
Loop wesnoth.units.find_on_map(filter)[1] instead of taking first.
Re: Teleport Function

What I wanted was to teleport multiple units at the same time, each one to a different location (not all to the same place); also with the option animate.
Maybe this is too much...

Last edited by Saizo-Luz on November 28th, 2024, 1:54 pm, edited 1 time in total.
- Celtic_Minstrel
- Developer
- Posts: 2371
- Joined: August 3rd, 2012, 11:26 pm
- Location: Canada
- Contact:
Re: Teleport Function
If all they are doing is teleporting from point A to point B, you can probably use
[animate_unit]
.- Animate all the units with
flag=pre_teleport
. - Move each unit to the destination space, eg with
[modify_unit]
. I think this has to be done one unit at a time. - Animate all the units with
flag=post_teleport
.
Re: Teleport Function
I did this! Animate them all, hide, teleport and animate again. But it's a lousy alternative; it's kinda laggy and messy. That's why I'm here - maybe someone is capable of doing a code.Celtic_Minstrel wrote: ↑November 28th, 2024, 1:49 pm If all they are doing is teleporting from point A to point B, you can probably use[animate_unit]
.
- Animate all the units with
flag=pre_teleport
.- Move each unit to the destination space, eg with
[modify_unit]
. I think this has to be done one unit at a time.- Animate all the units with
flag=post_teleport
.
Re: Teleport Function


Since no one was capable of creating a code to do such a thing, I was using the lousy alternative to teleport more than one unit: animate pre_teleport flag, hide units, teleport them with animate=no, unhide and animate post_teleport flag.

The problem was that when I unhide the units, sometimes (not always) they appear before displaying the animation - and that bothered me!


So I decided to take a look at the code of the [unhide_unit] tag. And I realized there was a [redraw] tag in it. Look...
Code: Select all
function wml_actions.unhide_unit(cfg)
for i,u in ipairs(wesnoth.units.find_on_map(cfg)) do
u.hidden = false
end
wml_actions.redraw {} #HERE!
end

Code: Select all
function wesnoth.wml_actions.el_unhide_unit(cfg)
for i,u in ipairs(wesnoth.units.find_on_map(cfg)) do
u.hidden = false
end
end

Code: Select all
#define TELEPORT_3_UNITS X Y ONE X1 Y1 TWO X2 Y2 THREE X3 Y3
{ANIMATE_UNIT id={ONE} pre_teleport {ANIMATE id={TWO} pre_teleport {ANIMATE id={THREE} pre_teleport ()}}}
[hide_unit]
id={ONE},{TWO},{THREE}
[/hide_unit]
{TELEPORT id={ONE} {X1} {Y1} no}
{TELEPORT id={TWO} {X2} {Y2} no}
{TELEPORT id={THREE} {X3} {Y3} no}
{SCROLL_TO {X} {Y}}
[el_unhide_unit]
id={ONE},{TWO},{THREE}
[/el_unhide_unit]
{ANIMATE_UNIT id={ONE} post_teleport {ANIMATE id={TWO} post_teleport {ANIMATE id={THREE} post_teleport ()}}}
#enddef

- Spannerbag
- Posts: 759
- Joined: December 18th, 2016, 6:14 pm
- Location: Yes
Re: Teleport Function
Not tried your code but looks good. 
Would it be possible to generalise the macro to accept an array?
I.e. rather than:
Have something like this (the macro looks for an array called
Just a thought.
Cheers!
-- Spannerbag

Would it be possible to generalise the macro to accept an array?
I.e. rather than:
Code: Select all
#define TELEPORT_3_UNITS X Y ONE X1 Y1 TWO X2 Y2 THREE X3 Y3
{ANIMATE_UNIT id={ONE} pre_teleport {ANIMATE id={TWO} pre_teleport {ANIMATE id={THREE} pre_teleport ()}}}...
teleportees
if none is supplied):Code: Select all
#define TELEPORT_MULTI
#arg TELEPORTEES
teleportees#endarg
[for] # Or [foreach] if array contents are not changed?
array={TELEPORTEES} # Should just drop through if array empty (length=0)
[do]
... insert magic here ...
Cheers!
-- Spannerbag
Re: Teleport Function
I don't know!Spannerbag wrote: ↑March 20th, 2025, 9:35 am Would it be possible to generalise the macro to accept an array?

- Spannerbag
- Posts: 759
- Joined: December 18th, 2016, 6:14 pm
- Location: Yes
Re: Teleport Function
Fair enough! 
Cheers!
-- Spannerbag

Cheers!
-- Spannerbag