I'm making a 2d platformer that has a player and some different enemies. Now I'm trying to add a knockback effect to the player whenever he gets hit. To do this I need to know the enemy's position.x so that if the player get hit from the right of the enemy, then he should have a knockback to the left, and vice versa.
The problem is that I have different types of enemies in my main scene and they have different node names. For example one is named "Skeleton" and the other is named "Goblin". So I can't really write:
var enemy = getparent().getnode("Skeleton")
Because then I would only get the skeleton and not the other enemies.
How can I reference these in my script even though they have different names from each other?
I tried to do this using an enum that I made in the enemy script which lets me choose what type of enemy I want. It looks like this:
export(String, "Skeleton", "Goblin") var Enemy_Type
And then I added this in my player script:
onready var enemyScript = "res://Scripts/Enemy.gd"
onready var enemy = get_parent().get_node(str(enemyScript.Enemy_Type))
I really thought that this would work but instead I got an error message saying:
Invalid get index 'Enemy_Type' (on base: 'String')
If anyone could tell me what I did wrong in my script or give me another method to do this then that would be highly appreciated.