How to select and move units rts style?

Hey all. I’ve been working on a little rts project and I’m wondering how would I go about setting up a way for me to be able to select unit/s and move them to where I want.

Most of the tuts I find are either extremely outdated, make a point of not using behavior trees or use C++. Since I’m still a huge noob when it comes to ue4 I’d prefer to stick with bps since that’s how the rest of my project is.

Ideally I’d left click and drag which draws a box and then any unit in the box would get selected and then I could right click to move them to a location.

Any help appreciated.

below is a simple example of how this could be done. i got the basics for this from Mathew Wadstein who’s youtube channel ive linked below. this is also assuming that you want the box select to be in screen space not world.

(First Picture) the important bit here is the get actors in selection rectangle which returns every actor of the filter type under a rectangle drawn on the hud. so each frame we are getting the actors under the rectangle and adding them to an array of selected actors. i added the draw rect at the end to visualize the selection box. also note the use of the gate which serves to enable and disable the selection process and serves to lock in the selected actors.

(Second Picture) to enable the selection i used a simple toggling of the gate in the hud by calling the event on left mouse click and release, dead simple. as for the movement of the characters selected thats really going to depend on your implementation so i just used a simple on right click get teh selected actors, then for each cast to character and ai move. im sure some of the casting here could be avoided and simplified but for proof of concept it works.

Hey thanks that looks great! Just a quick question why would casting want to be avoided?

its not that it should be avoided its more that it may not be needed in this case. most times when you specify a class filter the output array will change to be that class. so if you filter for character the output array would be of type character, like changing a variables type. in this case when i set it up the output was still actor even though i set the filter to a more specific class. the filter is basically a cast itself, so adding a second cast is somewhat redundant. as we know doing something twice is a waste of performance so we should avoid then when we can.

Ahh thank you.

Doesn’t work. Should this be setup in my level bp? And what kind of bp is the “new hud”

I was adding breakpoints and for some reason the events aren’t even firing, I have it setup the exact same way as you aswell.

Yup it’s setup exactly the same except the name are different. Have the hud selected in my game state the rectangle doesn’t appear at all and if I add a breakpoint it doesn’t even fire.

Should this be setup in my level bp?

It says it right there in the screenshots. Hud class and Level Blueprint, although I’d put the latter in the player controller instead.

And what kind of bp is the “new hud”

It’s the HUD class - info here

Doesn’t work.

Knowing ThompsonN13 and judging by how it’s set up, it should work. It’s pretty neat, using HUD class for this. Can’t confirm 100% whether it does or doesn’t work without trying but if you need to ask what “new hud” is, you definitely did not set it up correctly yet.

hud selected in my game state

Game Mode - are you sure you using it? If you can’t print string right after Receive Draw HUD, it’s not set up right.

Now I just need to figure out how to
use two different huds at once.

I don’t think you can… it’s a framework class. And why would you even want that?

Figured it out I have another bp that auto switches to a different hud so right at begin play it was switching to a different hud. Now I just need to figure out how to use two different huds at once.

My clock, calendar and other relevant player data are setup in the widget class.

Hud class and widgets are pretty much separate things. They are completely different, too (widgets replaced Hud). You can (but you do not have to at all) have a Hud class in your game and as many widgets as you want. One, both or none.

To be honest, you could probably skip the Hud class conpletely here and draw in the onPaint of the widget.

Although, I like this example.

How does that work? Is it basically the same?

It would be very similar, you could do it in an existing widget (ideally a fullscreen one, obviously). Or create a separate canvas that just draws rectangles. Widgets can override onPaint. Can’t remember how it’s called precisely, something with drawing brushes. Or you could resize a border instead. It’s just visual anyhow. The magic happens in the selection rectangle node and this could be pretty much anywhere.

It’s probably easier to do it in the Hud (as in the original pic) but widgets give you extra flexibility. I kind of like the Hud here because it will not interfere with any widgets you may have on the screen at the same time. Feels like a robust solution.

Yeah for sure my problem right now is in my player controller I have it set to setup/call my widget hud in beginplay and it doesn’t want me to use a widget hud and hud class at the same time.

I ended up figuring it out, thanks.

Yeah for sure my problem right now is
in my player controller I have it set
to setup/call my widget hud in
beginplay and it doesn’t want me to
use a widget hud and hud class at the
same time.

This make no sense, sounds like you’re confusing things. Took less than 1 min to set it up in a new project and it just works out of the box. Class filter seems to work well even when it needs to evaluate Select node choices, and the HUD can draw material (fast!) so this can be turned into something really fancy. And this will always be on top of widgets, since it’s da HUD.

Never knew this node existed before reading this. And I even considered writing something like this. Fistbump @ThompsonN13.

@PerCat: good luck with the rest.

i didnt know it existed either until i tried to make this setup lol. Rama has got a node for doing box selection as well but i didnt get it working.

For all those People who had problems with it. For me it does still had the previous units selected. To Handle this, just Resize the Array to 0 (Basicly deleting all of its content), after you cycling through your loop giving them commandos and stuff.
And thanks for the Threat. Way faster then watching 30Min of a yt Vid