Download

Unique Actor Identification? (Removing an Actor from an array)

I have an actor that spawns actors that spawns actors. Here’s the easiest way to look at it

Game

  • Category
  1. Item
  • Category
  1. Item

  2. Item

  3. Item

  4. Item

  • Category
  1. Item

  2. Item

  3. Item

Game has an array of categories and Category has an array of Items. Simple enough. One more note, when an Item is created, I also save a variable of type Category to tell it what Category it belongs to.

For the most part, the Game is running a “For Each” loop across a category, getting the items, then “For Each” item (under the right conditions), it generates a new category. So when the nested “For Each” loop ends and it goes back to the loop for categories, there are new entries and they get processed as well. I say this because, until now, I have not had a need to reference a specific item or category - I simply process them all in order.

I’ve come to the point where in some scenarios, adding an item to one category means I need to remove an item from a different category. Referencing another category/item has become difficult.

There are two ways to remove something from an array. Either by “item” or by “index”. In my case, I have no idea what the index is, so I tried “Item”.

Using my list from the top of this post, let’s say I’m in the second category. I get to the last item which manages to find another item (that will need to be removed). The found item is actually the THIRD item in the last category array. I use the found item’s “Category” variable to get the category that found item belongs to. I then get the array of items for that category, run the “Remove Item” function on it, passing in the found item.

This SHOULD remove the THIRD item in the array, but instead it removes the FIRST item in the array. The Remove Item function doesn’t seem to check anything other than the actor type so it doesn’t care which actor is removed. It looks at the array and says “Yep, Item at index 1 is an item. Removed. Done”

NOTE: Items are removed as they are processed, so removing an item in the way described above is my way of saying “This item no longer needs to be processed”. In saying that, I can’t be certain when I get the Item’s category from the variable, I can’t be certain it’s going to the correct one (if Item doesn’t get the correct one, why would Category?).

So, if I can’t use “Remove Index” because I don’t know what the index is and I can’t use “Remove Item” because the blueprint doesn’t know what the item is, I need to find a unique identifier to the Item actor. Then I will loop through the Category and check if the unique identifier is the one I’m looking for. If so, “Remove Index” of the Item array on the index I found. I have found the actor “Display Name” which looks good (and I’d be happy to use it as a unique ID) but the tool tip specifically says it should not be used to uniquely identify actors. Is there anything else I can use?

If there’s nothing else, I will need to come up with a system to generate unique identifiers myself, but I wanted to check here first.

Wow that post was much longer than I had anticipated.

TLDR:
When you run a “Remove Item” on an array, does it just remove the first item that matches the type? (mine seems to)
Is there a unique identifier generated for actors I can reference (Similar to “display name”)?

Hello,
to know your index, when you set or add your item in your array, set an int variable with the current index. Then you can get all actors / cast to / select the variable you want (or any other variable you can set which will help you)

But if the array is changed (item removed from the beginning, for example), the index will be wrong for every subsequent item unless I run a loop to update them which will have a decent amount of overhead.

Maybe you can do all in one loop : get all actors / loop / cast / check var in item, false do nothing, true remove then all others to the end var = var -1 (by a var “removed” set true on remove and false on completed.

Unfortunately there are a number of circumstances for which an item will need to be removed and there would be no way for me to implement this without running a second loop every time it happens. But I guess it wouldn’t matter much… it would either be

A:

  1. Add item to array, give the item the index
  2. Remove item from array using the index, run loop to update all subsequent items (usually less than 10, 2-4 is more common).

or

B:

  1. Add item to array, assign the item a unique ID
  2. Loop through array checking to see if the ID is found. If it is, break the loop, remove the item.

Looking at it this way, A seems better for a number of reasons

  1. Using the index as a URN makes it much more clear which category/item is being referenced at that moment.
  2. No need to check to see if anything matches within the loop, simply take the URN and use it as an index.

I had typed another 10 lines here or so about other problems I might have, but it’s all stuff I can work out with my logic and it wouldn’t make much sense without posting my entire blueprint. So I think I’ll give that a shot and report back with the results.

Thanks