x

Search in
Sort by:

Question Status:

Search help

  • Simple searches use one or more words. Separate the words with spaces (cat dog) to search cat,dog or both. Separate the words with plus signs (cat +dog) to search for items that may contain cat but must contain dog.
  • You can further refine your search on the search results page, where you can search by keywords, author, topic. These can be combined with each other. Examples
    • cat dog --matches anything with cat,dog or both
    • cat +dog --searches for cat +dog where dog is a mandatory term
    • cat -dog -- searches for cat excluding any result containing dog
    • [cats] —will restrict your search to results with topic named "cats"
    • [cats] [dogs] —will restrict your search to results with both topics, "cats", and "dogs"

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.

Product Version: UE 4.21
Tags:
more ▼

asked Feb 26 '19 at 04:56 PM in Blueprint Scripting

avatar image

PerCat
39 1 3 7

(comments are locked)
10|2000 characters needed characters left
Viewable by all users

1 answer: sort voted first

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.

https://www.youtube.com/watch?v=YopAmlWD8j8

alt text

alt text

capture1.png (280.3 kB)
capture.png (310.4 kB)
more ▼

answered Feb 27 '19 at 01:49 AM

avatar image

ThompsonN13
10.1k 33 10 25

avatar image PerCat Feb 27 '19 at 03:40 PM

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

avatar image ThompsonN13 Feb 27 '19 at 04:18 PM

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.

avatar image PerCat Feb 27 '19 at 04:32 PM

Ahh thank you.

avatar image PerCat Mar 01 '19 at 09:46 PM

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.

avatar image Everynone Mar 02 '19 at 01:18 AM

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.

avatar image PerCat Mar 02 '19 at 01:29 AM

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.

avatar image Everynone Mar 02 '19 at 01:33 AM

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.

avatar image PerCat Mar 02 '19 at 01:51 AM

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.

avatar image Everynone Mar 02 '19 at 01:55 AM

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?

avatar image PerCat Mar 02 '19 at 02:04 AM

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

avatar image Everynone Mar 02 '19 at 02:21 AM

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.

avatar image PerCat Mar 02 '19 at 02:35 AM

How does that work? Is it basically the same?

avatar image Everynone Mar 02 '19 at 02:52 AM

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.

avatar image PerCat Mar 02 '19 at 02:56 AM

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.

avatar image Everynone Mar 02 '19 at 11:44 AM

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.

avatar image ThompsonN13 Mar 02 '19 at 02:48 PM

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.

(comments are locked)
10|2000 characters needed characters left
Viewable by all users
Your answer
toggle preview:

Up to 5 attachments (including images) can be used with a maximum of 5.2 MB each and 5.2 MB total.

Follow this question

Once you sign in you will be able to subscribe for any updates here

Answers to this question