I built a device to take in an array of ufo vehicle spawners and when a player activates a conditional button they get assigned to one of the spawners and it gets marked in use using a map. Once the vehicle is destroyed, it gets marked back to not in use. I only have 3 spawners for testing but I will scale to 16 for all players on the map s at any given point any players can spawn a ufo without knocking another player out of theirs.
Everything works fine at first, however the issue is that once a driver gets assigned to a vehicle and that vehicle gets destroyed, after they respawn and try to spawn another vehicle, the code shows the last one they were in is available and says they are assigned as a driver but the vehicle spawns without them inside. Therefore the vehicle never gets destroyed and the button has to be hit again which then spawns them in the next available vehicle in the array.Which again works just fine, until THAT vehicle is destroyed and they move to the next one and so on and so forth eventually exhausting the array and making no more vehicles available. I’ve been trying all sorts of combinations and the problem persists.
Is there some flag I don’t know about that prevents them from respawning into a vehicle they previously occupied?
Also is this going to work with multiple players? It’s my first time doing something of this nature in Verse. Thanks!
@editable ConditionalButton : conditional_button_device = conditional_button_device{}
@editable VehicleSpawners : []vehicle_spawner_ufo_device = array{}
var tempSpawner : vehicle_spawner_ufo_device = vehicle_spawner_ufo_device{}
var vehicle_index : int = 0
var tempIndex : int = 0
#Map value of 0 indicates not in use, 1 indicates in use
var VehicleInUse : [int]int = map{
0=> 0
1=> 0
2=> 0}
OnBegin<override>():void=
# Subscribes to the InteractedEvent of the ConditionalButton.
ConditionalButton.ActivatedEvent.Subscribe(HandleButtonInteraction)
# Print("{VehicleInUse.Length}")
# Function to handle the button interaction.
HandleButtonInteraction(Agent : agent):void=
SpawnPlayerInAvailableVehicle(Agent)
# Function to find an available vehicle spawner and spawn the player.
SpawnPlayerInAvailableVehicle(Agent : agent):void=
# Iterate through the vehicle spawners to find an available one.
Print("About to enter the loop")
#Show current use state of all vehicles
for (Key->Value : VehicleInUse):
Print("{Value} value in VehicleInUse at key {Key}")
#Set vhicle index counter back to start of array
set vehicle_index = 0
#Loop through the vehicle spawners to find an available one
for (VehicleSpawner : VehicleSpawners):
Print("Checking index. {vehicle_index}")
if (VehicleInUse[vehicle_index] = 0):
VehicleSpawner.Enable()
VehicleSpawner.AssignDriver(Agent)
Print("Assigned driver to vehicle spawner {vehicle_index}")
#Mark the vehicle as in use.
if (set VehicleInUse[vehicle_index] = 1):
#Spawn function to listen for vehicle destroyed event
spawn:
IfVehicleDestroyed(VehicleSpawner, vehicle_index)
return
else:
if (VehicleInUse[vehicle_index] = 1):
set vehicle_index += 1
# Subscribe to the Vehicle DestroyedEvent
IfVehicleDestroyed(Spawner : vehicle_spawner_ufo_device, Index : int)<suspends>:void=
Print("Waiting for destroyed event for index, {Index}")
Spawner.DestroyedEvent.Subscribe(HandleVehicleDestroyed)
set tempIndex = Index
set tempSpawner = Spawner
#Mark the vehicle as not in use
HandleVehicleDestroyed():void=
Print("Setting back to false for index {tempIndex}")
if (set VehicleInUse[tempIndex] = 0):
tempSpawner.Disable()