I have my AI looking for pickups if it’s health is low. It can find a shield, a health regen, or a health bonus pickup. Right now it looks for them in that order and finds the first one it can get. Is there a way have it go to the closest one of any of the three?
Hey @DigitalSketch!
Sure you can!
You can do a Multi Sphere trace for Objects of a good distance, searching for any objects of those types, and then it will output an array.
From there, use a ForEach loop with that array as an input, getting the object location minus the AI’s location. Then use get vector length, and then add that float to an array of floats using Array: Add. (This array needs to be cleared with Array: Clear before the Sphere trace).
Once you’ve done that, you can use the “MinOfFloatArray” node to get the index of the float that is lowest. This index will match the index of the Multi Sphere Trace for Objects output.
Now, you can use that index number on your Multi Sphere Trace for Objects output array with a “Get: A Ref” node, and there you have it! The closest shield, health regen, or health bonus pickup object.
If you have any further questions, let us know!
Great thanks I’ll give it a try. Hopefully I can find it all.
You could declare a new interface, such as “IPickupable Interface”, add that interface to your pickupables, have some AI BlueprintTask called something like “Pickup nearest pickupable”, in which you could either do a sphere trace for pickupable actors and pickup the closest one, or use the “Get All Actors with Interface” and do some distance checking on each Pickupable.
u can do a EQS too if the other solution dont work
I’m getting the actors by class, but how do I assign those to the object types of the sphere trace?
I made a mockup for you!
Don’t forget: Right click to split struct pins when it’s only one pin that seems like it should be many!
Of course, this is in a function, but you could put it on the Event Graph as an event or part of something else!
I think I got it, and I found I can set the object channels for collision in the projected settings. Can I assign a channel to the pickup items so it only finds those? I see I can add the object channel, but cant’ figure out how to assign the channel to the pickups.
@Mind-Brain
I think I have this setup right, but the AI aren’t moving to the target. Do you see anything I did wrong? I’m getting the location from the get and setting it to “My Pickup” which should set the location target in the blackboard.
So a few slip ups I see with a once-over:
Also, make sure your Location Target key is named absolutely the EXACT same as it is on the blackboard. Click it and check the details.
You can merge those three branches into one “OR” logic gate- it will return true ONLY if one of them returns true.
Also, I goofed. Use “Add” not “AddUnique”, but that will only be an issue if there are multiple things EXACTLY as far away as another and they are closest- down to 6? decimal points, so it’s unlikely to ever pop an issue, but just to be safe.
To test this, you can leave the behavior tree open while you run the game, and then choose the character whose logic you want to watch in the tree with the >> symbol up in the top right. Try to get it to trigger and watch to make sure it is getting one.
Also, use printstrings to find out where the logic is stopping. If a printstring goes off, you know the exec is reaching that far. If it doesn’t print when it should, you know there’s a problem before that point!