Set default pawn (playable character) after char select screen?


I’ve done some searching but cant seem to find an answer to this specifically, which maybe means im doing it wrong.
I have a parent class called MyChar
Then 4 child blue prints with different names, like hero1, hero2 etc… the only differences between them at this point is the meshes. I can manually set one of the child blue prints in the settings on game mode in the editor and that works fine. but:

I have a menu, with 4 boxes, each represents one of the 4 playable characters mentioned up top(tps game).
The menu is on its own Level when the game starts(level BP). Black background with 4 boxes and mouse cursor input is loaded
OnClick of a character, it casts to a my game mode function, and sets the Default Pawn Class to one of my 4 character blueprints (depending on which box they clicked.)
Then it Opens the first game level.

Everything works just fine except it never changes the mesh, meaning it never changes the Default Pawn Class… … it always sets to “MyChar” …

Should I be trying to do this a different way? I don’t want to just change the mesh, because eventually each char would have unique abilities etc.

Thanks for any help

You can spawn Pawn manually and possess it with player controller. Default Pawn Class its just a autospawn on start, you dont need it if your pawn created in a middle game.

I understand what you mean, I don’t know where to do which thing though.

I tried:
In my CharSelect widget on click, casting to game mode… then running a custom function in game mode

the game mode function is spawn actor from class(the chosen char) and then cast to the the char selected, and plug that into possess as the pawn.

But its not working.

The object of the cast is connected to return value of the spawn.

It seems when i run the game from editor. the cast in CharSelect fails.

When I run in standalone… it succeeds. not sure what thats about.

just did some local testing… the character wasn’t passing through, which is obvious now on the spawn actor title being blank. so i can connect the return value direct to in pawn… and that works. (on event begin play)

Going to try and make the onclick do it now

Ok here is how im doing it now.
Everything is working except I can’t seem to make the Onclick of the Character box do anything in my function on game mode. The cast to game mode fails every time.

If I manually set the Char integer to 0-4… it works… but I cant get the onclick to set that . No matter what I put in the SET on the widget, it always loads char 0

A GameMode Actor is instantiated each
time a level is initialized for play
via the UGameEngine::LoadMap()
function. The gametype this Actor
defines will be used for the duration
of the level.
Game Mode and Game State | Unreal Engine Documentation

I not sure 100%, but afaik each level have their own GameMode. You cant pass datas between level throught gamemode actor.
You mus use GameInstance instead.

hmmm i see… i dont know how to use game instance… or pass variables through it from one level to another.

I guess I can try just moving the char select stuff to the same map out of the way somewhere… seems like not the best way to do it though.

Its happens becauze your GameState instantiated after “Char integer” set. You cant pass datas between levels using GameMode because when level opened - its instaniate their own new GameState actor.
There are another class for such purpose, GameInstance. Try use it

I tried… and I can cast to game instance, it doesn’t seem to fail. but then what do I do?

Theres no blueprint editor for gameinstance. I can’t set a variable in there… I’m not really sure where to go from there.

I’ll try and create a custom BP from the original game instance… and try that way.

Create own BP class derived from Game Instance. Add some vars in it. Set it inside old level before new opened, and read it in new after its opened.

Dont forget set yours BP Game Instance in project Settings->Map&modes

Yea that worked.

  • Created custom game instance BP off the main GameInstance class
  • Set it in Maps/Modes Section of Project Settings
  • Added a Variable, Char Selection
  • CharSelect Widget casts to Custom GameInstanceBP and sets the CharSelection variable to a number, 0-3 depending on the character button clicked, then it loads the next level
  • In the Level BluePrint of said “next level”, I cast to custom GameInstanceBP again and GET the CharSelection variable value, pass it into intSwitch and spawn the corresponding actor from the event begin node in the level blue print

It took me awhile to get this to work because I didn’t know about the bug that doesnt let you use custom events in the level editor. I thought I couldn’t so I tried it ten different ways in gameinstance and thirdpersonbp. Til I found a thread about the bug preventing custom events in levelBP if you’ve opened another levelBP somewhere. Work around was to reopen project and only open levelBP you want to edit. Found that in another thread.

Thanks a lot Detech !
Very much appreciate it-

The characters can’t see each other in the game now though (multiplayer). Before, all three windows would spawn same character and they’d interact with each other (shoot and kill each other for instance). Now after character select screen, they all spawn with the right character, but can’t see each other.

New characters have same checks on Replication property panel as old?

Yeah… replcate movement etc is all there… they all inherited all settings from the parent which was the default originally… that was working. I tried loading the parent just to see with this method… and still cant see each other.

everything should be replicated fine. I get confused with when and how to set up the multiplayer events though.

I tried the custom events as run on server and multicast… both didnt make a diff

Here is current setup

In this screenshot you can see that at the char select screen, all three players are in there… and they see each other… and can kill each other.

If I take one player and click on a name box… the char disappears from the MP session and loads in the next level… repeating that has same results but they cant see each other after the spawn - possess

Why you replicate spawn event to client? Pawns (or characters) must be spawn on server and possess on server. Server is a original world. Client autoreplicate pawn from server if its checked “bReplicate”.

All actor spawns on client exist only on that client. Server never accept pawn from client.

I think it must be some like that:
Scene loading:

  1. When level loaded
  2. Server spawn character for every player controller and possess it
  3. Clients autospawn same characted (copies) because character actor mark
    as “replicated”

Moving, jumping, firing etc

  1. When something happens on client (like character moving or…)
  2. Client send info on server (call server event like ‘character location (new location)’)
  3. Server set world state as info says (fe: set character location as client say)
  4. Server send info about changes to all clients (like multicast)

If its a character movement - you dont need 4, because server do that itself if “bReplicateMovement = true”

Again. The only true world state is a server state. All actor (except local cosmetics) must spawn on server and they automatically appear on a clients because mark as “replicated” and client try to copy all data marked as replicated from server to itself.

If some client need to change something global (like change own position, spawn the bullet, etc) - its must do it on server.

All changes on client stay on client. Its cosmetic and server dont care about that changes until client ask him do that changes through event called on server

I agree with what you’re saying.

It doesn’t seem to matter what way I do it. I set it all to run on server… and it still spawns everyone in separate versions of the same map. It’s strange… because when I run around with one character it affects the lighting/shadows on the others even though they cant see each other.

I’ve tried it like 20 different ways. I wonder if theres something else I’m missing.

Thanks a lot for your help so far. I think am almost there.

I have all of my movements, animations, shooting, gun equipment etc all replicated already. That I know works. If I just hit play on the map with a default pawn class manualyl set to one of the characters, the game loads all three with the same character, but all movements and actions are seeable by all. They can shoot and damage each other.

Its something about the spawn, possess or loading a new level thats not connecting them I guess

Gonna mark this answered and create a new one for this issue.