Check For NULL Array?

Is there a way to check if an array is empty, or more specifically, has no reference objects? I have actors in the scene that the player can destroy using a multi sphere trace and object type detection. The level blueprint holds the reference to these objects and is put into what I believe is a temporary array (the one from the ‘Make Array’ option) and is then passed into a function from a function library which I would like for it to detect when those actors in the temp array are no more.

After hours of research, the only thing I came to a conclusion to is that I simply have the ‘wrong setup’. What most people seem to do is to add the actors to an array, then when those actors are destroyed, they call the remove node from those destroyed actors, then check the array length to see if it’s non-comparable or if the length is equal to 0. In my case, the player just destroys actors with a specific object type, and I’m hoping to just check the reference of a set of actors in an array. With many types of actors that the player would be able to destroy, and being able to do this over a hundred times, it would be a hassle to set this up each time when I already have to get a reference for every object.

With all the other gets and index Add/Removes that unreal provides, is there really no way to check if an entire array is Valid without a silly workaround?

I may have misunderstood you, but if you want to check if an array is completely empty (containing no elements) you can drag off the array and check “Is Valid Index” against index 0… This would check if the array has any elements at all.

I saw this option too. What I couldn’t figure out was how do you cycle through all the elements in the array to check if the index is valid, and return once every single index in the array is NOT valid?

This is where I’m at right now.

There is a weird behavior though that if I destroy the first actor in the array (the one at index 0), it’ll destroy the second element as well and process the whole array as empty…

If you need to check if an array is valid, you can use the isvalid or you can see if the array length is greater than 0. Array indices start at zero, but if there is something in the array, then the first item will be index zero and it also means the length of the array is >0.

I tried that before and was hoping that it would work now that the array is a pass-by-reference, but it seems it does nothing. Here is the full setup in the Level blueprint and the function in the function library:

Looking at your example, I think your logic/flow is flawed. You loaded the array with entries, set the “Open” bool to true and “Any” to false. It goes into the function, the first branch will be true, the second branch will be false, it will go into the bottom branch. The bottom branch will be true because the array has more than zero entries in it and you don’t have the true output of it hooked up to anything.

Another thing, if your array has zero entries in it, that loop will never run and will never trigger the isvalid node.

EDIT: What exactly are you trying to do? Are you trying to see if the array is null or if entries are null? If you remove an item from an array, it’s slot that it used to be in doesn’t become null, the array shifts all the entries down to fill the hole. So if you had 10 items in an array and removed the 3rd item, the 4th-10th items would drop down an entry and the array would now have a length of 9. The only way it would become null is if there was one entry in the array and you removed it, this would then make the array length 0 aka null.

Yes, I’m trying to see if the entire array is null. So based off of what you’re saying, I can get away with having the bottom branch checking if is valid index 0 is valid since it checks the last remaining element or check the length like you mentioned. What I have is the player destroying those array entries, not removing them from the array. So my question now is when I just call Destroy Actor on those array entries, does that remove those actors from the array and shift ls them? Could I cycle through the array to find null entries then somehow delete the index or reduce the size of the array, to finally check the length to see if it’s 0?

In that case, when you destroy the actors, you should remove the entry from the array at the same time. It depends on your system though, as to which method you should use. You could run an isvalid check in a for each loop and remove the entries that aren’t valid anymore too, but that can get sloppy quick. Ideally, you’d just remove the array entry as you destroy the actor.

You can use something like this, but you might have casting involved depending on your exact setup. The blue wire coming in is the actor to be destroyed/removed from the array:

Not sure if you have solved this, but if I were you I will process the array in reverse (ie start from the last index). This way, the index will never be changed when an item is removed.

Okay, I see. And I do appreciate your help as well, it’s tough getting a reply on here, but I think I may have to approach this entire thing differently; the player just calls destroy on certain object type actors with multispheretrace, and using this method, I would have to assign these specific actors to an array that’s in the player BP, or have the actors assign themselves to an array in the level BP and have them remove themselves on destroy or something like that. It requires a lot of setup because I can potentially have MANY groups of specific actors that needs to be checked if they’re still alive, if you know what I mean.

My goal here was to have something flexible and almost automated, because this will be used countless times throughout the level: The Null node will take a bunch of referenced actors in the level blueprint and continuously check if ANY or ALL of those specific actors are still being referenced. Based on the boolean, it’ll return true and open the next node. The top part(ANY) works just fine, but because there is no dedicated check node for an empty array, we would have to Remove(node) each item before destroy to shrink the array, then ask if the length is 0. SO, my problem is how can I remove/shrink the array that is passed into the Null node when it detects that that one of the elements’ actors is gone, so that I can THEN check if the length is 0?

And sorry for being such a bugger and I hope you see what I’m saying. I have a tendency of picking up the most complex of tasks. lol

My good sir, you were right! I don’t know why I didn’t see this before, but when I read it it all clicked to me. Here’s the finished blueprint:

If anyone comes across this as I did, my solution to checking for an Empty Array is to simply do this:

You may want to fix those booleans. :wink:

1 Like

Typically when you remove an index from an array, it reorders the indices. So for example if you have 4 items at index 0,1,2 and 3 and you remove index 2, 3 will become 2 and 3 itself will be removed or empty. Checking if Index 1 is valid means that the array is NOT empty which is what you were wanting. If Index 1 is NOT valid, then your entire array would be empty unless you’re somehow removing indices and not re-ordering the array, but I don’t think that’s possible.

I’d probably check the length of the array instead to be safer. If the length is == 0 then it’s empty.