[Closed] [SOLVED] Populate array with loop?

What am I doing wrong with this blueprint?
All I am trying to do is populate an array from another array of “options”. It loops for the total army size.
At least, it’s supposed to. It’s populating the “Army Units” array with 1200 “none” fields, and i cant figure out why. i have no blank fields in the Unit Options array and I dont reference 1200 ANYWHERE in the blueprint.
The army size is randomly selected between 20 and 50, and the Unit options array only has 4 actors contained in it…
Am i just missing something that is glaringly obvious?

Thanks,

One thing that’s wrong is that you’re getting a random Int within [0-4], whereas the array only has indices [0-3]; make it length - 1.

However, that would explain some items being Null, not all of them.

Are you sure you are not accessing it from somewhere else? Maybe you set a default value somewhere?

I tried it myself in a level blueprint with arrays of actor class references and it worked fine.

Are you using this in multiplayer and the replication makes problems?

Also, like Widzo said, use Last Index.

This is how I tried:

292542-try.png

Or use “Last Index” instead of “Length”.

That did it! Thanks to your guys’ help, I got a solution!

Thank you guys very much! Its so simple, sometimes you just need another pair of eyes to see the problem(:
I’ll probably update the random int. to grab from the last index like LordStuff recommended.

This is how I did it:

Glad that you could solve it! :wink:

Just mark the question as answered pls!

You’re right! I initially had this on begin play I think, but I must’ve swapped it over to tick just to see print string the array length to make sure it was working right.

why are you running this on tick? it seems like your trying to make a array of units basically initializing it. so that would be something that in my mind you would do once then not need to do again. now it may not be a huge performance hit it to check the array length on tick but its a rather simple thing to solve as well.

right now your basically asking is condition true every frame(30-120 times per second roughly), you could instead do the same script on begin play and it will run once and never run again.