Attaching a player to a Player Controller (solved)

I get this error

CreateWidget cannot be used on Player Controller with no attached player.

And I cannot figure out how to attach a player to a Player Controller. The issue is that I need to use “Spawn Actor From Class” to get the player controller, since at the beginning of the game the server and the client are completely different.

I made the player controller class, made a blueprint of it, and now I cannot add widgets to it. It all seemed straightforward but I don’t really understand why it doesn’t let me add widgets.

I use [WIDGET]->AddToViewport(); to add the widget, and it worked before, but I had to change it because it would show on both the server and the client.

Edit: What worked in the end was adding a player manually in the scene and spawning the second player with the second controller set in the gamemode.

You should not spawn them yourself, PlayerController is created automatically for each player and you can use simple ”Get Player Controller" or to make things a lot easier and cleaner place HUD initiation code in to PlayerController it self, then you don’t need to get controller at all and leave pin as it is (or use “Self” node). There other ways to get PlayerController, some classes holds reference to related controller, for example Pawn will have controller that possess it and you can get it via “Get Controller” (it a function of Pawn class).

If you do it in PlayerController this should work in multiplayer too as local PlayerController stays in local computer memoery.

You can use the Create Player node in blueprints to create a new player along with its controller.

That’s interesting, but I’m not sure what happens when I try it. The error persists but there are some changes in the viewport, and only in the viewport of the client, which is a good thing. However, I don’t understand the changes.

104238-knipsel.png

How do I tell the player which player controller to use?

Create Player spawns the player controller defined in your gamemode. As long as you go to your gamemode and choose BP_MagicPlayerController as your player controller, just do “Create Player” and then use the “Get Player Controller” node to get the player controller that was created. If you use Controlled ID -1, then the first player controller will be numbered 0, the second will be 1, and so forth.

The problem is that for this multiplayer game I need player 1 and 2 to have different player controllers, and from the gamemode I can only choose one.

I suggest putting your player-specific logic into the pawns instead. It’s simple to switch which pawn your controller controls (and each different pawn can have very different behavior). Your controller should just pass input from keyboard/mouse/gamepad down into your pawns.

Dangit, whenever I comment it clears the answer acceptance. You’ll have to accept the answer again.