Why cant I call this function

Godot Version

4.2.2

Question

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()

This is the script where the function is in:

func _dead():
	max -= 1

You’re not adding spawner to your scene tree before calling _dead().

the spawner is alway in the scene tree

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.

1 Like

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: :point_left: this isn’t true, you don’t get an error, apparently I just made this up :drooling_face:

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 :man_shrugging:

i also tried it without underscore but still doesnt work

That might be related to the other replies then?
What is the actual error you are getting, can you paste the exact error please?

“Invalid call.Nonexistent function ‘dead’ in base ‘Node2D’.”

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…

1 Like

yea but if i dont instantiate it still doesnt work

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.

FYI, making mistakes is a great way to learn!

1 Like

Can you screen shot your spawner’s scene tree? I believe your script isn’t attached or is attached to a child of the scene.

1 Like

My understanding is that Godot does not have real private variables, and the underscore is just a suggested style.

2 Likes

Game scene tree:
Screenshot (47)
Spawner Scene tree:
Screenshot (48)

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:

$Skill/Spawner._dead()

That should be all.

2 Likes

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 :drooling_face:

Just tried again to be sure, and yeah, no errors at all… :exploding_head:

dont work the error say: " Attemp to call function ‘dead’ in base ‘null instance’ on a ’ null instance’

Is this script from an enemy that the spawner spawns?

If so you will have to provide enemies with a reference to the spawner, not instantiate a new spawner.

sry i dont really understand