Make child inherit Area2D from parent

Godot Version



New to gamedev. So I’m trying to make an enemy system like in terraria, e.g. a universal Enemy class, within it are enemy AIs, within those are individual enemies.

I got everything working fine, I can change variables on individual enemies that are exported from their parent and grandparent, but the enemies don’t have the Area2D hitbox and hurtbox that their grandparent has.

I have an Enemy node which also creates the class EnemyClass, and within it are two Area2D nodes, each with a CollisionShape2D. Within Enemy is also AI_Fighter, which extends EnemyClass, and within that are my individual enemies, which extend AI_Fighter.

I export hitboxradius and hurtboxarea in Enemy, and in each of the Area2D I set the radius & area of their CollisionShape2D to hitboxradius and hurtboxarea. I can change the values of hitboxradius and hurtboxarea in each individual enemy, but they just dont spawn with either of the collision boxes.

Can I make the enemies inherit the Area2D from the main enemy node? Am I doing something wrong? Is there a better way to do this? Any help is appreciated, thanks for reading.

Hi can you post the code that spawns the enemies

I think you would have a better time making a base enemy scene and inheriting the scene between enemies.

var wave1 = [["slime", 1], ["slime", 1], ["redslime", 1]] #and so on
var wavelist = [wave1, wave2, wave3, wave4, wave5, wave6]

func start_next_wave():
	var wave_data = retrieve_wave_data()

func retrieve_wave_data():
	return wavelist[current_wave]

func spawn_enemies(wave_data):
	for i in wave_data:
		var new_enemy = load("res://Enemy/" + i[0] + ".tscn").instantiate()
		add_child(new_enemy, true)
		new_enemy.global_position = get_random_position()

Here it is.

So making individual enemies direct children of the base enemy scene will make them inherit the Area2D? But then how do I handle the AIs? I don’t suppose i can attach both the individual enemy script and the AI script to a node?

I could be off base, but why not make a generic Enemy class, then extend that class for each specific enemy (slime/orc/etc)?

The Enemy class can have whatever functions / logic / AI that is shared among all enemies.

Then in the game you just instance a slime or orc node or whatever when you need that enemy.

The problem is I want to make multiple AIs, being able to pick a different AI for each enemy, with some enemies sharing AI.

You can extend a script on an inherited scene.


1 Like

Individual enemys would be a inherited scene of the base, not children


1 Like