DEV Community

Cover image for Let’s Learn Godot 4 by Making an RPG — Part 13: Enemy Dropping Loot on Death🤠
christine
christine

Posted on • Updated on

Let’s Learn Godot 4 by Making an RPG — Part 13: Enemy Dropping Loot on Death🤠

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.

    ### 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
Enter fullscreen mode Exit fullscreen mode

What is call_deffered?
call_deferred 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

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.

Buy Me a Coffee at ko-fi.com

FULL TUTORIAL

Godot RPG

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!😊

You can find the updated list of the tutorial links for all 23 parts in this series here.

Top comments (0)