0 votes

Hi, i want a loot drop system. If the player kill a enemy or fishing a fish or axe a tree...

example pictures:

enter image description here

enter image description here

Godot version 3.2.3
in Engine by (86 points)

Do you have an working inventory system?
If yes just work with item objects maybe an area 2d. You could add those as child of the scene when killing something. When colliding with the player change the inventory values.

If you don't have an inventory system there are tutorials for this.

Please have a look at this.

Yes,i have a inventory system but how can i make it, that objects are dropped? and the random? Can you write a Code?

This problem consists of two parts.
Part 1: collectable items in your scene
This basicly works like coins. For this there are tutorials.
Additional each item has to store its item id.
Baaed on this you can change the correaponding inventory value

Part 2 droping items (including randomnes)
This can be solved in diffrent ways. But i would suggest using a autoloaded manager for this. In it you have a dictionary of loottables.
The key is the name for the loottable e.g. "fishing".
The value which correspond to a key is a list. This contains more list.
Each of it contains 3 values. At index 0 the weight. At index 1 the item id.
At index 2 the amount.
The class has a function invoce_drop or so. It takes an position and the name of a loottable. Based on the weights you can choose an entry of the loottable list.
For weighted randomness have a look at this.
Based on the entry for value and item you can now add items from part 1 to the scene.

Try to figure out the code for yourself :)

Thanks, i write back If i have questions

Hey, I tried it but didn't work, I know how to do a random generator and also how to store the id of the item but I don't know how to do the items In the case of wood that drops from a tree. Please write me a code where I can do that. thanks

Edit: It doesn't have to be random

1 Answer

0 votes

There are two ways of doing the droping item. First make a custom scene for evry item.
Or the way that I would use one scene inheriting from Area2D. Under the root of the scene add an animated sprite. And for evry item add an animation with the item id as name.
Add a script to the item.

extends Area2D

var id

func _ready():

# has to be called before adding the node to the scenetree
func init(id):
    self.id = id

func _process(delta):

# has to be connected to the corresponding signal of Area2D
func _on_Coin_body_entered(body):
    if body.has_method("on_item_collected"):

Now how to drop the items from the manager

onready var item_scene = preload("path/to/your/scene.tscn")

# the node which keeps your scene elements. The items will be added as child of it
# in a real game you should get this dynamcly or change it when the level changes
export(NodePath) var scene_root

func drop(id, position):
    var item = item_scene.instance()
    # in a real game you may add a little bit of randomnes to the position to not let all items be at the same place
    item.position = position
by (331 points)
Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.