Getting an error, even though blueprint is working.

I’m getting an error when trying to set the text in a widget blueprint.
It is trying to get the minimum damage from an array of minimum damages on my character blueprint.
It is successfully getting the correct number & displaying it, but it is still throwing up an error.

The blueprint:

The blueprint is working correctly, & can access the float I want it to:
Showing the correct number:
image

But still throwing up errors:
“Blueprint Runtime Error: “Attempted to access index 3 from array weaponsMinDmg of length 3!”. Node: Return Node Graph: GetText Function: Get Text Blueprint: inventoryUI”

The number changes as expected when selecting various weapons.

I’d just like to know how to avoid this error, am I doing something wrong here or is it an editor bug?

Hey @Flyin_Guy!

So I can tell you what’s likely going on just through the description. Length 3 of an array means 0,1,2- there is no 3. Length is a measure of how many items, and doesn’t count 0 as an entry, so to get the final entry in an array you must use Length-1 to get the final entry. I know it’s strange, it has been that way since arrays have existed, so I doubt it will change any time soon.

So you need to make sure the array being pulled from has FOUR entries, meaning Length 4, in order to pull index 3. :slight_smile:

I appreciate the help, but I am already pulling from 1 index behind as you can see in the screenshots.
It is def getting the correct variable, as displayed in the screenshot, so I don’t understand the errror.

How many elements are in that array? Print its length.

unless you absolutely know for certain how many elements an array has, you should NEVER be grabbing specific elements, even element 0 shouldn’t be trusted as that could be uninitialized.

and because the way Unreal can sometimes move things around in your array by insert, add, addUnique, Remove, you shouldn’t trust the order of the array to be always exact.

when you are quarrying an array it should for the most part always be in something like a For loop.

on the matter at hand where your elements should almost always be there, and will always have specific meanings then shouldn’t this be a struct instead of an array?

1 Like

I checked the length of the array before and as I was adding variables.
It starts at 0 and ends at 4 when all 4 slots are filled.

I’ve managed to narrow down the problem a little, I’m not seeing why it’s an issue in a widget blueprint but not in a normal blueprint though.

The text is being set every tick to the following:
If there is a weapon in the weaponArray, get the weapon’s minDamages array index equivilent & then change the text to what is in that array.

This how how the selectedWeapon bools are set, by reading the weapon’s MinDamages & making sure there is a variable there to read.

This is where the text is set for the Min Dmg.

So it shouldn’t even be accessing variables past what exists.
It’s clearly getting the correct value, so I’m a little lost as to what is happening.

I’ve gone back over my weapon pickup to make sure the length is being set correctly:

The above used to be done on a loop, I laid it out this way to try & find the point of error.

The current error reads:
Blueprint Runtime Error: “Attempted to access index 3 from array weaponsMinDmg of length 0!”. Node: Return Node Graph: GetText Function: Get Text Blueprint: inventoryUI
Blueprint Runtime Error: “Attempted to access index 2 from array weaponsMinDmg of length 0!”. Node: Return Node Graph: GetText Function: Get Text Blueprint: inventoryUI
Blueprint Runtime Error: “Attempted to access index 1 from array weaponsMinDmg of length 0!”. Node: Return Node Graph: GetText Function: Get Text Blueprint: inventoryUI
Blueprint Runtime Error: “Attempted to access index 0 from array weaponsMinDmg of length 0!”. Node: Return Node Graph: GetText Function: Get Text Blueprint: inventoryUI

It throws up this error even though the correct array index exists & is being shown in the text on min dmg here:
image

Blueprint Runtime Error: “Attempted to access index 0 from array weaponsMinDmg of length 0!”. Node: Return Node Graph: GetText Function: Get Text Blueprint: inventoryUI

The array is empty when accessed, half the time you do not even Size to Fit.

Print the length here:

See what you get before the first errors triggers. Does the error spam non-stop or only once / for some time. It could be an incorrect order of execution. You tick-access it before it’s even set?


Also, why runs this on Tick?

image

It really does not look like something that needs updating 60/120 times every second. The Get Text bound function (right?) has 4 casts. Asking as you may have a good reason - perhaps it needs updating that often because you’re animating the values, for example. Are you?

Here not shown how you build array. You trying get value, which exceed array length.

So there were a bunch of errors on the old screenshots for the weapon pickup, I fixed those and was still getting the same result.

For whatever reason, it will throw up that error if you try to set it up in a widget, but it’s fine if you hook it up to a text variable that is then set from the character pawn blueprint itself. Not sure why this is the case, all the logic remains the same.

It now also does not update on tick, only when needed. That may have been what fixed it, but there should not have been an error there in the first place as it should not have been trying to access those array values without the bool being enabled for it to do so. Not sure why it executes differently in the widget.

1 Like

Which bool?

Every single node will be evaluated here every time. That’s how the Select node works and there’s also no short-circuit evaluation in BPs whatsoever.

The selected weapon bools, it shouldn’t be able to check those arrays without me enabling those bools? They shouldn’t be accessed? Forgive me if I assume wrong, I’m still learning all of this.

1 Like

No worries. And yes, every single node and wire will be evaluated here. The above script casts 4 times, no matter what the bools say. That’s why I had suggested printing the length of the array.

The Select node is particularly susceptible. Try something along the lines of:

image

And it will go bonkers. Try setting by-ref and you’ll crash the engine (at least in UE4).

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.