Autoload positions not working

Godot Version

4.3

Question

Hey y’all. I have a slight problem, this is what I’m doing; I have a inventory Control node that has a script attached that defines the behavior for the items in the slots, when you click on them, how they get moved around, etc. I’m trying to move those functions into a global autoload script called slotitemsbehavior and just keep the functions that trigger certain things within the inventory script itself. The reason being I think it would be better for multiplayer purposes where instead of every player holding a copy of those functions they instead can grab them from the autoload script so its just one instance of them at all times, (maybe this is not a good idea, you can lmk if you think so lol).

But the problem comes when i grab an item from a slot, its position should follow the mouse cursor much like minecraft or terraria inventories, yet the position goes to 0,0, (relative to node).

I was doing this right before I left the house so I have come up with some guesses as to the error. It could be that when I do add_child() from the autoload it adds it as child to the autoload node and not the inventory node, so maybe I can pass the inventory node as a parameter to the autoload scripts functions to get the add_child() to the right node.
I’m just not home right now so I wanted to get some thoughts on this, thank you.

You don’t need an Autoload to have some “shared” functions. You can define static functions that are not bound to any instance of the class, which you can easily call from within the class and outside of it.
e.g. you Inventory class script is the following:

class_name Inventory
extends Node

static func static_method() -> void:
	pass

func non_static_method() -> void:
	pass

func _ready() -> void:
	static_method()
	non_static_method()

Then e.g. in your player class you can call the static method without any instance of the Inventory class, as opposed to the non-static-method for which you always need a specific instance of the class.

class_name Player
extends Node

func _ready() -> void:
	Inventory.static_method()

	var inventory_instance: Inventory = Inventory.new()
	inventory_instance.non_static_method()

Regarding this:

This is definitely what’s happening, so that’s another point why not to use the Autoload :slight_smile: