I cant call a function and the error say that they cant found the function in that node2d but the Node2d have that function, pls help.
This is the script to call the func:
const SPAWNER = preload("res://GameScene/Enemy/spawner.tscn")
func take_hit(amount: float) -> void:
if health_barr.value <= 0:
var spawner = SPAWNER.instantiate()
spawner._dead()
queue_free()
Calling the instantiate method doesnât mean it was added to the scene tree. You need to add it by calling the add_child method in your tree.
If it is already there as you said, you donât need to call the instantiate method, but refer to itâs instance in the scene tree and just call your functions.
Try to add a unique name to your spawner (like %Spawner) and call the function using %Spawner._dead().
Also you can add an export variable and add your spawner to it, so you can use this instance to call your function.
By doing that, you are instantiating a new Spawner (instance of the scene: res://GameScene/Enemy/spawner.tscn).
Are you sure is that you want to instantiate?
Or you want to have an instantce of Spawner, that internally spawn a new Enemy ?
Also, as everyone said before, you need to add
the instantiated scene to your tree to have some effect, but that doesnt affect the call to _dead() func.
Youâve made _dead() private by starting it with an _ (underscore).
The editor still seems to auto-complete private methods for you, but youâll get an error at runtime. Edit: this isnât true, you donât get an error, apparently I just made this up
Prepend a single underscore (_) to virtual methods functions the user must override, private functions, and private variables
Yes, itâs a bit confusing that virtual methods (i.e. _ready, _process etc) that you have to override also start with an underscore
Iâm assuming thatâs after youâve tried removing the _ from both scripts. If that is the case, then I can only assume your spawner scene doesnât actually have the script with the dead method attached.
You are probably going to need to post more of your scene tree and more of the scripts to make sure everything is wired up correctly.
Also itâs worth thinking about what the other replies said. The fact you instantiate the spawner then try calling dead on it, seems incorrect. So, Iâm not sure you are going to get the result you expect either wayâŚ
It really sounds like youâve got a fundamental misunderstanding of something.
Iâd suggest going back to the documentation and/or tutorials to better understand the basics of Godot. This would let you figure out what mistake youâve made, and will teach you much more than just getting the answer from the forums.
People on the forums will be able to help you if you post more of your code/scene tree. Iâm just not sure thatâs the best thing for you in the long run.
You have a single Spawner in your game, so get rid of your preload since you donât need it. Donât instantiate a new spawner since you donât need it. You need:
My bad, I was sure I got a runtime error when I tried to access a âprivateâ variable (I was testing something and didnât bother renaming the variable).
Maybe something else errorâd and I just assumed OR I invented the whole thing
Just tried again to be sure, and yeah, no errors at allâŚ