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 inquiry the nearest actor?

Hello!
I've been trying to accomplish a series of things that seem deceptively simple.

  1. Find the nearest actor with the tag "Interactable."

  2. Select that actor.

  3. Check if the distance to said actor is small than the variable "MaxInteractRange" stored within the actor.

  4. If so, run whatever function is in there.

Can anyone help?

Product Version: UE 4.20
Tags:
more ▼

asked Sep 30 '18 at 01:33 AM in Blueprint Scripting

avatar image

GMSG
3 2 2

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

4 answers: sort voted first

theres probably a better way to go about it depending on your circumstance, such as overlap events or line traces which would be easy to implement and possibly more optimal.

but to give a brief overview of what you asked for. you would first need a get all actors with tag node, this should return an array which contains all actors that have the tag your looking for. you then need to parse this array to find out which actor is closest. to do this you need to use a foreachloop which gets the distance to (get distance to node) the player from the actor, then compares that distance to the distance of the other actors. the way i implement this is to have a variable which contains the closest actor then compare each other actors distance to the current closest, if the new actor is closer then you set it as the current closest and move on. once you find the closest you can then move on to the rest of what your looking to do by comparing the "distance to" to the max interact range. then you can do the last part though im not entire sure what you mean there but im assuming your trying to call an event in the actor.

alt text

capture.png (326.9 kB)
more ▼

answered Sep 30 '18 at 02:04 PM

avatar image

ThompsonN13
10.1k 33 10 23

avatar image GMSG Oct 03 '18 at 08:43 PM

Yes, thank you! It works like a charm now that it's all sorted out.

avatar image Vaei Oct 03 '18 at 08:53 PM

GetAllActorsOfClass is acceptable due to how UE4 handles it; remains largely performant. Still doesn't scale well with large worlds and should be avoided. Any other GetAllActors... nodes are not performant whatsoever and should be avoided.

avatar image Nebula Games Inc Oct 04 '18 at 03:46 AM

I actually created a custom node that does exactly this since it seemed like a common problem. It takes in any array of actors (so you could use ThompsonN13's method with "Get All Actors With Tag", but you can replace everything else after that with this custom node. It returns a sorted struct array, the struct being (actor + distance from target). So to get the closest actor from target you would simply "get" the 0 index. To get the farthest actor from target you would "get" the last index. Or it has an option to sort in reverse order, furthest to closest from target.

Here is a link to the tutorial to add the C++ code for the node (all copy/paste)

https://youtu.be/cWesVY0zF0U

Below is an example of the node and the struct it returns

alt text

avatar image Douglas Dec 17 '18 at 06:08 AM

Hey @Nebula Games, so I've tried to use your function but it's not returning me the "distance" and a simple "actor" data, but only a "Actor Dist" type of variable. Can you help me pls?

avatar image Nebula Games Inc Dec 17 '18 at 12:28 PM

Hey @Douglas, the functio returns a sorted array of the custom struct “Actor Dist”. If you use a “Get” node pulling out the 0 index will be the closest or furthest actor depending how you chose to sort. Once you have an individual struct variable from the “Get” node simply right click on the output pin and ask it to split the struct like any other struct variable. This should then give you access to the 2 component variables (Actor object pin, Distance float pin)

avatar image Douglas Dec 18 '18 at 04:29 PM

Many thanks! Not it works great. Sorry about this kind of question but I'm still learning BP and I had no clue of this "split" function haha.

avatar image Nebula Games Inc Dec 18 '18 at 04:59 PM

No problem. I'm happy the node works for you. That was the plan to make things simple and easy :)

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

Interactable actors should simply add themselves to an array contained in the GameState in "On Begin Play" and unregister themselves in "OnDestroyed", when you need them you can "Get GameState" -> Cast to your game state with the array, and access them there.

No need for tags. No need for expensive lookup operations. If their interactability is toggled via tags then the actor just checks it's own tags.

Never use GetAllActors.. nodes, they will really hurt performance, level load times, etc. -- For smaller games GetAllActorsOfClass (only) is acceptable.

This way the performance costs you nothing, it's effectively free.

This however, is only answering your question in a way that's constrained to your chosen method. There should be no need to loop every interactable actor. Simply have a trigger sphere that adds and removes themselves to an array on your character/pawn when you begin/exit overlap. Easy.

more ▼

answered Oct 03 '18 at 08:56 PM

avatar image

Vaei
596 28 24 52

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

I don't know how often you wish to do this "query" to find the nearest actor, nor how many actors you will have with this particular tag. But you might want to consider either

  1. When ever the actors are created/spawned, that they register with a blueprint, giving their location, and when they move, updating that location. Then the Blueprint that knows where everything is, can break the game playing area up into quadrants, knowing all actors in a particular quadrant, and then can answer queries from the other actors that need to know. I would set the quadrant size to be the MaxInteractRange, that you mentioned. Granted, the actors that would be querying, will hardly ever be centered in the quadrant they are asking from, so more than one quadrant will need to be inspected.

  2. Create a static mesh component that remains invisible, that you have centered on the actor that is going to query. Then invoke the function of get all overlapping actors. Again you can set the size of the static mesh component to be the MaxInteractRange. This method allows you either see the actors behind you or not, and how much to the sides of the actor that is going to query, because the shape of the static mesh, is going to determine what actors are returned from the query.

Hope this helps,

Jay

IceWare

more ▼

answered Sep 30 '18 at 06:11 AM

avatar image

jayice
1.4k 41 28 56

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

Hello.

While I'm sure this works, I'm actually unable to replicate your code.

I thought that maybe my version was outdated after I couldn't find the "Cast to Pickup" or "Use" function anywhere, so I reinstalled it all. It took ages, since I've never really had good internet...

But since I still can't find "Cast to Pickup" anywhere, I have to ask if there's something I'm missing here?

more ▼

answered Oct 02 '18 at 04:57 PM

avatar image

GMSG
3 2 2

avatar image Everynone Oct 02 '18 at 05:07 PM

Pickup is a custom class you'd need to create yourself. The same goes for the Use function - it belongs to the Pickup class ThompsonN13 created.

No need to reinstall the anything.

avatar image GMSG Oct 02 '18 at 07:29 PM

Ah, thank you! I still ended up needing to download something (The C++ redistributable) but I'm sure it'll be much easier now. Thank you!

avatar image ThompsonN13 Oct 02 '18 at 09:36 PM

yea what Everynone said.

(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