saving an agent to use it later

My issue/question is that VehicleSpawnedEvent does not give an Agent, of course. However, I need the agent to be able to set it back into the car. How would I be able to reference the agent?

The script needs to work for 5 players

using { /Fortnite.com/Devices }
using { /Verse.org/Simulation }
using { /UnrealEngine.com/Temporary/Diagnostics }

player_car_handler := class(creative_device):

    @editable SuvSpawner : vehicle_spawner_valet_suv_device = vehicle_spawner_valet_suv_device{}

    OnBegin<override>()<suspends>:void=
        SuvSpawner.AgentExitsVehicleEvent.Subscribe(RespawnCar)
        SuvSpawner.VehicleSpawnedEvent.Subscribe(PutPlayerIntoCar)

    RespawnCar(Agent : agent): void =
        SuvSpawner.RespawnVehicle
    
    PutPlayerIntoCar(Agent : agent): void =
        SuvSpawner.AssignDriver(Agent)

Seems like you need to use fort_vehicle.GetPassengers()

Thanks for the reply, what does it return and do you have the documentation about this function?

Here you go:

Essentially it returns all the fort_characters in an array.

Here’s an example code that should get the fort characters from a vehicle, goes through the entire list and checks if they have a valid agent associated and if true runs the .Respawn() function on them

for(FortChar:MyCar.GetPassengers()):
      if(Agent:=FortChar.GetAgent[]):
          Agent.Respawn(vector3{},rotation{})
1 Like

I don’t think this will work in this case, everytime the car respawns it should put the player into the car

Well unfortunately you can’t do that with fort_vehicle, only the device that spawned it itself has functions to assign players inside cars

1 Like

SomeVehicleSpawner.AgentEntersVehicleEvent will return the agent, then keep this agent and use SomeVehicleSpawner.AssignDriver(Agent)

To explain further, in my project, I have 5 cars. The player needs to be in the car, always. They select a vehicle by going Into a class selector, and from that point forward the player needs to stay in the car. The car can be destroyed, so every time it respawns, it needs to put the player back into the car he selected.

I think for me the solution would be to assign the player to an ID, that ID is also linked to the vehicle. Then I would always be able to call the Player from anywhere.

1 Like

In that case you should use a CustomPlayer class that you store in a map with
var CustomPlayers : [agent]CustomPlayer = map{}

Have a pool of vehicle spawners so you have enough for each player, and when players are added assign them a vehicle spawner from the pool that you store in your CustomPlayer class, along with any other assets or info you need to know for each character. You can subscribe to AgentEntersVehicleEvent, AgentExitsVehicleEvent, and Destroyed event to run logic on these events.

Learning how to handle custom data per player is a really important part of Verse, here is a tutorial that goes over it very well:
https://www.youtube.com/watch?v=TTP4qviK2eM&ab_channel=MangoVoxel

1 Like

Hi, I have made a custom player/game manager script according to the video, but I still don’t know how I can get the player from the map.

What I’d need is a way to call a function that returns the player
If the players are stored in an array I could Assign every player to the array[0-4], and so calling the function would require an int that returns the player accordingly.

Is this possible? Thanks!

Not sure what you mean by get the player from the map. If you can share your code and what function you need the player for I can take a look. The vehicle events like AgentEntersVehicle and AgentExitsVehicle return an agent which is interchangeable with player, so you should be able to get what you need from those.

1 Like

What I need, is to be able to put the agent into the car (SuvSpawner.AssignDriver(Agent) when the car respawns (VehicleSpawnedEvent). But when I call AssignDriver there is no agent, so I need to get the agent from somewhere.

What I think could be a solution is to store the agent into an array based on what car it got into, so let’s say you choose car 3 out of 5, it will store you in in array[2] (since the index starts at 0)

Would this be possible or do you have another solution in mind?

The code I’m referring to:

using { /Fortnite.com/Devices }
using { /Verse.org/Simulation }
using { /UnrealEngine.com/Temporary/Diagnostics }

player_car_handler := class(creative_device):

    @editable SuvSpawner : vehicle_spawner_valet_suv_device = vehicle_spawner_valet_suv_device{}

    OnBegin<override>()<suspends>:void=
        SuvSpawner.AgentExitsVehicleEvent.Subscribe(RespawnCar)
        SuvSpawner.VehicleSpawnedEvent.Subscribe(PutPlayerIntoCar) # Puts player into car when spawned

    RespawnCar(Agent : agent): void =
        SuvSpawner.RespawnVehicle
    
    PutPlayerIntoCar(): void =
        SuvSpawner.AssignDriver(Agent) # Gives error since agent is not defined

First off if you want 5 players you’ll need 5 suv spawners as I suggested above, have a pool @editable SUVPool : []vehicle_spawner_valet_suv_device = array{}

As I also said above you’ll also need the CustomPlayer data class and an [agent]CustomPlayers map to track which is assigned to what player.
In my projects I have a Verse device Asset_Pool that stores all the devices needed in arrays like this, and then you have a function for CheckOut(id : int) and Return(id : int) that tracks the index of your array (if it is checked out) and assigns the asset to the CustomPlayer data. Here is what my customplayer looks like:

CustomPlayer:= class():
var Assets : PlayerAssets = PlayerAssets{}
var AssetsID : int = 0
FC : fort_character
var vehicle : ?vehicle_spawner_device = false

Once you figure out how to assign data and devices to players through a map you’ll be able to do all the things you’re looking for and more, it is a crucial part of learning Verse.

Also, as the VehicleSpawnedEvent does not pass an agent, you’ll want to call SuvSpawner.Respawn() manually in Verse somewhere you already have the Agent reference.

1 Like

Thank you! This will work for me.

I will handle the respawning of the car myself, if the player dies it will respawn the car and put the player into the car again.

But one more question if you don’t mind :sweat_smile:, do you know how I can detect if the player has died? The elim manager only sends an event when another agent has killed the player. But in my case the player dies because of a damage barrier or the expotion of the car, so there is no other agent involved.

I tried looking at this function, but I don’t really know how I could use it. One thing I need is the agent that has died.

Thanks again for your last response!

Another thing is that DestroyVehicle and RespawnVehicle from class vehicle_spawner_device do not work for me. Only AssignDriver does

My code (Modified for example):

using { /Fortnite.com/Devices }
using { /Verse.org/Simulation }
using { /UnrealEngine.com/Temporary/Diagnostics }

player_car_handler := class(creative_device):

    @editable SuvSpawner : vehicle_spawner_valet_suv_device = vehicle_spawner_valet_suv_device{} 
    @editable TestTrigger : trigger_device = trigger_device{}

    OnBegin<override>()<suspends>:void=
        SuvSpawner.AgentExitsVehicleEvent.Subscribe(RespawnCar)
        TestTrigger.TriggeredEvent.Subscribe(PutPlayerIntoCar)

    RespawnCar(Agent : agent): void =
        Print("Respawning Vehicle")
        SuvSpawner.DestroyVehicle # This does not work
        SuvSpawner.RespawnVehicle # Also does not work
    
    PutPlayerIntoCar(Agent : ?agent): void =
        Print("Assing Player to car test")
        if (TestAgent := Agent?):
            SuvSpawner.AssignDriver(TestAgent) # Does work

because you’re not actually calling them

1 Like

Ah thanks, had to add ()

1 Like

Would you know the answer for this?

You can take the Agent and do .GetFortCharacter, and with the fort character you can subscribe to EliminatedEvent. This returns an elimResult, then you can get result.EliminatedCharacter to get the character who died. The EliminatingCharacter is an optional so if a barrier killed the player it will return false

2 Likes

Thank you bro

1 Like