Exit from Inventory to current screen

Godot 4.2

Hi, guys! This might be a pretty dumb question since I’m a bit new to Godot, but I’ve been having a bit of trouble with my Inventory system. I have gotten some semblance of an inventory started up and properly displayed. My only problem is that I’m not sure how to make the inventory exit button so it exits on whatever screen the player is on. I’ve been fiddling around with it for a few days but am a bit stumed on how to go about it.

Here’s the inventory script:


extends Node

class_name Inventory
var inventoryslot : Array[InventorySlot] 
@onready var window : Panel = get_node("inventorywindow")
@onready var info_text : Label = get_node("inventorywindow/infotext")
@export var starter_items : Array[Item]

func _ready ():
	
	
	for item in starter_items:
		_add_item(item)
		
func _process (delta):
	pass
func toggle_window (open : bool):
	toggle_window(false)
	for child in get_node("inventorywindow/slotcontainer").get_children():
		inventoryslot.append(child)
		child.set_item(null)
		child.inventory = self
		
		if Input.is_action_just_pressed("inventory"):
			toggle_window(!window.visible)
		
func on_give_player_item (item: Item, amount: int):
	pass
func _add_item (item : Item):
	var inventoryslot = get_slot_to_add(item)
	
	if inventoryslot == null :
		return
	
	if inventoryslot.item == null :
		inventoryslot.set_item(item)
	elif inventoryslot.item == item:
		inventoryslot.add_item()
	pass 
func remove_item (item : Item):
	var inventoryslot = get_slot_to_remove(item)
	
	if inventoryslot == null or inventoryslot.item == item:
		return
	
	inventoryslot.remove_item()
	
	pass 
func get_slot_to_add (item : Item) -> InventorySlot :
	for inventoryslot in inventoryslot :
		if inventoryslot.item == item and inventoryslot.quantity < item.max_stack_size:
			return inventoryslot
	
	for inventoryslot in inventoryslot:
		if inventoryslot.item == null :
			return inventoryslot
	
	return null 
func get_slot_to_remove (item : Item) -> InventorySlot:
	for inventoryslot in inventoryslot:
		if inventoryslot.item == item:
			return
	
	return null 
func get_number_of_item (item : Item) -> int:
	return 0

,,,

And right now the exit button its own script and node:

,,,

extends Button

func _on_pressed():
	get_tree().change_scene_to_file("res://parkscreen1.tscn")

Right now I can only get it to change to the main screen, but am aiming to get it to change to whatever the current screen is.

Thanks for the help!

I split my inventory system in two: the part that stores the data, and the GUI that shows the data. The latter is an autoload CanvasLayer that is hidden by default. There’s a second autoload that keeps track of game state. When the player presses ‘enter’, the game state is set to the ‘menuing’ state. In this state, the canvas layer is made visible. Likewise, exiting the ‘menuing’ state makes the CanvasLayer invisible again.

I don’t really ‘switch’ from the overworld to the inventory menu; I simply draw things based on game state. The inventory menu is always there, just hidden.

1 Like

Your code formatting is a bit broken, just wrap three backticks before and after the whole code block, like this
```
code
```


Do I read this right? Is this the toggle_window function?

it seems a bit like the code got mixed up:

  • there is no reason to call toggle_window(false) in the function itself (recursively)
  • there is no need to do the for-loop in toggle_window, you only should do this once, e.g. in _ready()
  • the only thing you need to do in toggle_window() is actually making the window visible, e.g. with this line window.visible = open
  • the two lines checking for input ( if Input.is_action_just_pressed …) should be in _process(), or better you write it in the _input() like this:
func _input(event: InputEvent) -> void:
	if event.is_action_pressed("inventory"):
		toggle_window(!window.visible)

edit:
Forgot the exit button:
here you only need to get a reference to the node with the Inventory script and call
toggle_window(false)
If the exit button is part of the window, you don’t need to access the inventory script, you can connect the pressed-signal to a script on the window (instead of the button) and use a function like this:

func _on_pressed() -> void:
    visible = false
1 Like

Thanks for the reply. I got the inventory to disappear, but now I’m trying to figure how to make it so it goes back the the screen the player was previously on.

I didn’t read your code because as @substain said, you need to format it correctly. If you can’t take the two seconds to format it, why should we take the much larger amount of time to try and decipher it?

Having said that, I recommend you look into How to Pause the Game and then just change the visibility of your inventory screen. You can change the UI’s process mode to When Paused or Always.

1 Like

Hey there, thanks for the reply,

Sorry if the code seemed confusing, this was my first time posting on here and I wasn’t sure whether to put the whole code or just part of it in hopes of making my question seem a bit clearer. Sorry for the confusion.

Looks like I got it working! Thanks for the advice! I’ll try to be a bit more clear on my code format next time.

1 Like

Glad it’s working. The amount of code wasn’t the issue (though focusing on the problem area can help), it was the formatting.

1 Like

Ah, gotcha. I have been having trouble with the organizing part that comes with coding, so I guess that’s another thing to look into.

I recommend you start here:

1 Like