How Does One Make An Inventory

Godot Version

4.6

Question

Helllllllllllllooooooooooo :3

Ive been on this for a week and a half and its one of the only things holding me back. Ive followed multiple tutorials, but none of them seem to work at all.

I have an interactable instance, but i need a way to be able to put that in an inventory. Problem is, I have no idea how to make that work. Ive attempted both without and with tutorials, and its genuinely giving me headaches :,D (The interactable code will be at the bottom)

im pretty new to godot, i can only make simple code by myself/kinda know how and why each node works together, so some solutions ive seen seem like a foriegn language haha.

TLDR: How to make an inventory? Like, a working one? (Please explain like im five :,D)

INteractable instance:

extends Node3D

var current_interactions := []
var can_interact := true


func _input(event: InputEvent) -> void:
		if event.is_action_pressed("interact") and can_interact:
			if current_interactions:
				can_interact = false
			
				await current_interactions[0].interact.call()
			
				can_interact = true



func _process(_delta: float) -> void:
		if current_interactions and can_interact:
			current_interactions.sort_custom(_sort_by_nearest)
		
	
func _sort_by_nearest(area1, area2):
	var area1_dist = global_position.distance_to(area1.global_position)
	var area2_dist = global_position.distance_to(area2.global_position)
	return area1_dist < area2_dist
	
func _on_area_3d_area_entered(area: Area3D) -> void:
	current_interactions.push_back(area)


func _on_area_3d_area_exited(area: Area3D) -> void:
	current_interactions.erase(area)
		

INteractable script (each interactable object is on the same layer and within a class if that helps):

extends StaticBody3D

func interact():
	print("interacted")
	queue_free()

Thank you so much :D!!!

Create an area node. 2d or 3d depending on your needs. Create an export var. Drag the scene into the export. Now, it will be stored within the scene as a compressed scene.

When a body enters the area - check if it is named player, in group players or whatever you do to decide if its the player that has entered or not.

If player: instantiate the variable and append it to body.inventory

Queue free.

If you want to i could fix it for you.

You’ve posted before, and I’ve mentioned using resources but I’d be happy to give a thorough example. From your other posts you seem to have an inventory already built, which is important to say as the main thing you seem to be asking is how to connect your interact script to the inventory.

So the first question is “Where is your inventory relative to this interactable?” How do you give control of the inventory all the way to this interact() function; it seems like your “Interactable Instance” may be related to the player quite directly, if it is a child of the player scene then you can access the player through a @export variable, then pass the player into your interact calls.

extends Node3D

@export var player: Player ## new line
var current_interactions := []
var can_interact := true


func _input(event: InputEvent) -> void:
	if event.is_action_pressed("interact") and can_interact:
		if current_interactions:
			can_interact = false

			await current_interactions[0].interact.call(player) ## added `player`

			can_interact = true

Your intractable objects must take a player parameter now, and your player can use a new add_item function to place what ever you want into their inventories.

extends StaticBody3D

@export var held_item: InventoryItem ## new line

func interact(player: Player):
	print("interacted")
	player.add_item(held_item) ## new line, you must make `add_item` on your player
	queue_free()
1 Like

thank youuu :smiley: but if my interactable instance isnt a child of the player, but a child of the object, what do i do?

Does that mean these functions are push_backing the player? Or does your player have an Area3D for interactions in addition to a body? Could you share your interactable and player scene tree?