Godot Version
4.2.2
Question
Hello! I am making a game like vampire survivors, and I am trying to figure out how to make the player detect the type of enemy it is colliding with, so each enemy can have a different damage.
This is my code for the player taking damage:
func _physics_process(delta):
...
const DMG_RATE = 10.0
var overlapping_mobs = %Hurtbox.get_overlapping_bodies()
if overlapping_mobs.size() > 0:
health -= dmgRate * overlapping_mobs.size() * delta
%ProgressBar.value = health
So right now the damage rate is stored in a constant, but I would like it to change depending on what type of enemy is overlapping with the player hurtbox.
Hey
You already have all the information you need with this overlapping_mobs
right now your variable is an array of PhysicsBody2D, you could create a class like this:
class_name Mob
extends PhysicsBody2D
# You can inherit anything inheriting PhysicsBody2D btw, CharacterBody2D, RigidBody2D, StaticBody2D
var damage_rate = 1
Once you have your mob created and customized with it’s own damage_rate, you want to tweak a bit your method like so:
var damage_rate: float = 0
var overlapping_mobs = HurtBox.get_overlapping_bodies()
for mob in overlapping_mobs:
if mob is Mob: # Making sure the field will be there
damage_rate += mob.damage_ratio
if overlapping_mobs.size() > 0:
health -= damage_rate * delta
%ProgressBar.value = health
Hope that helped!
1 Like
Thank you, I kind of understand but not really. I’m new to godot and never used classes before.
I want to have different types of enemies, so for example, a zombie and a skeleton. The zombie deals 10 damage and the skeleton 15. I had a zombie scene where I did what you said with the class name, then I tried duplicating the scene to make the skeleton, but when I change something in one script it changes in the other one too. I tried the option Make Sub-Resources Unique
but then there’s an error in the class declaration.
It is okay, there is a ton of concepts if you are really new to this world, never forget that you can always come back to something you did previously with new ideas and improvements
Yesterday solution might not be the most optimized one but it got you here today so never be affraid to tinker and fiddle with everything you found, don’t run after “the correct way” each problems have tons of different solutions and I would focus and making things work and on gaining experience
About your two differents Mobs, has stated previously there is multiple ways to handle that:
You could have inheritance:
class_name Zombie
extends Mob
func _init() -> void:
damage_ratio = 10
class_name Skeleton
extends Mob
func _init() -> void:
damage_ratio = 15
This is a bit weird to have inheritance without having child redefining anything and just setting field to a value
You could have some kind of factory pattern:
class_name MobFactory
# defined here for convenience but should be stored in a config file along with the actual stats
enum Type{ZOMBIE, SKELETON}
static func create_mob(p_mob_type: Type) -> Mob:
var result: Mob = preload("res://mob.tscn").instantiate()
match p_mob_type:
ZOMBIE:
result.damage_ratio = 10
SKELETON:
result.damage_ratio = 15
return result
func spawn_mob() -> void:
var new_mob = MobFactory.create_mob(MobFactory.Type.values().pick_random())
%PathFollow2D.progress_ratio = randf()
new_mob.global_position = %PathFollow2D.global_position
add_child(new_mob)
So yeah, I would go for something like the factory but it really depends on all the structure and link your code have that I am not aware of
Sorry for the TL;DR
Hope that helped you, don’t give up and good luck!