possessing pawns with blueprints

I’m completely stuck on this and I haven’t been able to find a thread with a solution. When I spawn on a level I want to possess pawn 0 (easy). Then, using a button on the umg I want to posses another pawn on the level. Is this possible? it seems like it should be very easy to do but after 2 days I just don’t know.

This is doable. You need to define what pawn is pawn 1, pawn 2 etc though. If the order of pawns is not important you can do a GetAllActorsOfClass<Pawn> and pick a pawn from the returned list. Then make PlayerController 0 possess that pawn, assuming this is a single player game. if you have placed the pawn before in the level, you can use the Level blueprint to store pawn pointers to your custom GameState or a similar point from which your UMG widget can retrieve the list.

Thanks for the reply. I’m going to try that right now.

I guess my problem is getting the UMG to talk to the rest of my game. I’ve tried sending it over interfaces but I can’t target my menu. I also tried casting but I don’t know what object to feed it.

Well if you only have 2 pawns that your working with maybe you could do something like this(Just making this up off the top of my head)

When you click the button to posses a new pawn try this.

Button clicked -> cast to playerController (with PlayerCOntroller 0, or get owning player of the HUD as the input) -> get controlled pawn from playerController

-> get all actors of class (the pawn you’re using) then from the ForEachLoop, use a compare actors NotEqual node. (!=)

Compare the controlled pawn of the player to the one currently being looped. tie that to a branch…

IF True (meaning they are unequal/not the same pawn) then break the loop and unpossess the current pawn of the player controller and posses the new pawn.

IF False you could print something or what ever you need.

Again, this will only really work if you are switching between two pawns. BUT you could do more, if you just do some sort of check and get the distance from other pawns and if it is in range control it. But that’ll come with its own bugs. haha

goodLuck!

I managed to get it to work by initializing an array of all the characters in the level blueprint and then casting that to the player controller. In the player controller I was able to add an interface that received messages from the UMG and would possess the appropriate pawn.

I was about to suggest that solution. Seems like the best way to do it from what I can figure. It is a good way of keeping things modular which is definitely what you should aim for.