Trouble duplicating enemies

Godot Version 4.4.1

Hello!

I am new to this, only been at it a few days. I am making a 2D platform game to start, and when I duplicate my ‘Boar’ enemy, it doesn’t work and crashes the game. The original Boar works fine, and the duplicated one is named ‘Boar2’.

From what I understand, my problem occurs because they are sharing the same script and such - I looked through some answers here and on reddit, and am still a little lost since it appears code can be formatted in a few different ways. I do have my Boar dropping a ham upon death, and that works fine with the original but not a duplicated one. I did find out that the Boar(s) work if they are not in the same parent node in the world, but I definitely don’t want 50 separate parents just for one enemy. :rofl:

I have tried duplicating via the CTRL + D method as well as dragging in the scene.

Below is the code for my Boar - forgive me if it seems convoluted!

Thank you for your time!

What’s the error you are getting?

How are you spawning the new boar? Are you loading the scene itself (var boar = load("res://Scenes/Boar.tscn for example) or just calling Boar.new()? If it’s the latter then that’s the problem as the script does not know anything about the scene and it’s the scene resource the one in charge of instancing all the nodes (like the AnimatedSprite2D).

I took a video but since I am new it won’t let me upload it - I am clicking and dragging the boar scene into my world/level scene. The original works, but it seems to be when I enter the radius of the player detection for the new boar that I get the above error.

Thank you for replying!

I’m guessing your error is pointing to line 25 in the script? Because we can’t see that in the screenshot.

If so, I think your reference to Knight is messed up. When you duplicate your Boar, is the duplicate exactly in the same position (has the same parent) as the original Boar? Because the reference to Knight is based on a very specific relative position of the node in the tree, so if you change the position of the Boar, it won’t work anymore.

Can you show a picture of your scene tree?

Sure can! The one in the OG post is the Boar, I’ll attach the Knight (separate reply since I can only post one image at a time) and duplicate the error so you can see the tree:

Error with Duplicate Boar:

I read something about making them an instance, but I am not sure how to do that - I duplicate the Boar either by CTRL + D and moving it from the OG Boar or dragging in the Boar Scene from the left.

Sorry if I am confusing -

Knight page 1 -

Knight page 2 -

Thank you for helping!!

Where is your second (duplicated) Boar scene? I see only one on the screenshot you provided.

I bet one the second boar isn’t a sibling of the first, so this:

Knight = get_node("../../Knight/Knight")

is not resolving and coming back null.

You probably want to make that an absolute path, like $World/Knight/Knight.

1 Like

Ok! I will give it a try - for my understanding, what is an ‘absolute path’?

You can also mark your Knight node to have a Unique Name, then you can access it simply as %Knight no matter where it is in the scene.

“Absolute path” is meant to be the same regardless from where you are trying to obtain said path.
As opposed to the “relative path” which is relative to the original node you are trying to obtain it from and might return a different result when called from a different node.

1 Like

An absolute path is one that specifies all the components.

Say we’re trying to get to /foo/bar/baz and we’re at /foo/bar/quux; we could do a relative path:
../baz
We could do an absolute path:
/foo/bar/baz
The relative path is relative to where we are. The absolute path specified the complete path, and so is location independent.

2 Likes

MAKING THE KNIGHT UNIQUE WORKED!! I was able to kill both Boars and they both turned into hams as they should!!! Thank you all so so much :smiley:

2 Likes

Ohhhh I getcha!! Thank you so much! I did try the $World/Knight/Knight but I think I added it in wrong - the unique route worked though. It’s so interesting how many different ways there are to do things :smiley: