`extends Node2D
const SlotClass = preload("res://Inventory/Slot.gd")
@onready var inventory_slots = $GridContainer
var holding_item = null
func _ready():
for inv_slot in inventory_slots.get_children():
inv_slot.connect("gui_input" , self , "slot_gui_input", [inv_slot])
func slot_gui_input(event: InputEvent, slot: SlotClass):
if event is InputEventMouseButton:
if event.button_index == MOUSE_BUTTON_LEFT & event.pressed:
if holding_item != null:
if !slot.item: # Place holding item to slot
slot.putIntoSlot(holding_item)
holding_item = null
else: # Swap holding item with item in slot
var temp_item = slot.item
slot.PickFromSlot()`
temp_item.global_position = event.global_position
slot .putIntoSlot(holding_item)
holding_item = temp_item
elif slot.item:
holding_item = slot.item
slot.pickFromSlot()
holding_item.global_position = get_global_mouse_position()
func _input(event):
if holding_item:
holding_item.global_position = get_global_mouse_position()`
I was following a tutorial for an inventory, the tutorial used Godot 3. whilst I use Godot 4. This is the code that is giving the error ( < inv_slot.connect("gui_input" , self , "slot_gui_input", [inv_slot]) >, is giving the error) Error
So this line:
inv_slot.connect("gui_input" , self , "slot_gui_input", [inv_slot])
Looks correct for Godot 3, but as you found out, it is not correct in Godot 4.
First of all, you no longer specify a method using a String
. Instead you do this:
inv_slot.connect("gui_input", self.slot_gui_input, [inv_slot])
In fact, you don't need self
, you can do this:
inv_slot.connect("gui_input", slot_gui_input, [inv_slot])
Please notice that here the method name is not followed by parenthesis (slot_gui_input(...)
), so you are not calling the method, you are making a reference to it... And that reference is a Callable
.
And second, you do not pass extra parameters to connect
anymore. Instead you bind them to the Callable
, like this:
inv_slot.connect("gui_input", slot_gui_input.bind(inv_slot))
And that line should work.
But wait, that is not all. You can also avoid referencing the signal using a String
, like this:
inv_slot.gui_input.connect(slot_gui_input.bind(inv_slot))