If I wanted to make an enemy counter that opened a door when it reached zero, how would I?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By IVTheSimple

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.

You should research AutoLoad and Singletons

Snail0259 | 2021-07-27 05:46

:bust_in_silhouette: Reply From: Help me please

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:
	door_node.hide()

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

this is my code but it says "Invalid Call Nonexistent Function ‘get_child_count’ 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 enemy_parent = $Enemy_Parent #Reference to enemy’s parent
onready var door_node = $Door #Door Reference

var enemy_count = enemy_parent.get_child_count()

func _ready():
print("enemy count: " + str(enemy_count))
func count_em():
if enemy_count == 0:
door_node.hide()

IVTheSimple | 2021-07-28 07:08

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 enemy_parent = $Enemy_Parent #Reference to enemy’s parent
onready var door_node = $Door #Door Reference

var enemy_count = self.get_child_count()

signal Enemies_Gone

func _ready():
print("enemy count: " + str(enemy_count))
if enemy_count == 0:
emit_signal(“Enemies_Gone”)

IVTheSimple | 2021-07-28 07:20

wait I will send you a project

Help me please | 2021-07-28 07:25

See here
https://drive.google.com/file/d/1dWCcqtzQTZifNv7ordOUzSUvRKqAEhEP/view?usp=sharing
import and run it get_child_count() works fine

Help me please | 2021-07-28 07:44

this is my code but it says "Invalid Call Nonexistent Function ‘getchildcount’ in base invalid

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

Help me please | 2021-07-28 07:55

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

IVTheSimple | 2021-07-28 08:07

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

IVTheSimple | 2021-07-28 08:13

Sorry!
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.

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

Help me please | 2021-07-28 08:35

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

Help me please | 2021-07-28 08:39

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.

IVTheSimple | 2021-07-28 08:44

ConnectionLost.exe - Google Drive

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

IVTheSimple | 2021-07-28 11:23

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

Help me please | 2021-07-29 02:51

ConnectionLost.exe - Google Drive

ok

IVTheSimple | 2021-07-29 03:20

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

Help me please | 2021-07-29 06:00

.zip - Google Drive

IVTheSimple | 2021-07-29 06:27

Unfortunately you have to work with autoloads

Reference
Singletons (Autoload) — Godot Engine (stable) documentation in English

Help me please | 2021-07-29 06:47

how do I do that?

IVTheSimple | 2021-07-29 23:56

Make a script named Globals. Then go to Project > Project Settings from the menu and switch to the AutoLoad tab.

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

Help me please | 2021-07-30 03:13

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

IVTheSimple | 2021-07-31 02:42

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.

IVTheSimple | 2021-07-31 02:45

This still doesn’t work

IVTheSimple | 2021-07-31 03:12

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

Help me please | 2021-07-31 04:49

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

IVTheSimple | 2021-07-31 05:00