#What is a better way to deal with Resources that use Signals?

1 messages · Page 1 of 1 (latest)

wind halo
#

Hello, I will make this simple. I have a resource called hotbar which stores an array of items for the player. Inside of this resource i also have some functions and signals so the hotbar can be scrolled through.

My problem is that I feel like i am doing something stupid or something wrong when it comes to refrencing my resource. Inside of nearly every single class that involves using the hotbar in every single way i have it linked to preload a specific resource (the resource is the first picture.) I do this because at my _ready i need it to link the signals inside of the hotbar to specific functions in my code.

My problem is that i don't want my code to automatically use a specific resource in case I want to include multiple save files so those save files can have there unique hotbars loaded.

So what can i do to make it so my resources can emit signals but i do not have to preload them?

vapid moon
#

I would use a root-level node here that just exports the hotbar. Name it HotbarContainer or something to that tune, give it a unique name, and then have nodes reference it with

get_tree().root.get_node("%HotbarContainer").hotbar_resource

#

if you plan to have many different copies of the hotbar resource that vary in their data you will need to store reference to that saved resource path somewhere somehow

wind halo
vapid moon
#

Yeah so to put it in code (warning: untested pseudocode, may have syntax errors etc):

Add this guy at the top of any root-level scene

class_name HotbarContainer
extends Node

@export var hotbar: Hotbar

static func get_hotbar(from: Node):
  return from.get_tree().root.get_node("%HotbarContainer").hotbar

Note the static function I added to make access a bit cleaner and more abstract

Then to reference the hotbar resource, such as the code you posted:

func _ready() -> void:
  var hotbar = HotbarContainer.get_hotbar(self)
  hotbar.item_added_hotbar.connect(_on_item_added_hotbar)
  # etc...
#

You'll of course have to set the hotbar variable in the inspector to whatever hotbar resource you want to use.

#

And you'll have to remember to save that as part of your save file

wind halo
#

OHH Okay now i fully see what you are doing god, that is smart. thank you. i am trying to implement it right now

#

Yeah so it is saying invalid access to property on object of type 'null instance'

#

just for refrence instead of hotbarcontainer, since i have a different node named that, i called it hotbarcontrol

#

And this is in one of the other nodes

vapid moon
#

HotbarControl.get_hotbar(self) not hotbar_control.get_hotbar(self)

#

you want to call your static function here