Teleport Function

Discussion of Lua and LuaWML support, development, and ideas.

Moderator: Forum Moderators

Post Reply
User avatar
Saizo-Luz
Posts: 83
Joined: June 14th, 2024, 12:50 pm
Location: High Heavens

Teleport Function

Post by Saizo-Luz »

About the [teleport] function.

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
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? :hmm:
User avatar
Ravana
Forum Moderator
Posts: 3313
Joined: January 29th, 2012, 12:49 am
Location: Estonia
Contact:

Re: Teleport Function

Post by Ravana »

Loop wesnoth.units.find_on_map(filter)[1] instead of taking first.
User avatar
Saizo-Luz
Posts: 83
Joined: June 14th, 2024, 12:50 pm
Location: High Heavens

Re: Teleport Function

Post by Saizo-Luz »

Ravana wrote: November 27th, 2024, 11:43 pm Loop wesnoth.units.find_on_map(filter)[1] instead of taking first.
:? I don't know how to do this kind of codes... I just do WML. I tried to do it even without understanding, but it went wrong. Can you do it for me?

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... :| Is there any code master here who can do this?
Last edited by Saizo-Luz on November 28th, 2024, 1:54 pm, edited 1 time in total.
User avatar
Celtic_Minstrel
Developer
Posts: 2371
Joined: August 3rd, 2012, 11:26 pm
Location: Canada
Contact:

Re: Teleport Function

Post by Celtic_Minstrel »

If all they are doing is teleporting from point A to point B, you can probably use [animate_unit].
  1. Animate all the units with flag=pre_teleport.
  2. Move each unit to the destination space, eg with [modify_unit]. I think this has to be done one unit at a time.
  3. Animate all the units with flag=post_teleport.
Author of The Black Cross of Aleron campaign and Default++ era.
Former maintainer of Steelhive.
User avatar
Saizo-Luz
Posts: 83
Joined: June 14th, 2024, 12:50 pm
Location: High Heavens

Re: Teleport Function

Post by Saizo-Luz »

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].
  1. Animate all the units with flag=pre_teleport.
  2. Move each unit to the destination space, eg with [modify_unit]. I think this has to be done one unit at a time.
  3. Animate all the units with flag=post_teleport.
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.
User avatar
Saizo-Luz
Posts: 83
Joined: June 14th, 2024, 12:50 pm
Location: High Heavens

Re: Teleport Function

Post by Saizo-Luz »

:D I HAVE A MAJOR ANNOUNCEMENT TO MAKE! :D

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. :geek:

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

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
And THAT was the problem! The screen is redraw and sometimes the units appear before the animation. :idea: So I copied the code and removed the [redraw]...

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
And it worked! ^_^ Smooth like the teleport tag. Take a look at the whole macro using the [el_unhide_unit]... (I used another macros)

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
:eng: We need to scroll before unhiding them; otherwise the scroll will redraw the screen.
User avatar
Spannerbag
Posts: 759
Joined: December 18th, 2016, 6:14 pm
Location: Yes

Re: Teleport Function

Post by Spannerbag »

Not tried your code but looks good. 8)

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 ()}}}...
Have something like this (the macro looks for an array called 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 ...
Just a thought.

Cheers!
-- Spannerbag
SP Campaigns: After EI (v1.14) Leafsea Burning (v1.18, v1.16)
I suspect the universe is simpler than we think and stranger than we can know.
Also, I fear that beyond a certain point more intelligence does not necessarily benefit a species...
User avatar
Saizo-Luz
Posts: 83
Joined: June 14th, 2024, 12:50 pm
Location: High Heavens

Re: Teleport Function

Post by Saizo-Luz »

Spannerbag wrote: March 20th, 2025, 9:35 am Would it be possible to generalise the macro to accept an array?
I don't know! :hmm: I'm not a programmer, I don't understand much about this! I created five macros for that: teleport 2, 3, 4 and 5 units. I won't need to teleport more than that, so...
User avatar
Spannerbag
Posts: 759
Joined: December 18th, 2016, 6:14 pm
Location: Yes

Re: Teleport Function

Post by Spannerbag »

Fair enough! :)
Cheers!
-- Spannerbag
SP Campaigns: After EI (v1.14) Leafsea Burning (v1.18, v1.16)
I suspect the universe is simpler than we think and stranger than we can know.
Also, I fear that beyond a certain point more intelligence does not necessarily benefit a species...
Post Reply