Some players are not being teleported using Verse (teleport device)

hi guys… i have an issue.

it is a published map!

I use teleport devices to move players to specific locations. I loop through all the players and teleport each one right away, but sometimes one or two players don’t get teleported. What might be going wrong?

this is the code i used:

 set Team_Manage.TeleporterNum = 0

 for (Player : GetPlayspace().GetPlayers()):
      TeleportToTeamSelection(Player)
      set Team_Manage.TeleporterNum += 1

and now i want to test the code below but before i publish the changes i want to ask you guys if it is a known issue?

You’re incrementing TeleporterNum twice, so you’re probably ending up trying to fetch a teleporter that doesn’t exist in the array, also you might want to use this method to avoid fetching indexes out of array length

# Returns the remainder of `X/Y` as defined by Euclidean division, i.e.:
#  * `Mod[X,Y] = X - Quotient(X/Y)*Y`
#  * `0 <= Mod[X,Y] < Abs(Y)`
# Fails if `Y=0`.
Mod<native><public>(X:int, Y:int)<computes><decides>:int

I’m pretty new to Verse and programming in general, so I’m having a bit of trouble understanding the code you sent.

Here’s what I used when I ran into the issue

OnGame_Start_Timer_Success(Agent : ?agent) : void =

set Team_Manage.TeleporterNum = 0

for (Player : GetPlayspace().GetPlayers()):
     TeleportToTeamSelection(Player)
     set Team_Manage.TeleporterNum += 1

where do i incrementing TeleporterNum twice ? what am i missing ?

set Team_Manage.TeleporterNum = Mod[Team_Manage.TeleporterNum + 1, TeamSelectionTeleportersArray.Length]

This way you don’t overflow the index, search for modulo operator on internet

I looked it up, and I see now. This will cycle back to the first teleporter in the array once it hits the last one.
This is really useful. I can apply it to other things. Thanks!

Honestly, I still don’t quite see where the double increment is happening. I’ve got 16 teleporters in the map of max 16 players, yet even with only 4 players, sometimes one isn’t teleported so i dont know what to think. I’m more curious about why this is happening than focused on fixing it lol

1 Like

I think it’s just that you posted 2 different codes in your first post and I got confused :+1:

Ah, I see… that’s on me. I realize the post got a bit messy. The first code is what I’m currently using, while the second(the screen shot) is the one I plan to test out. The issue remains unclear though, because if it’s not double incrementing, then something else must be causing it.

1 Like

Where is TeleportToTeamSelection being called from?

In any case, you’ve got some assumptions there that you should not be doing. You gotta remember Verse is failable. A. You gotta always assume that everything might fail. Because it can and will. B. GetPlayspace() is a scary function that you better have a reason for using it. And you better know what you are doing with it. Maybe you do, maybe you don’t. I don’t know. C. You have an assumed += which goes back to A. I would rethink this blurb of code if is important to your game and replace it with a better function.

Thank for the response lantram

TeleportToTeamSelection is an array of teleporters that I defined at the head of the file.

I use GetPlayspace().GetPlayers() to get all the current players in order to teleport them to where i want, is it ok to use it like that ?

Regarding the += operation, I switched to what Lama suggested to ensure I always find a valid teleporter.

I changed:

set Team_Manage.TeleporterNum += 1

to this:

set Team_Manage.TeleporterNum = Mod[Team_Manage.TeleporterNum + 1, TeamSelectionTeleportersArray.Length]

Is there anything else I should pay attention to?

This is just me, so feel free to roast and/or ignore my advice.

#1. If you find yourself doing GetPlayspace().GetPlayers() in the middle of your game, then you already messed up

#2. That whole mod thing, yeah, super cool but … why?

#3. Don’t use hard coded indexes

#4. Assume everything will fail. Always. And write your code to handle those situations

#5. When you have something super important, make your code super paranoid in handling everything failing and responding appropriately

#6. Always remember that fort_character is evil. It will bite you. Never forget it, and then only use it knowing that

#7. And, call me crazy but you should have a player manager and should be setting it up with that evil fort_character like this:

GetPlayspace().PlayerAddedEvent().Subscribe(OnPlayerAdded)
GetPlayspace().PlayerRemovedEvent().Subscribe(OnPlayerRemoved)

That way you know the players. And you do not need to ask