0 votes

To be more specific, I’m trying to make a door disappear when the count of enemies that are children of a node reaches 0 so you can access the exit, but I don’t understand how to, I’m not even sure which node to attach the script to that knows the number of enemies.

in Engine by (20 points)

You should research AutoLoad and Singletons

1 Answer

+2 votes
Best answer

Sorry for that answer but I found another better solution

var enemy_parent = $Enemy_Parent #Reference to enemy's parent
var door_node = $Door #Door Reference
var enemy_count = enemy_parent.get_child_count()
print("enemy count: " +  str(enemy_count))
if enemy_count == 0:

Only set the reference to the nodes. The number of childs would be counted automatically by get_child_count()

by (909 points)
selected by

this is my code but it says "Invalid Call Nonexistent Function 'getchildcount' in base invalid
(I made those alterations because it does not allow me to use if without a function, and the same applies to print, also there are underscores where this message doesn't show them but they're supposed to be, I don't know why the code sample doesn't show them)

extends Node2D

onready var enemyparent = $EnemyParent #Reference to enemy's parent
onready var door_node = $Door #Door Reference

var enemycount = enemyparent.getchildcount()

func ready():
print("enemy count: " + str(enemy
func countem():
if enemy
count == 0:

alright so I changed it up now so that the enemy counter emits a signal that tells the door to destroy itself when the counter reaches 0, but there's a problem, the counter is always at 0 so the door destroys itself immediately, this is my code

extends Node2D

onready var enemyparent = $EnemyParent #Reference to enemy's parent
onready var door_node = $Door #Door Reference

var enemycount = self.getchild_count()

signal Enemies_Gone

func ready():
print("enemy count: " + str(enemy
if enemycount == 0:

wait I will send you a project

See here
import and run it get_child_count() works fine


This means that you have not set the node references correctly
$Door and $door are both different please see to it. Nothing will work as long as the node references are incorrect

You never mentioned all of this was in one function, and the door isn't a real object so you can hide it from there, and the enemies don't have any sort of health, they die when clicked, so our projects are vastly different

I already fixed this issue, still doesn't work, your example project is still very different to mine

It's actually one function. Its just a demo project that's why only one click to die.
What is door in your project? a node?
If stil not working then I need to look at your project.

If nothing works then create an autoloaded script with an variable named enemycount. And set its value. Whaen each enemy die then subtract 1 from enemycount

If earlier way don't work then convert
if enemy_count == 0:
if enemy_count == 1:

I’ll send you my project in the morning, for me it’s 4:43 AM and I have work in the morning at 12:00-6:00 so I’ll try to change some stuff around like you suggested and then send the project to you before I go to work if it still doesn’t work, and if you can’t find what’s wrong, I guess I’ll just have to learn to use an auto load script.


(Menu textures don't work, press enter one the menu screen then start New Game if you try to play test it, also, the easter egg in the trees is just some of my friends' discords)

Please change it's sharing settings to anyone with this link can view

I need to look at your project (zip file) not at your game

how do I do that?

Make a script named Globals. Then go to Project > Project Settings from the menu and switch to the AutoLoad tab.
enter image description here
Now add your Globals script here.

Now from any script you can access Globals's elements using Globals.###
Now in Globals script:-

var enemy_count = #total number of enemies. It is not in any function

Now add this in your enemy's death code:-

func enemy_death:
    Globals.enemy_count  -= 1

for some reason my enemies have now all turned invincible after doing this

never mind, what happened was that I tried to pass globals into a function because I forgot to make it auto load because I was gonna do an autoload with a different script, testing it after removing that, the enemies die now, I'll test to see if the door opens when they all die.

This still doesn't work

When all enemies die then (how will the door know if it should hide or not).
So attach a script to Door node and use this:-

func _process(_delta):
    if Globals.enemy_count == 0:

Thanks a ton man, I had to change hide to queue_free so the collision shape disappears too, but you were a huge help, I was worried I’d have to make some big change in the game to make it work without some way to ensure you killed all the enemies in the level which is the only way I could think of to get the player to explore with my extremely limited knowledge of game design and coding, gimme a sec to select this as the answer

