Reference an actor from array based on location?

Hey all. By now I’ve posted this to the ue4 sub, it’s been on the answerhub for about a week and I’m honestly at my wits end and super upset at this since I can’t progress any more until I solve this.

I’m trying to make it so players can interact with the HISMs that are generated in the world. I do this by a HUD menu that will popup with the relevant buttons depending on what you’re interacting with. This works pretty well. Right now in the process of resource collection, things like wood, gold, etc, etc…

I have a simple setup to chop down trees and collect the wood. This is used with an event generation master that will spawn a BP Actor to act as a specific event when needed. It will destroy itself when it is done.

My problem is I need some sort of way to re-reference the actor if the player did not finish chopping down a tree, or went and did something else, or was interrupted for whatever reason. I’m attempting to get the player, tree and the cut event actors’s location and check them against each other but it doesn’t seem to work.

  1. Player interacts with tree.

  2. HUD prompt appears.

  3. Chop wood button is hit.

  4. Event master spawns chop wood actor by getting the hit HISMs location.

  5. Tree is chopped down wood pops out every “chop” this works perfectly with the third attempt(as long as the player stays in the exact same spot) and the other two it will only work once.

I am trying to get the players location, the HISM’s location and the cut wood actors location, compare them if similar then don’t spawn and use that one to de-increment the trees’s health.

(1st and 2nd attempt) - (Second branch = True)Any further attempts will spawn another one, but since I’m for looping all actors of class it drops wood exponentially more often. 10, 20, 40, 80, 160, etc, etc… So infinite wood then game crash.

(1st and 2nd attempt) - (Second branch = False)It will not want to spawn another for unknown reasons, and it won’t go off the first spawn as the array length is now greater then 0. Unless the actor is deleted, but since multiple people can chop wood it’s just assumed this would always be greater then 0 after the first spawn.

Code

The closest I’ve gotten by far is using a sphere trace to detect if the player is overlapping the tree HISM. The problem is a chopped tree is a log on the ground; so a sphere trace will never cover the whole thing unless it is gigantic. which is just gonna mean I can’t chop any trees close to each other at all. I need some sort of way to trace along the trees entire collision and then if it has a cut wood actor overlapping(and a player within x range) use that, exact one’s functionality. If it does not have an overlapping cut wood actor, spawn a new one so you can use that one’s functionality instead.

Where mine fails is it will just spawn a new one if the player walks a little bit down the tree’s length as it’s checking for overlaps in a sphere not the tree’s collision.

Any help appreciated.

I suggest you just check for the tree health when the levels loads and see if it’s bigger or smaller than half of it if bigger return it back to HISM if smaller cut it and make it into a stump. The idea is few trees actors won’t tank the performance of your game and it would be a hassle and a waste of resources to create a whole new system just for few unfinished cut trees. Ps: Did the same thing with rocks in one of my games does it removes a little bit of the emersion of the game maybe if the dude logged off in front of the tree but 99% of the time they wouldn’t even remember it, so don’t go too much over your head with this problem. Good luck.

Well I need this to able to work correctly otherwise it will keep spawning the same actor over and over or be unable to get a reference to the correct one which would allow a player to just get infinite wood from the tree and potentially cause crashes and lags as the actors wouldn’t be getting the removal call.

Again why would a player run around a forest and half finish every tree he sees at best you will have ten to fifty tress like that scattered in an area of a whole forest and that at a worst-case scenario, with will give no lag a all just 50 static meshes is nothing especially if they have a huge distance between each other. And I not really sure how did you got into the infinite actors spawning again really simple if the three is too damaged add a stump in its stead(or remove it completely) when logging out(or if its a multiplayer add a delay with distance to players that calls himself if a player too close) and if not remove it back to HISM even if you get wood when you not finishing removing a tree it will take ages to get more resources even if people will know about the “feature” they won’t bother. Again you can add a complex system for this but I don’t see a reason why you should do something like that if you do want use Map for this as location the key and actor as a reference that will fix most of your problems.

That’s a pretty lazy way of looking at it man. I don’t think you don’t understand my issue well or something. This is any resource the player gets and the player also creates the level so I don’t know how many trees there will be, where they’ll be, etc etc… But, it’s a situation where resources matter, so just leaving in exploits that allow people to get infinite wood is an instant game break, and no minor one, an integral part of the game.

  1. Player interacts with tree.
  2. Hud prompt appears.
  3. Chop wood button is hit.
  4. event master spawns chop wood actor by getting the hit HISMs location.
  5. tree is chopped down wood pops out every “chop” this works 1 time.

I am trying to get the players location, the HISM’s location and the cut wood actors location, compare them if similar then don’t spawn and use that one to de-increment the trees’s health.

(Second branch = True)Any further attempts will spawn another one, but since I’m for looping all actors of class it drops wood exponentially more often. 10, 20, 40, 80, 160, etc, etc… So infinite wood then game crash.

(Second branch = False)It will not want to spawn another for unknown reasons, and it won’t go off the first spawn as the array length is now greater then 0. Unless the actor is deleted, but since multiple people can chop wood it’s just assumed this would always be greater then 0 after the first spawn.

Sorry, I tough you were further ahead and talking about the cleanup system so we were talking about different things. As I worked with HISM before I can see that you not using them correctly, you use too many loops with will collapse your game performance easily I give you an example of how I use HISM. When my player hits a HISM rock it removes that HISM Instance and creates an actor of rock in its stead, the actor has the same material and transform as the instance, then this actor has all of its stats health and etc all of your functions will work on him and not on HISM, because at best HISM can only be a place holder for actors it’s really hard and complicated to use them as anything else because HISM using a dynamic array as its base so you can’t save an index of an instance so you have to use a trace or to go through all of its instances with a loop with is called bad codding, that why you should do this.

  1. The player interacts with a tree.
  2. Hud prompt appears.
  3. The chop wood button is hit.
  4. Spawn actor tree (take reference of a tree from here) and remove an instance
  5. Chop the tree down
  6. ???
  7. Profit

Simple no loops looking for the instance no complex systems just simple actor to actor interaction, and it will fix your problem in a more correct way because now if you have a forest of 10000 trees your gave will freeze every time someone would have tried to cut a tree, I know this is not an answer that fixes your code but its the right way to do it.

Hmmm… Okay so you’re saying actually replace the actor with a bp instead of an event… That might work pretty well actually. Lemme test that and report back thanks!

Edit: After some initial attempts I’ve realized it doesn’t synergize well with the way my controller scheme is setup, and it would be way more resource heavy… Any other way to pull a ref of an actor from that array?

Bump, cause this thread is 10 days old with still no solution. Does anybody know how to do this? I’m stuck until I can figure out how to do this.

Use map with location to your events, I don’t really know if it will work because vector map(dictionary) is a little bit finicky but I guess if you round the location every time and have a rule that no two trees can spawn in each other then it should work a lot easier.

Hey, I appreciate you coming back to help, I know literally nothing about maps so I’m gonna research those real quick and test from what I learn, thanks!

Edit: I don’t think maps will work cause it still doesn’t solve my problem that I need some sort of way to pull the reference, ti just means I’m failing at pulling the ref from a map instead of an array.