HI!
I have a need to get a component in GameMode (mostly to create widgets at different events), but I somehow think it doesn’t look right.
The screenshot shows what I mean:
How inconsiderate is it that I use GetPlayerCharacter in the GameMode BeginPlay?
And how do you fix it?
Thx!
In gamemode blueprint you can override function/event OnRestartPlayer, which is called right after spawning pawn for each player/controller. It is a generic event for both players and AIs, so it passes a generic Controller as parameter. You should first cast to PlayerController to check that it is the player, then use Controller->GetControlledPawn to access the Pawn, then get your component from there.
1 Like
Thank you! It really works. Just a little clarification on whether there will be any overheads due to cast?
In terms of memory footprint no, PlayerController is a native class and native classes are all always loaded, always.
In terms of performance, eh.. It’s just a node. If you spawn hundreds of pawns per frame then sure, it may have an impact, although it would still be negligible compared to the overhead of pawns/controllers themselves.
Alternatively you can use Controller->IsPlayerController, but then you have to Branch, so that’s two nodes instead of one…
If you don’t use AIControllers, you can probably get rid of the check altogether.
1 Like
Ok, I understand, what’s the option to make a method in the gamemode interface and call it at the end of begin play character?
GameMode always come before Character (since character is spawned by gamemode) so you can just do that in Character’s BeginPlay → Get Game Mode → call your function
1 Like