Adding new resource using for loop

Godot Version

`4.2.1

I am trying to add a new custom resource to my characters inventory array using a for loop and cannot understand why it wont work.

the inventory is an array of “Inv_Slot_data” resources.

func _ready():
inv_data.slot_datas[4] = Inv_Slot_Data.new()
inv_data.slot_datas[4].item_data = LOG

var player_inv = inv_data.slot_datas
for slot in player_inv:
	if slot == null:
		print("empty")
		slot = Inv_Slot_Data.new()
		slot.item_data = LOG

the first 2 lines where I specify what index of the array to create a resource for work like a charm. when I try replicate this in a for loop it just doesn’t do anything, no errors nothing. I have tried printing all the results and everything matches but i just doesn’t want to create a resource when in a for loop

Does this method print “empty”?

it does, i figured out the solution just simply get each index number by

for slot in player_inv.size():
if player_inv[slot] == null:
print(“empty”)
player_inv[slot]= Inv_Slot_Data.new()
player_inv[slot].item_data = LOG

1 Like

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