![]() |
Attention | Topic was automatically imported from the old Question2Answer platform. |
![]() |
Asked By | miomeow |
Hey everyone in the Godot Community,
I’m pretty new to video game development and programming. I’m currently designing the inventory and hotbar for a game and have been following this tutorial series: https://www.youtube.com/watch?v=Jf7Vbsx274s
I’m now at the point where I’m trying to connect the hotbar and the inventory, so you can switch items between the two. I’m also doing the coding for the slected/ unselected slots, and I’ve been getting the following error code:
Invalid set index ‘texture’ (on base: ‘Nil’) with value of type ‘StyleBoxTexture’.
Whenever I start the game, it immediately crashes and it “highlights” the code in line 27: selected_style.texture = selected_texture
I’m in the Script for the Inventory Slots, and it looks like this:
extends Panel
var selected_texture = preload("res://Inventory/Inventory Slot Selected 50x50.png")
var deselected_texture = preload("res://Inventory/Inventory Slot Default 50x50.png")
var hotbar_slot_deselected = preload("res://Hotbar/Hotbar Slot Background.png")
var hotbar_slot_selected = preload("res://Hotbar/Hotbar Selected Slot Background.png")
var selected_style: StyleBoxTexture = null
var deselected_style: StyleBoxTexture = null
var hotbar_slot_deselected_style: StyleBoxTexture = null
var hotbar_slot_selected_style: StyleBoxTexture = null
var ItemClass = preload("res://Inventory/Item.tscn")
var item = null
var slot_index
var slot_type
enum SlotType {
HOTBAR = 0,
INVENTORY,
}
func _ready():
selected_texture = StyleBoxTexture.new()
deselected_texture = StyleBoxTexture.new()
**selected_style.texture = selected_texture**
deselected_style.texture = deselected_texture
refresh_style()
func refresh_style():
if SlotType.HOTBAR == slot_type and PlayerInventory.active_item_slot == slot_index:
set('custom_styles/panel', hotbar_slot_selected_style)
elif item == null:
set('custom_styles/panel', hotbar_slot_deselected_style)
else:
set('custom_styles/panel', hotbar_slot_deselected_style)
func pickFromSlot():
remove_child(item)
var inventoryNode = find_parent ("UI")
inventoryNode.add_child(item)
item = null
refresh_style()
func putIntoSlot(new_item):
item = new_item
item.position = Vector2(0, 0)
var inventoryNode = find_parent("UI")
inventoryNode.remove_child(item)
add_child(item)
refresh_style()
func initialize_item(item_name, item_quantity):
if item == null:
item = ItemClass.instance()
add_child(item)
item.set_item(item_name, item_quantity)
else:
item.set_item(item_name, item_quantity)
refresh_style()
Screenshots here:
https://i.imgur.com/ZfG7q5H.png
https://i.imgur.com/4C1TfeK.png
Any ideas on where I went wrong and how to fix it?
Any help is welcome, thanks in advance
Edited to fix forum code formatting.
jgodfrey | 2023-04-20 13:04
What if you add the styleboxtexture to the scene tree before assigning a value to the texture
member? For example:
func _ready():
selected_texture = StyleBoxTexture.new()
add_child(selected_texture )
deselected_texture = StyleBoxTexture.new()
add_child(deselected_texture )
**selected_style.texture = selected_texture**
deselected_style.texture = deselected_texture
refresh_style()
godot_dev_ | 2023-04-20 13:14