Need help with an item/weapon pick up system

Godot Version

Player:

var inventory = {
	item = null,
	melee_weapon = null,
	ranged_weapon_1 = null,
	ranged_weapon_2 = null,
}

Item:

func _ready():
	weapon_ID = 1
	type_ID = "ranged_weapon_1"
	collision = $CollisionShape2D

func _on_area_entered(area):

Base pickup script:

# Identification.
var weapon_ID : int
var type_ID : String

# Collision.
var collision : CollisionShape2D

Question

I want to make an item system for my 2D shooter. The way I thought of doing this is by having there being 4 different inventory slots. I’m trying to have it so when the player presses the interact key the interact area enables for a second. When the item interacts with it would disappear and its weapon ID and type ID would be sent to the player so it understand what inventory slot it should go in, but i’m not sure how to send that information over

sounds like your _on_area_entered function has a reference to the player by area, depending on how your player scene is set up you can get the player script from that variable passed in.

For example assuming the player script is attached to the Area2D

func _on_area_entered(area):
	if area is Player:
		area.inventory  = self.type_ID

I have it set up, but nothing changes inside of the inventory.

How are you editing the inventory? How are you accessing it? Can you share some more code?

func _on_area_entered(area):
	if area is Player:
		area.inventory.ranged_weapon_1 = self.weapon_ID

Have you confirmed if this code runs, or does it error? Does area is Player align with how you created your player? Can you share your player scene tree?

The code does run; the area is player seems to align with how I created it; and my scene tree is

image

The problem is that you are checking for the area-collision but the player is the characterbody. Therefore the ā€œarea is playerā€-condition evaluates to false

1 Like
func _on_area_entered(area):
	if area is Area2D:
		Player.inventory.ranged_weapon_1 = self.weapon_ID

I changed it to this but it says it can’t find the inventory dictionary in the Player.

Does that form of dictionary definition work? In my stuff I’d do:

var inventory: Dictionary = {
    "item":     null,
    "melee":    null,
    "ranged_1": null,
    "ranged_2": null
}

And then:

Player.inventory["ranged_1"] = self.weapon_ID

i got it to work but when i interact with the item it says ā€œinvalid get index ā€˜inventory’ (on base: ā€˜null instanceā€™ā€ and crashes.
also i changed my inventory to hexgrid’s method.

Base script:
@onready var player = get_node("res://Scenes/Player.tscn")

Item:
func _on_area_entered(area):
	if area is Area2D:
		player.inventory["ranged_1"] = self.weapon_ID

Your script is attached to the CharacterBody2D which is a parent of the area

you could try this instead

func _on_area_entered(area):
	var player = area.get_parent()
	if player is Player:
		player.inventory["ranged_1"] = self.weapon_ID
1 Like

It worked

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.