How to add a script containing resources to an empty slot in a resource array dynamically from code

Versión: Godot 4.2.1.stable

Hi everyone,

I have what must be a very stupid question for some, but I have been playing with it for a while now and I can’t make it work. So any help regarding this topic would be tremendously helpful.

I have a resource called PlayerInventory, which contains a var called slot_data that passes Array[SlotData]. SlotData resource contains two vars inside, one for the item resource (that contains inside of it all of the info about the given item), and one for the quantity of such item.

Well, in that setup, let’s consider that the PlayerInventory is empty, thus no SlotData are loaded and all array entries are null. Then imagine I pick up an item in the world that contains a valid resource that can be included in this inventory. First thing I do is to search for an empty slot in the array (something like Inventory.slot_data.find(null), which works greatly to detect an empty slot. But then, I am totally unable to get something into that slot.

My main approach, using my newbie logic, has been to preload a slot_data script to it so that the slot is not empty anymore, but the system keep printing that the slot remains empty. I did something like:

var firstEmptySlot = Inventory.slot_data.find(null)
var slot = preload("res://Inventory/slot_data.gd")
print(Inventory.slot_data)	
Inventory.slot_data[firstEmptySlot] = slot
print(Inventory.slot_data)	

If I print firstEmptySlot, it always detects the first slot which is empty, which is great for functional purposes. But no matter where this slot is in the array, the first print of the slot_data list is always exactly the same as the second one, and it kind of makes no sense to me.

I’ve tried other stupid things, like preloading directly the item_data, which I guess it doesnt work because there is no SlotData on it. And I definitely don’t know now how I can assign a SlotData, then a known item_data to that slot.

At this stage, I do not know how to make it work well, and before creating another completely different system to make my player inventory (which would be a shame…), I would like to know what do you guys think I might be missing.

Thanks in advance for any expertise dropped in your answers, I really appreciate it. Hope you’re having a nice week.

The problem is, your variable slot is not an object of type SlotData. In Godot when you load a script, you get a Script object. To get a type SlotData you need to call .new() method on Script variable (in this case, slot):

Inventory.slot_data[firstEmptySlot] = slot.new()

Brief example from documentation:

# bot_stats_table.gd
extends Resource

const BotStats = preload("bot_stats.gd")

var data = {
	"GodotBot": BotStats.new(10), # Creates instance with 10 health.
	"DifferentBot": BotStats.new(20) # A different one with 20 health.
}

func _init():
	print(data)
1 Like

You are the savior of my project. Indeed, that fixed the issue. For some reason, I didn’t consider that solution at all. Thank you very much for showing up in here and for the help. Really appreciated!

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