Best way to pick random enemy for an RPG

Godot Version

4.2.1

Question

So I’m making an RPG where I got a combat screen that hides/shows when a new combat is triggered, then chooses a random enemy and replace the stats with the corresponding ones.

I first did it with a script that has a dictionary and went well, but can’t add functions for special behaviour. So I tried with resources, one custom resource for each enemy, then iterate the directory with dirAccess.

This worked at first but really slowed down the game.

What is the correct way of doing this?
thanks in advance

What do you iterate through the directory for? Just keep a list of paths, each pointing to one of your resources, then pick one at random and load it:

var enemies = [
	"res://enemy1.tres",
	"res://enemy2.tres",
]

func _ready() -> void:
	var random_index = randi() % enemies.size()
	var enemy_resource = load(enemies[random_index])
2 Likes

Pf you’re right. I overthinked that. thanks!

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.