Using a UMG button to select a character

Is it possible to select a character by using a button created within the UMG? I have 5 characters, all of whom are set out within the level when the game starts.

The idea is that the player will be able to select a character, move it, and then select another character to move, either to join the previous one, or go off in another direction. I could setup an option to click upon the character model and possess each character individually but this wouldn’t work if the other character(s) were out of view. By using a UMG button I would be able to select a character even if one is out of visible range.

I’m looking to create the game using touch input rather than a keyboard so my options for using a key select for a desired character are not an option.

I have built the buttons but I can’t seem to find an option to interact with my character models within the button’s graph/blueprint.

At present I’m making use of the TopDownController from the default templates which at this time controls just the default ‘PlayerStart’ character.

I have four test characters in the game at the moment which consists of a ‘PlayerStart’ node and three characters dropped into the scene. At the moment they are BulldozerCharacter (a repeat of the PlayerStart), Bulldozer2Character and Bulldozer3Character

I’m using the TopDownGameMode which references the BulldozerCharacter as the Default Pawn Class.

When the game is in ‘Play’ mode I have three buttons in the scene which are ‘Ch1’ (Character 1), Ch2 (Character 2) and Reset. These are only for test purposes - more would be used in the final layout.

As I say, my aim would be to have it so that the player taps/drags on the screen to move the character around at the time of playing the game but when they want to change character, they can tap on the button to select another.

This is to be aimed at young children so it has to be kept simple. I did initially plan to have it that the characters were tapped on but of course this doesn’t work when they go out of view of the other models. At the moment, the setup is that each character (vehicle) has its own camera on a springarm.

Any help would be greatly appreciated as I’m now at an impasse that is holding up the rest of my game’s development.

I’d create an actor variable (say “PlayerCharacter”) in the UMG widget you’re using to select the character to be controlled. Then whenever you create the widget from your player blueprint set “PlayerCharacter” to self. Then in your UMG widget OnClick event just unpossess “PlayerCharacter” and possess the new pawn as PlayerCharacter. I’ve not tried anything like that personally but I’d think it should work.

You have to set up your controller to possess the character you want. To talk to your controller bp from UMG, I would suggest using a BP interface. Create an interface and add it to your controller. You need to set up one function and set a varible you are going to use determine which character to select. I used an int since it is easy to work with an array.

Next, Create a variable in you Controller to hold the character variables. Also, set up the event from the interface to change possession.

I quickly dragged and dropped four players in the world and referenced them in the to the level bp, created an array, and sent it to the controller. I also created and added my widget to the viewport here. I would not do this stuff here in an actual game.

Finally, I created my widget with four buttons to select each character. I sent a message to the Controller interface with the int I wanted for each character.

Actually, the last thing I did was test it.

It worked!

-James

Thanks for this James, especially considering the time and effort involved to supply blueprint screenshots (they make it soooo much easier to follow). I can’t wait to try this out later once the kids are in bed and I can start working on this again :smiley:

No problem, didn’t feel like working on my stuff yesterday so I went through and tried to answer as many questions as I could. AS for the images, setting up BPs and taking screen shots is a lot quicker for me than trying to type all that out in an understandable way, and I can make sure that what I think is going to work does work; it helps me try new things.

Let me know if this does work out for you.

Hi James, Can I just check, the ‘Chars’ variable that was added to the TopDownController, what Variable Type was it? I changed it to an array but then suddenly realised that there was a whole load of options :confused:

Once again many thanks for your help on this :slight_smile:

Since you are putting Characters in it use a character array. Once you select the var you can type in the type you want in the search to find it easier

Hi James, sorry to keep pestering you on this one - so close to getting a working setup now but have realised that by trying to add unique characters (rather than dragging and dropping repeats of TopDownCharacter) doesn’t work within the array. Is there something. Is there a way to link unique character blueprints to the same array?

Once more - a big thanks for your help so far.

Yes! When you call “Make Array” its data type automatically gets set to the first thing you put in it. Instead, create a variable and set it to the base type of your actors. In the case below I used the Maximo Animation pack, I could have set the array to the base Maximo character, but instead I set it to “Character”. If some of your “players” are not bipeds using the character class, set it to pawn. Then add them in to your array. Make sure the array in your other blueprint is set to the same data type.

James, you are a superstar! Thanks for this - I’ll be sure to test this tonight :slight_smile:

James, cannot thank you enough for this. I tried out your new array layout and it’s worked an absolute treat!! Finally I can move on the next stage and start building the models knowing that I have a working navigation system in place :slight_smile: