Godot Version
4.4.1
Question
I’m trying to make 2d enemies and i have already made my first enemy which can detect gaps and follow the player.
What’s the best way to duplicate the enemy, change the texture and change some behaviours while having most of the code be similar to the original?
I have already converted my first enemy’s scripts to a separate “BaseScript” which it inherits from. For the other enemies, I will be inheriting from the same base script. Is that the best way to go about it?
Maybe you can do it something like this?
export_enum("Defensive", "Faster", "Chaser") var behaviour = "Chaser"
export var texture: Image
func _ready():
$Sprite2D.texture = texture
func _physics_process(delta):
match behaviour:
"Defensive":
#Add the logic here
"Faster":
#Add the logic here
"Chaser":
#Add the logic here
This is just the concept codes. You can customise it as you like.
Edit: There are also more two ways that you can do, but the above is easiest!
1 Like
Thanks for the response.
Apologies for the unclear question. I was looking to how I should duplicate code using good OOP (obj oriented programming) principles.
Right now, If i want to make a new enemy, I would copy and paste nodes from my first enemy into another CharacterBody2d and then changing the scripts manually so that it will inherit from the BaseStates/BaseScripts for all their scripts.
It feels inefficient because even though only the texture, health, speed, and dmg needs to be changed, I am still putting work in duplicating other things so thats why I have asked for help.
1 Like
I do this for the enemies in my game:
class_name NonCombatant
extends CharacterBody2D
# All the code shared between characters the player can damage
class_name Combatant
extends NonCombatant
# All the code shared by characters that will fight the player
class_name SpecificCombatant
extends Combatant
# This is where I define a specific enemy - their stats, attack, etc.
1 Like