Spawn as blueprint based on Enum value

Currently I have playerstate set an Enum to decide team. This is done through Gamemode on join server. I’d like to use two separate player blueprints based on this.

I can swing using c++ in the same respawn code curious if anyone can give me some hints

Hi Inph1del,

Depending on how symmetrical your teams are, it might be easier to use one blueprint, tell the spawned instances which team they are on, and let the player blueprint/code handle the differences.

If the teams are distinct enough that you really need separate blueprints, there are a number of possible solutions:

If the teams are fairly static (you wont be adding teams often or changing them very drastically), you can simply add a couple TSubclassOf variables to the class that is spawning the players (I’ve not been into that area much yet) and spawning the appropriate one for the different teams (e.g. via a switch). You’d still need to set the variables to the proper player blueprints in the blueprint of the “spawner” class. This would be quick, but would require updating the code when you add teams or make sweeping changes to them or the player blueprints. it might look something like this (inserting your teams in place of the Red and Blue stuff):

In your Game Mode *.h:

// The player class for players on the Red team
UPROPERTY(VisibleAnywhere, Category="Teams")
TSubclassOf<APlayer> RedTeamPlayer;

// The player class for players on the Blue team
UPROPERTY(VisibleAnywhere, Category="Teams")
TSubclassOf<APlayer> BlueTeamPlayer;

In the spawning code:

switch (PlayerTeam)
{
case TeamRed:
    // The player is on the Red team, spawn a Red player
    PlayerInstance = ...; // Use the RedTeamPlayer variable in the normal spawning code
    break;
case TeamBlue:
    // The player is on the Blue team, spawn a Blue player
    PlayerInstance = ...; // Use the BlueTeamPlayer variable in the normal spawning code
    break;
}

If you want something more permanent and dynamic, you might look at using a structure to specify which player blueprint goes with which team. I’d think it would be composed of one of your enums and a TSubclassOf for the player blueprint. You could have a TArray of them in the “spawner” class so you could update it via blueprints at any point. Then, when spawning (or respawning) a player, you could iterate over the array to find the right blueprint to spawn. Or, if you’re re/spawning players really fast, you might consider setting up a hash (probably based on the previously mentioned array) to map from the team enum values to the player blueprints. I’m happy to provide an example of this if you like, though I suspect it’s more involved than you need.

In any case, please let me know if this helps. Thanks!

I’d love to see an example. Sounds like what I’m looking for, I’m essentially changing mesh material at the moment but this might include things attached to more then just the character mesh. Such as color of effects etc.

Hi Inph1del,

Sorry for the delay – I had to learn a few things to put the example together. :slight_smile:

Attached is an example project that does what I was thinking of:

  • The MyPlayerController class has a EPlayerTeam enum and exposes a CurrentTeam variable of that type.
  • The MyGameMode class has a FDefaultTeamBlueprint struct that contains a EPlayerTeam and a TSubclassOf. This is the information needed to tell what pawn (blueprint) a single team should use. The class also exposes a TArray of these structures so blueprints can fill in the appropriate data.
  • The MyGameMode class also has a private TMap> to use when determining the proper pawn (blueprint) to spawn for a given player.
  • During the PostLogin step of client connection, the GameMode looks at the new PlayerController and uses its CurrentTeam (if it has one) to spawn the appropriate type of pawn and make the 'Controller possess it.
  • Last, the blueprints: There are 4 player blueprints (a “base” player and 3 that derive from it – Freelance, Red Team, and Blue Team). The MyPlayerController_BP derives from the MyPlayerController class and specifies which team they are on. The MyGameMode_BP derives from the MyGameMode class and specifies which team uses which blueprint in the DefaultTeamBlueprints list.

For debugging/testing, the MyGameMode_BP uses the MyPlayerController_BP as the player controller. You can edit the CurrentTeam variable on the MyPlayerController_BP to change which team the player is on. You may also need to check the “Run Dedicated Server” box in the “Play” drop-down in order to see the effects.

Obviously, the places I chose for the various actions may not fit your situation, so feel free to move the code around as needed.

Does this help more?

Sorry for the late response, really looking forward to checking this out today. Probably best response I’ve gotten on here to say appreciate the time you’ve taken. I’ll update you later!