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.
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!
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.
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.
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:
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.
“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.
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.
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