How to inquiry the nearest actor?
Can anyone help?
asked Sep 30 '18 at 01:33 AM in Blueprint Scripting
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.
answered Sep 30 '18 at 02:04 PM
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.
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
Hope this helps,
answered Sep 30 '18 at 06:11 AM
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?
answered Oct 02 '18 at 04:57 PM
Follow this question
Once you sign in you will be able to subscribe for any updates here