Error Code: Invalid set index 'texture' (on base: 'Nil') with value of type 'StyleBoxTexture'.

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: 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 :slight_smile:

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

:bust_in_silhouette: Reply From: jgodfrey

Taking a quick look at the mentioned tutorial video, your code is definitely different than what’s proposed. I’m not sure if you’re changing it as you go or just don’t have it quite right, but that’s likely causing some confusion.

The obvious miss I see in your code is that you never set your selected_style variable to a valid StyleBoxTexture. That’d be done like the others in your _ready() method. So…

selected_style = StyleBoxTexture.new()

That IS in the _ready() function of the code shown in the tutorial.

Hi, thank you for the quick reply.

The code is intentionally different from the video. The guy in the video has three textures: selected, deselected and empty slot and his texture for inventory and hotbar are the same. I was aiming for selected and deselected, but the ones for hotbar look a little different (transparent) than the slots for the inventory. Compared to the video, deselected and empty should look the same.

I implemented your suggestion and it is working again, thank you so much :slight_smile:
I’ve still got a lot of learning to do !! :smiley:

miomeow | 2023-04-20 13:49