Let’s Learn Godot 4 by Making an RPG — Part 13: Enemy Dropping Loot on Death🤠
Tags: gamedev, beginners, tutorial, programming
Published: 2023-07-01 03:10:00


We can finally shoot and kill our enemy, but we can do one better. I want us to upgrade our enemy’s death function so that our enemy drops loot that we can pick up. This loot drop will spawn at a 90% chance, and the loot will be randomized from our Pickups enum. This means our enemy might drop either ammo, a health drink, or a stamina drink.

---

>  ***WHAT YOU WILL LEARN IN THIS PART:***
>  · How to randomize percentage and Enum return values.

---

In our death conditional in our damage() function, we need to use our randomized method to randomize the loot change percentage. If the value is less than 0.9 (90%), we will randomize a pickup. This pickup will also be randomized according to the number of enum values we have, which is 3. This pickup will then be added to our scene and spawned at our killed enemy’s location.
```python
    ### Enemy.gd
    extends Area2D
    
    # older code
    
    #will damage the enemy when they get hit
    func hit(damage):
        health -= damage
        if health > 0:
            #damage
            animation_player.play("damage")
        else:
            #death
            #stop movement
            timer_node.stop()
            direction = Vector2.ZERO
            #stop health regeneration
            set_process(false)
            #trigger animation finished signal
            is_attacking = true     
            #Finally, we play the death animation and emit the signal for the spawner.
            animation_sprite.play("death")
            death.emit()
            #drop loot randomly at a 90% chance
            if rng.randf() < 0.9:
                var pickup = Global.pickups_scene.instantiate()
                pickup.item = rng.randi() % 3 #we have three pickups in our enum
                get_tree().root.get_node("Main/PickupSpawner/SpawnedPickups").call_deferred("add_child", pickup)
                pickup.position = position
```
---

>  ***What is call_deffered?***
>  [call_deferred](https://docs.godotengine.org/en/stable/classes/class_object.html) is a method that allows you to defer the call of another method to the end of the current frame or cycle. This can be particularly useful in situations where you want to make changes to the scene tree or other objects but don’t want those changes to take effect immediately during the current function or signal.

---

If you now run your scene and you kill your enemy, they should drop loot at a 90% chance. You can lower this value to 50% or 60% if you want — set it according to your preference of how probable you want your enemy to drop loot on death.

![Godot RPG](https://cdn-images-1.medium.com/max/3840/1*uF-VdPHALBFDziM9XLwOLw.png)

Now that our enemy drops loot, we don’t have to continuously find pickups. In the next part we will add the functionality to our enemy to be able to shoot at our player and damage us. Remember to save your project, and I’ll see you in the next part.

The final source code for this part can be found [here.](https://github.com/christinec-dev/DustyTrails_RPG_Parts/tree/main/Part_13)

<a href='https://ko-fi.com/J3J2LP2U5' target='_blank'><img height='36' style='border:0px;height:36px;' src='https://storage.ko-fi.com/cdn/kofi2.png?v=3' border='0' alt='Buy Me a Coffee at ko-fi.com' /></a>
---
### FULL TUTORIAL

![Godot RPG](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/pd9n6khb9jtaj0bynp7e.png)

The tutorial series has 23 chapters. I’ll be releasing all of the chapters in sectional daily parts over the next couple of weeks.

If you like this series or want to skip the wait and access the offline, full version of the tutorial series, you can support me by buying the offline booklet for just $4 on [Ko-fi!](https://ko-fi.com/s/19c32424e5)😊

You can find the updated list of the tutorial links for all 23 parts in this series [here.](https://oops-i-devd.gitbook.io/oops-i-devd/godot-tutorials/dusty-trails/tutorial)