Godot 4.1: 错误: 无效设置索引“纹理”(在基数上:“无”),值类型为“AtlasTexture”

Godot 4.1: Error: Invalid set index 'texture' (on base: 'Nil') with value of type 'AtlasTexture'

提问人:Sarah Coulston 提问时间:11/16/2023 更新时间:11/16/2023 访问量:14

问:

我希望有人能帮忙。我可能只是错过了一些明显的东西。我正在尝试在 Godot 中制作一个库存系统,使用插槽场景、插槽块场景(具有插槽场景网格)和库存屏幕场景(具有插槽块场景的选项卡容器)。但是我收到此错误:无效的设置索引“纹理”(在基数上:“Nil”),值类型为“AtlasTexture”。在老虎机场景中。

老虎机场景脚本:

extends PanelContainer


@onready var item_icon: TextureRect = $MarginContainer/ItemIcon
@onready var quantity_label: Label = $QuantityLabel


func set_slot_data(slot_data: SlotData) -> void:
    var item_data = slot_data.item_data
    item_icon.texture = item_data.item_icon
    tooltip_text = "%s\n%s" % [item_data.name, item_data.description]
    
    if slot_data.quantity > 1:
        quantity_label.text = "x%s" % slot_data.quantity
        quantity_label.show()

插槽块场景脚本:

extends MarginContainer


const SLOT = preload("res://inventory/slot.tscn")


func _ready() -> void:
    pass

func set_slots_block_data(slot_datas: Array[SlotData], item_type_filter: ItemData.ITEMTYPE) -> void:
    
    var passed_inv_data: Array[SlotData] = slot_datas
    #optional item type filter
    if item_type_filter and item_type_filter != ItemData.ITEMTYPE.NULL:
        match item_type_filter:
            ItemData.ITEMTYPE.EQUIPPABLE:
                passed_inv_data = passed_inv_data.filter(func(x): return x.item_data.item_type == ItemData.ITEMTYPE.EQUIPPABLE)
            ItemData.ITEMTYPE.FOOD:
                passed_inv_data = passed_inv_data.filter(func(x): return x.item_data.item_type == ItemData.ITEMTYPE.FOOD)
            ItemData.ITEMTYPE.USABLE:
                passed_inv_data = passed_inv_data.filter(func(x): return x.item_data.item_type == ItemData.ITEMTYPE.USABLE)
            ItemData.ITEMTYPE.QUEST:
                passed_inv_data = passed_inv_data.filter(func(x): return x.item_data.item_type == ItemData.ITEMTYPE.QUEST)
            ItemData.ITEMTYPE.MISC:
                passed_inv_data = passed_inv_data.filter(func(x): return x.item_data.item_type == ItemData.ITEMTYPE.MISC)
            
            
    populate_item_grid(passed_inv_data)


func populate_item_grid(slot_datas: Array[SlotData]) -> void:
    var item_grid = $"ScrollContainer/MarginContainer/ItemGrid"
    var children = item_grid.get_children()
    
    if children:
        
        for child in children:
            child.queue_free()
        
        for slot_data in slot_datas:
            var slot = SLOT.instantiate()
            slot.set_slot_data(slot_data)
            item_grid.add_child(slot)
            

库存屏幕场景脚本:

extends PanelContainer

@onready var inventory_tab_container: TabContainer = $HBoxContainer/MarginContainer/VBoxContainer/InventoryTabContainer

const SLOTS_BLOCK = preload("res://inventory/inventory_slots_block.tscn")

var test_inv = preload("res://test_objects/test_inventory.tres")
var slotdatas = test_inv.slot_datas

# Called when the node enters the scene tree for the first time.
func _ready() -> void:      
    set_inventory_panel(slotdatas)


func set_inventory_panel(inv: Array[SlotData]) -> void:
    var children = inventory_tab_container.get_children()
    for c in children:
        self.remove_child(c)
        c.queue_free()
        
    if inv:
        var inv_panel_all = SLOTS_BLOCK.instantiate()
        inv_panel_all.set_slots_block_data(inv, ItemData.ITEMTYPE.NULL)
        inventory_tab_container.add_child(inv_panel_all)

ItemData 类:

extends Resource
class_name ItemData


const MAX_VALUE: int = 1000000


#name of the item
@export var name: String = ""
#description for the item
@export_multiline var description: String = ""
#value in currency for a single unit of the item
@export_range(0,MAX_VALUE) var value: int = 0
#determines max hit points of item
@export var max_hp: int = 1
#determines if item is indestructable
@export var indestructable: bool = false
#item type of item 
@export var item_type: ITEMTYPE = ITEMTYPE.MISC: set = set_item_type
#item icon
@export var item_icon: AtlasTexture
#determines if an item is stackable
@export var stackable: bool = false


enum ITEMTYPE {
  EQUIPPABLE,
  FOOD,
  USABLE,
  QUEST,
  MISC,
  NULL,
}

func set_item_type(new_value: ItemData.ITEMTYPE):
    item_type = new_value


func decrease_value(change: int = 0):
    var prev_value = value
    value = value - change
    
    
func increase_value(change: int = 0):
    var prev_value = value
    value = value + change

库存数据类:

extends Resource
class_name InventoryData

@export var slot_datas: Array[SlotData]

插槽数据类:

extends Resource
class_name SlotData

const MAX_STACK_SIZE:int = 99

@export var item_data: ItemData 
@export_range(1, MAX_STACK_SIZE) var quantity: int = 1: set = set_quantity


func set_quantity(value: int) -> void:
    quantity = value
    if quantity > 1 and not item_data.stackable:
        quantity = 1
        push_error("%s is not stackable, setting quantity to 1" % item_data.name)

对我搞砸了什么有什么想法吗?我可以很好地运行插槽块场景,但是当我尝试运行库存屏幕时,它给了我这个错误。

我希望我加载它时的库存屏幕场景(使用我的测试库存,其中有 20 个插槽数据插槽,其中两个插槽中有插槽数据。

项目 1(口粮项目 2(项链)

似乎在第一个插槽(其中有口粮的插槽)上出错

运行时错误 Godot Godot4

评论


答:

0赞 Sarah Coulston 11/16/2023 #1

我想通了!我需要将item_icon和数量标签变量移动到插槽场景中的 set_slot_data 函数内部。