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

1 Like

Thank you so much for the tips man :star_struck:

Sorry for the delay i was on a few days off.

About #1 what should i do instead ? Create a list with all the players and update it ?
About #2 so the Team_Manage.TeleporterNum won’t increment above the array length
About #3 OK, just looked up what hard coded indexes are, and got it :+1:t3:
About #4 & #5 Got it.
About #6 I will read more about it !
About #7 Same question as #1

Now i hate the evil fort_character :angry: :joy:

Make a player manager device. Put a map object in it. Put all your spawners in it (the device) as editables. (Make sure you add 1 more spawner than the maximum number of players.) Subscribe to player added, player removed, and player spawned events. When a player shows up (either by spawning in or the player added event), see if they are in the map and if they aren’t add them. When a player leaves (player removed event) find them in the map and remove them.

The map object is your “list” of players. Look them up using agent to get player, then use player as an index to get your “custom” player object that has all of the stuff you want to keep track of about the player. (You’ll need to make a unique class for that.)

There should be videos on YouTube about how to keep track of players (via custom player maps). Look up Warforge, Graeme Bull, and there are probably some others.

In regards to my comments about fort_character, if/when you find yourself using GetFortCharacter[] (for a valid reason, of course) - just make sure you use IsActive[] on the result to make sure it is actually valid.