Any way to ensure teleport success?

Ask about ACS, DECORATE, ZScript, or any other scripting questions here!

Moderator: GZDoom Developers

Forum rules
Before asking on how to use a ZDoom feature, read the ZDoom wiki first. If you still don't understand how to use a feature, then ask here.

Please bear in mind that the people helping you do not automatically know how much you know. You may be asked to upload your project file to look at. Don't be afraid to ask questions about what things mean, but also please be patient with the people trying to help you. (And helpers, please be patient with the person you're trying to help!)
Post Reply
User avatar
Enjay
 
 
Posts: 26970
Joined: Tue Jul 15, 2003 4:58 pm
Location: Scotland
Contact:

Any way to ensure teleport success?

Post by Enjay »

I have a situation where I am teleporting a couple of boss enemies into an arena, I am doing this via TeleportOther.

It works fine but I worry that the teleport might fail. I have done my best to ensure that can't happen (it's highly unlikely - perhaps even impossible that it will fail with the way that I have set up the map), but I was wondering if there was any way to check whether the teleport has been successful or not and, if not keep trying until it is?

Thanks.
User avatar
MartinHowe
Posts: 2061
Joined: Mon Aug 11, 2003 1:50 pm
Preferred Pronouns: He/Him
Location: East Suffolk (UK)

Re: Any way to ensure teleport success?

Post by MartinHowe »

Is this from a monster closet (Vanilla/Limit-Removing) or is ZScript available? And do these monsters already have context or could they be spawned?
User avatar
Enjay
 
 
Posts: 26970
Joined: Tue Jul 15, 2003 4:58 pm
Location: Scotland
Contact:

Re: Any way to ensure teleport success?

Post by Enjay »

They already exist on the map, and the player may have interacted with them (and damaged them a bit) before the teleport happens. They appear, the player does stuff, they go away, and then they come back at a later point in a different area of the map.

ZScript is a possibility.
User avatar
MartinHowe
Posts: 2061
Joined: Mon Aug 11, 2003 1:50 pm
Preferred Pronouns: He/Him
Location: East Suffolk (UK)

Re: Any way to ensure teleport success?

Post by MartinHowe »

Well, you know you can spawn an item and check if it was spawned? I'm thinking something like this:
  • Define an actor the same size as the one to be teleported.
  • Make it invisible but solid
  • Use SpawnItemEx to spawn it, checking if it was spawned, until it is
  • Make the monster non-solid and invisible
  • Teleport it to the desired area using the co-ordinates of the other actor
  • Make the original monster solid again
  • Destroy() the other actor
  • Spawn a teleport flash at the same location (these are not solid so should be OK)
  • Make the monster visible again
It's a bit hacky but should work. Or you could have several of these fake actors already in the arena marked +NOINTERACTION and shortly before the teleport, remove the nointeraction flag and make it solid on one that has nothing else in the same place; for a short time, there will appear to be a part of the map that monsters are avoiding, but hopefully the player won't notice in the confusion.

This is what I use for repeated attempted spawns:

Code: Select all

    const SPAWNCATS_Y_SPREAD = 256;

    void SpawnCats(actor player, array<BlackCatToken> catsToSpawn)
    {
        uint count = catsToSpawn.Size();

        if (!count)
        {
            return;
        }

        int yofs = -(SPAWNCATS_Y_SPREAD / 2);
        int ystep = SPAWNCATS_Y_SPREAD / count;

        double a = player.angle;
        double s = sin(a);
        double c = cos(a);
        double z = player.pos.z;

        // Offset code adapted from that in A_SpawnItemEx

        for (int cat = 0 ; cat < count ; cat++)
        {
            BlackCatToken catToken = catsToSpawn[cat];

            for (int attempt = 0; attempt < 32; attempt++)
            {
                int xofs = 32 * attempt;

    			vector3 newpos = player.Vec2OffsetZ(xofs * c + yofs * s, xofs * s - yofs * c, z);
                if (Level.IsPointInLevel(newpos))
                {
                    bool wasSpawned;
                    actor whatWasSpawned;
                    [wasSpawned, whatWasSpawned] = player.A_SpawnItemEx(catToken.catClass, xofs, yofs, 0);
                    if (wasSpawned && whatWasSpawned)
                    {
                        BlackCat aCat = BlackCat(whatWasSpawned);
                        aCat.StartHealth = catToken.startHealth;
                        aCat.Health = catToken.currentHealth;
                        aCat.FriendPlayer = catToken.FriendPlayer;
                        aCat.bSpawnWithTeleportFog = true;
                        aCat.bFollowPlayerToMap = catToken.followToMap;
                        aCat.bFollowPlayerInHub = catToken.followInHub;
                        aCat.bFollowPlayerToHub = catToken.followToHub;
                        break;
                    }
                }
            }

            yofs = yofs + ystep;
        }
    }
User avatar
Enjay
 
 
Posts: 26970
Joined: Tue Jul 15, 2003 4:58 pm
Location: Scotland
Contact:

Re: Any way to ensure teleport success?

Post by Enjay »

OK, yes, thanks. I can work with and adapt that to my needs. :thumb:
Blue Shadow
Posts: 5040
Joined: Sun Nov 14, 2010 12:59 am

Re: Any way to ensure teleport success?

Post by Blue Shadow »

There's an easier and simpler way.

Code: Select all

script 1 (void)
{
	while (!TeleportOther(...))
	{
		Delay(35);
	}
}
The script will attempt to teleport the monster. If the teleportation fails, it'll try again every second until it succeeds.
User avatar
Enjay
 
 
Posts: 26970
Joined: Tue Jul 15, 2003 4:58 pm
Location: Scotland
Contact:

Re: Any way to ensure teleport success?

Post by Enjay »

Oh, I didn't realise that you could effectively check for TeleportOther success like that (and presumably other teleports). That is simpler. Thanks. :D
User avatar
MartinHowe
Posts: 2061
Joined: Mon Aug 11, 2003 1:50 pm
Preferred Pronouns: He/Him
Location: East Suffolk (UK)

Re: Any way to ensure teleport success?

Post by MartinHowe »

Enjay wrote: Sun Jun 29, 2025 5:59 pm Oh, I didn't realise that you could effectively check for TeleportOther success like that (and presumably other teleports).
Nor did I; the Wiki is out of date; it reports the special as void rather than bool.
User avatar
Enjay
 
 
Posts: 26970
Joined: Tue Jul 15, 2003 4:58 pm
Location: Scotland
Contact:

Re: Any way to ensure teleport success?

Post by Enjay »

Yep, same for me. I checked the Wiki and when I didn't see any way of checking success listed, I started this thread.
Post Reply

Return to “Scripting”