Small Problem With A Tutorial

Godot Version

4.6

Question

Hellooooooo! :smiley:

Im following this tutorial on how to make an inventory,
How To Create an Inventory System In Godot 4.2 In 20 minutes

and its been really good so far! But at 21:51, he pulls up the inventory through a button he didn’t teach us how to set up. How would one bring up the inventory like that? Is it just a pause menu and a button node to get to the inventory? How would I connect the inventory to it?

Following a tutorial is good. For this scope I would only recommend learning and probably
making your own dictionary with resource scripts u can pull from and assign to slots
that you spawn when game starts from empty slots… then moving through the index with
keys and having some sort of display cursor for the current slot your on.

I made my own inventory and storage containers all with save like the old resident evil games.
I could always provided script and run down if there was enough demand for it.
The video is on my youtube.
Good luck

1 Like

If it’s a YouTube video, check the comments. Probably someone else had this problem and asked.

3 Likes

I did try that first, but theres only 13 comments and none of them were about it :,D

Ill probably look through his series and try to figure it out though, I was just wondering if there was a way to open it at the press of a button

I`m making my next video a inventory system.
Starting with UI grid setup and resource and using item and picking up.
I have a roadmap from starting, picking up, stacking items, tooltip to equip or use.
Then saving and loading and storage boxes… hope it will help.

1 Like

Okay Ive got it half working!! :smiley: (Im very proud of it, at least now ive got a prototype!!)

Now, when i go into my game, it shows up automatically and i cant click out of it. My mouse isnt showing up though, either, and the inventory button isnt working?

Heres the problem code

		
func open():
	if Input.is_action_just_pressed("Inventory"):
		if isopen == false:
			visible = true
			isopen = true
			Input.mouse_mode = Input.MOUSE_MODE_VISIBLE
		elif isopen == true:
				visible = false
				isopen = false
				Input.mouse_mode = Input.MOUSE_MODE_CAPTURED
1 Like

At 7:41 there is some input. … in the code. There he switches the ā€œContainerā€ to the state of the pause. So if pause is true, than the ā€œContainerā€ is visible.

1 Like

I use mine like this

func _unhandled_input(event: InputEvent) -> void:
	if event.is_action_pressed("toggle"):
		inventory_open = !inventory_open
		self.visible = inventory_open
		update_selection_position()
	if inventory_open == true:
		if event.is_action_pressed("left"):
			selected_index -= 1
			selected_index = (selected_index + max_slots) %max_slots
			update_selection_position()
		elif event.is_action_pressed("right"):
			selected_index += 1
			selected_index = (selected_index + max_slots) %max_slots
			update_selection_position()
		if event.is_action_pressed("attack"):
			use_item()
1 Like

But it’s not a good tutorial. I don’t like the code. I wouldn’t get nodes (by name) and get_tree every _process.

1 Like

Okay so i tried troubleshooting :slight_smile:

And i got this code (which has worked before on a different thing ^^)

And its still not working, still showing up onto the screen and not closing when i press the button. I tried putting this code in both the inventory script itself and the player script in which its parented to, and ive also made sure i spelt the input name name right, but i still cant open and close it, it still is open by defualt, and i still cat see my mouse.

Although i do only have one quiestion after this (put i gotta get this working first ^^)

func open():
	if Input.is_action_pressed("Inventory"):
		if inventory == visible == false:
			visible = true
			Input.mouse_mode = Input.MOUSE_MODE_VISIBLE
		elif inventory == visible == true:
				visible = false
				Input.mouse_mode = Input.MOUSE_MODE_CAPTURED

Maybe you want inventory.visible == false?

What is the intention here?

Maybe (i put it in)

I wanted to make it so that if you pressed the Inventory button, it would check if it was already closed/open (nonvisible/visible) and act accordingly

Most of the time you don’t check such flags. You just flip them. bool_value = !bool_value that assigns the opposit of itself to the variable. Like visible = !visible. And if you want to compare more than two values I would use && an ||(AND and OR).

1 Like

Okay :smiley:

Ive got another thang half working!! ^^ my problem now is two things:

(For context, i now have this on a canvaslayer)

  1. Only the panel is showing up on ready, not the actual inventory
  2. When i press ā€˜i’ to close the inventory, its never able to become visible again.

(ive been experimenting with the code, im sure i just made a simple mistake :,D)

func on_ready():
	visible = false
	
func _process(_delta):
	if Input.is_action_pressed("Inventory"):
		if visible == false:
			visible = true
			Input.mouse_mode = Input.MOUSE_MODE_VISIBLE
	if Input.is_action_pressed("Inventory"):
		if visible == true:
			visible = false
			Input.mouse_mode = Input.MOUSE_MODE_CAPTURED
  1. I would set visible in the Editor to false. But it’s ok to switch it off on_ready().
  2. Why on_ready() and not in _ready() ?

The if logic is a little bit doubled. Use ELSE.

func _process(_delta):
	if Input.is_action_pressed("Inventory"):
		if visible == false:
			visible = true
			Input.mouse_mode = Input.MOUSE_MODE_VISIBLE
		else:
			visible = false
			Input.mouse_mode = Input.MOUSE_MODE_CAPTURED


or better:

func _process(_delta):
	if Input.is_action_pressed("Inventory"):
		visible = !visible
		if visible == true:
			Input.mouse_mode = Input.MOUSE_MODE_VISIBLE
		else:
			Input.mouse_mode = Input.MOUSE_MODE_CAPTURED

EDIT:

If it is a bool, you don’t have to check for == true or == false.
You can simply check for if visible:

func _process(_delta):
	if Input.is_action_pressed("Inventory"):
		visible = !visible
		if visible:
			Input.mouse_mode = Input.MOUSE_MODE_VISIBLE
		else:
			Input.mouse_mode = Input.MOUSE_MODE_CAPTURED

Omg thank youuuuu!!! :smiley: its consistently showing up now!

(I used the last one you gave me ^^)

small quiestion though?

when i press the I key, its fluttering, and it takes multiple presses for it to open/close?

That’s because you are checking for is_action_pressed. It’s better to check for down or up.
Default in Godot is on up. But I prefer on down, it’s snappier.
is_action_just_pressed or is_action_just_released

1 Like

Omg thank you!!!

One more quiestion (For this part at least :,D)

Its only the panel/canvas layer showing up, not the actual inventory. Any way to fix that? (The inventory is parented to the canvas layer)

Also how do i get it so that it shows up hidden insead of shown like it is right now?

For that I would need to see your scene tree (the node hierarchy). Could you make a screenshot of your nodes? (or the tscn as text)

1 Like