Godot Version
Godot 4.3
Question
I am following DevWorm’s tutorial on how to make an inventory (link here, timestamp 19:40) and I am creating my version in C#.
I am at the point where I am trying to update each slot to display the items from the data in the inventory resource, but I am stuck on calling the Update method from the UpdateSlots function.
I get an error saying ‘Node’ does not contain a definition for ‘Update’ and no accessible extension method ‘Update’ accepting a first argument of type ‘Node’ could be found. I am not sure how to convert the slots variable properly so it can call this method while also being a Node array.
All help is greatly appreciated.
public partial class InventoryUI : Control
{
bool isOpen = false;
Inventory inventory;
InventorySlot inventorySlot;
private Godot.Collections.Array<Node> slots;
public override void _Ready()
{
slots = GetNode<GridContainer>("NinePatchRect/GridContainer").GetChildren();
inventory = (Inventory)ResourceLoader.Load("res://Scripts/UI/Inventory/playerinv.tres");
Close();
}
public override void _Process(double delta)
{
if (Input.IsActionJustPressed("Toggle Inventory"))
{
if (!isOpen)
{
Open();
}
else
{
Close();
}
}
}
private void UpdateSlots()
{
for (int i = 0; i < Math.Min(inventory.items.Count, slots.Count); i++)
{
slots[i].Update(inventory.items[i]);
// ERROR HERE
}
}
private void Open()
{
Visible = true;
isOpen = true;
}
private void Close()
{
Visible = false;
isOpen = false;
}
}
public partial class InventorySlot : Panel
{
Sprite2D itemVisual;
public override void _Ready()
{
itemVisual = GetNode<Sprite2D>("item_display");
}
public void Update(InventoryItem item)
{
if (item == null)
{
itemVisual.Visible = false;
}
else
{
itemVisual.Visible = true;
itemVisual.Texture = item.texture;
}
}
}
[GlobalClass]
public partial class Inventory : Resource
{
[Export] public Godot.Collections.Array<InventoryItem> items = new(); // this seems like the proper way to define godot arrays in c#?
}
[GlobalClass]
public partial class InventoryItem : Resource
{
[Export] public string name = null;
[Export] public Texture2D texture;
}