Emty object array returns length 1

Hey all,

when i try to get the length of an (empty) object array it always returns 1. What is up with that? Is this behavior intended?

Best regards,

Maffinius

No, not intended. Create a new variable, make it into an array, get length, you’ll get 0.

Show us how you handle the array you find problematic.

The array is not used so far and empty. String-output: Arraylength: 1

Thanks in advance :slight_smile:

Looks good. This would only print 1 if you dynamically spawned and added a worker. You sure nothing like that happens somewhere on a forgotten Begin Play? A testing leftover of sorts?

As an easy debug, open a New Task Villager blueprint and Prrint String something on its Begin Play event. Does it spawn?

Weird, as test, can you call Clear on the array and then print it?

251651-ue4screens5.png

I also print the arrays content right after printing out the length. Nothing is printed out and the length is still 1 :frowning:
I also double checked all begin play’s and event graph’s (even though i’m 100% sure that i nowhere create or add villagers :/)

I also print a string in the begin play like u suggested but no villager is spawned somewhere

Ok when i call clear directly before calling the printArray function its has no impact.
If i call clear in the printArray function (directly before getting the length) i get length = 0. My suggestion was that i maybe do something wrong in eventTick but there is nothing that has anything to do with the villagers.

EDIT: i changed the arraytype from Unit_BP array to Object array and now i can call clear the array before calling the function to get length = 0. but the main bug is still happening

One more thing you could try is to Print it in Tick and observe how early it becomes 1 in the log.

Also, do you know why you have World Context Object on your nodes? Haven’t seen this in years…

This is not a pure blueprint project, right?

OK i looked up my right mouse event again since the length was set to 1 every time i pressed it. It appears that i made a new array (“make array”) with no content at the end. I use “clear” now and it works perfectly fine.

Thanks for youre patencience and help :smiley:

PS: Is it intended to have length 1 when i make a new array with no content?

This happens sometimes if you drag an actor variable and select the print string node directly, without ToString() function. it creates this input pin. And it may sometimes stay there even if disconnected.

No, definitely no. Can you show a screenshot of how you do it? Make Array has a very specific use, perhaps it is not being used right.

There we go. Your array now has 1 element at index zero. You do not need this at all and it’s actually quite dangerous to use it like this for actors as you create a null pointer.

By simply having this variable, you already have an empty array - no need to make array. Try it.

I used “make array” like this.
EDIT: Just saw the “remove array element pin” option … this works aswell ^^

@ Tuerer: Thanks! It seems like it does not do much anyway.