How to create collectible items?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By iconicity
:warning: Old Version Published before Godot 3 was released.

I’m very very new to Godot so apologies in advance.

I’m making a project and I want a very simple inventory. I have made gems (and various other objects throughout the levels) for the player to collect, and my goal is to have a larger, non-interactable object in the corner with text for how many of something the player has.

At this point I can’t even figure out how to get the gems to disappear when the player collides with them. They collide normally, but I can’t figure out how to make them disappear.

I think the issue is with trying to control collisions through the player which is a kinematic body, not a rigid body…? I’ve been trying queue free() in different scenarios, through the player which won’t have it, and I experimented under the gem code for queue free(self) but I don’t even know if that’s a legitimate command.

Thanks so much. I’ve probably spent 4 hours today in tutorials but they’re all so different and nothing is working for me.

I’m doing this exact thing in my current tutorial: Godot 101. I haven’t shown the gem collecting yet, but that’s the next video, due out in a couple of days.

I’m just using Area2D for my player, but it’s the same process to do it with Kb2D if the gems are Area2D (since you don’t need to collide/bounce off the gems, they don’t need to be a PhysicsBody). With Area2D on the gems, it’s just connecting the on_body_enter()signal to a callback that does queue_free() and the gem disappears.

How are your nodes set up?

kidscancode | 2017-02-26 00:44

:bust_in_silhouette: Reply From: avencherus

Do you have any code or setup to share? We can only blindly speculate on what you’re not referencing.

Also, queue_free(self) is not valid. It takes no arguments, and returns nothing:

What queue_free() does is free the node calling it on the next idle frame. They exist on all nodes that inherit from Node.

If you just call queue_free() as is in any node’s script, it is performed on the node itself. So no need to specify anything about the caller.

If you want to get rid of other nodes outside the node’s script, you have to obtain their reference. Such as:

var item = get_node("path_to_node")
item.queue_free()
:bust_in_silhouette: Reply From: eons

Look at platformer demo to see the collisions with coins and/or the kinematic character demo on the princess detection.

The rest, how and what to do on collision, depends on your design, but you may not want to free nodes that should react to collision/overlap.

An option is to put the colectibles on a group and call a common method (like collect) to do something like animating, adding the value to the corresponding script/node and other things before setting free the node.