Negative Array Indeces

I am feeding an array of actors into another array of actors, basically emptying one from the Controller BP into an Inventory BP. In a For Each Loop’s Body I’m using a Set Array Element to the Array Index of the Loop, passing the item off right from it into the target Inventory array. The problem is for some reason in the target array I’m getting a negative value for the index when I go looking for the item. It’s being assigned to index -1. I didn’t even know arrays could go negative, let alone the fact that it’s not starting at zero for some reason. I’m checking off “Size to Fit” on Set Array Element and just in case, I made just to use Add nodes to add the right amount of slots to the array on Begin Play.

Here is the code:

Check if the Controller Ref is valid before you do anything to it. You may not get a variable and therefore all the states will be invalid. Also, if the controller ref is valid, check if the array is & is not empty. By valid array I mean if it’s been constructed and initialized in the controller already and filled with non-garbage data.

If it’s possible I’d advise to fill the table in construction script to avoid near begin play mess. You can also initialize arrays through the default values panel.

The Controller Ref is valid because it passes through one item but for some reason only one item and while it appears in the right spot, it’s in a weird index. I’ll check if the array is empty and move the initial bunch of ADD nodes to construction script. I’ll let you know how it goes, thanks. Also, I noticed for my array of actors, I can’t initialize the values in the default values panel. I tried making a new array to see and it says the same thing, so I can’t set up the size of it there which is why I used the ADD nodes. Is that different for you?

The reason I had it in Begin Play was because there’s a known glitch in inheriting variables in Child Blueprints that’s really screwed up my level which was all made by child blueprints set up in the construction script and since then I’ve gotten in the bad habit of copy-pasting code and completely ignoring construction script to avoid that. They reeaaally need to fix that btw (there’s a separate case report on that already though so I won’t go into that any more than I already have).

I got it! I moved the ADD stuff to Construction script and I think the problem was I was using a For Each Loop and not a For Each Loop with Break. I’m not sure what it was doing because if it was filling all the slots I think I would’ve seen the entire inventory fill up but it would only place one and still make the other indeces… for lack of a better term “non-empty” haha something was there. So there we go! Problem solved! Thanks for your help!

I believe your array is of type Object, which means it cannot be initialized in default panel because you cannot have a reference to object before run-time. Generally you can, as long as it’s not a reference type (class is ok).

Yes, there are several things with blueprints that they really need to fix to make it work-able for bigger projects or custom things.

Would be nice if you re-accepted the answer, since it gets unmarked when anybody replies :wink: Good luck with your project.

It’s of type Actor, but it is a reference, so that still applies. Hmm I could change it to class. Thanks, I’ll keep that in mind.

I’ll re-accept the answer. I didn’t know it did that.

edit: you were right, they were of type Object btw.