Blueprint Runtime Error

Hello I am now getting the classic error when picking up items.


IndexVariableScreenshot 2024-04-26

Ive attached a shot of the script break down for the main assets.

i am wondering if i need to reconnect the add item functions in the inventory but when i do the items have already filled the inventory when i play the level but i get no errors.
And I wondered if I should use a is valid node somewhere

thanks for any help if anyone gets a chance to look at this

image
Your get widgets of class seems return with 0 size array. When you trying to get index 0 it returns null. That’s why you’re getting the error.

Some thoughts.

  1. Is the widget you are trying to get in viewport at the time of triggering this event?
  2. I’d recommend saving your item in a class instead of a widget.

Hi thank you very much for your reply,

yes the main inventory widget, which is added correctly to viewport, is the parent of the item widget but as soon as i click on the pickup item in the level i start to get the runtime error. Can you please clarify what you mean by saving the item in a class instead of a widget?

thanks again

The other thing I meant to say was that each of six pickups I have already allocated in the level a index number from 0 to 5 so how could i easily reference each of the pickup index numbers from within the inventory widget event graph so they correctly get added to the add item function in the inventory widget. Would I make variable for the pickups within the inventory widget and add a index input ? Could that work?

If your parent inventory widget is always in viewport and only has 1 ever added. Get index 0 on the array.


Replace the widget class and call the add item by index.



Thanks a lot for your reply. The error keeps saying its related to index 5 and its somehow tied to the add item function i created. I have added extra variables for the index to find the source of the issue but havent resolved it.
So are you suggesting i add my Add item function to the end of the cast to widget node?

Regarding saving item in a class instead of a widget. It’s because widgets are usually being added/remove often, not a safe way to store variables.

You can put your item info on some actor on the level instead. Then you can open/close (add/remove) your inventory widget fine without losing the variables.

Every time the inventory actor is triggered(like clicked as you use on pickup), you toggle the inventory widget using its information.

Move the add item function to the inventory actor, clicking on pickup add item to inventory actor instead of inventory widget.

ok thanks for your suggestion. So you would advise using a struct as well then?


This is the problem you should aim to solve. Not the red one. The error is the result of the access index out of bound. Try to make sure Get Widgets() array has the index before using it. The error would be gone. But you still need to find out why it’s length is 0.

okay many thanks for your advice, i will look into that.