Loot drop by fishing, axe a tree and kill enemy

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By JeremiasDev

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

example pictures:

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.

Edit:
Please have a look at this.

RoniPerson | 2021-01-02 18:25

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

JeremiasDev | 2021-01-02 18:36

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 :slight_smile:

RoniPerson | 2021-01-02 19:12

Thanks, i write back If i have questions

JeremiasDev | 2021-01-02 19:14

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

JeremiasDev | 2021-01-02 22:07

:bust_in_silhouette: Reply From: RoniPerson

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():
    set_process(false)

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

func _process(delta):
    self.queue_free()

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

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()
    item.init(id)
    # 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
    get_node(scene_root).add_child(item)
    item.position = position