Enemy Spawning and using their variables in main script

Godot Version 4.3

I’m making a game where I want enemies to infinitely spawn/despawn in/out from both sides, and I’m using the $Enemy.var in the main script to control the variables of the Enemy script to do things with it, but how do I spawn in enemies and use the variables referring to each one individually, and also, how do I spawn them in a random spot in a given area (for example, spawn anywhere within this square)?

If you make the enemies as scenes, you can spawn them as instances and they will react individually to their own variables.

Use the random function for the x and the y of the spawn position.

1 Like

Hold on, I don’t think I worded it good enough, I do have the enemy scene, I’m using the variables in the main script to interact with the variables of other things so that’s why I need them in the main script, I don’t know if a better way to have things like “if $Player.punching == true and $Enemy.isinpuncharea == true:
Enemy.punched()”

If you are handling a lot of enemies, then adding them to an array makes it easier. Then you can do a for loop:

for n in enemies:  
     if n.punching:
           do_something()

or you can put them all under a node and then get the children which returns an array

I have one enemy type, but I was just want them to spawn in infinitely, but I have no idea how to have the $Enemy.var work for each one individually if that makes sense. Should I just have 20 or so copies of enemies and have those spawn in and connect their variables?

When you create instances, the editor will name them with a number. Write a small program that spawns 5 enemies and look at the scene editor on the left of the screen while the game is running. Make it in a window so you can see the editor. That would be how you would call them with that number. It will be something like enemy[01] or something. I can’t remember just now. That’s how you have to call them if you don’t put them in an array.

1 Like

I made a const enemy = preload(“res://enemy.tscn”) type thing, and replaced the $Enemy.var with enemy.var but it pops up with an error when I try to run it saying Invalid access to property or key ‘var’ on a base object of type ‘PackedScene’.

What you have is a preloaded scene object in your script, this basically acts like a class or a blueprint for an object. You need to instantiate it to turn it into an actual object.
var enemyInst = enemy.instantiate()

Also don’t forget to add the new instance to the scene tree.

1 Like

I do have the var enemyinst = enemy.instantiate() thing, but it still won’t work with the variables

Post the code snippet, it’s kind of hard to help you without it.

1 Like

const food = preload(“res://cheese_burger.tscn”)

func _process(delta: float) → void:
spawn_food(delta)
if food.notbeingpunched == false:

func spawn_food(delta):
var Burger = food.instantiate()
self.add_child(Burger)

“food” refers to enemy.

I FIGURED IT OUT, I moved the func to the process delta, and replaces “food” with Burger

1 Like

Although, when I have more than one spawned in, the variables won’t work because I don’t think it knows which one to work with. Nothing breaks, just nothing happens. It works perfectly fine with just one spawned in though. How would I get it to work with a possible infinite amount of things spawned in?

Awesome! Just some notes that would make your code cleaner:

1 Like

Also, when I use $“.”.queue_free() to delete the enemy, it stops and puts this error relating to the var in script:

Invalid access to property or key ‘var’ on a base object of type ‘previously freed’.

Try set_deffered() for that

1 Like

Ok thank you, I will try this later

1 Like

Good luck! Read up the documentation, saved me a lot of headaches.

1 Like

I tried set_deferred() but this is what happened
(Here is how I used it)
if Burger.eaten == true:
Burger.set_deffered()

here is the error:

Invalid call. Nonexistent function ‘set_defferred’ in base ‘CharacterBody2D (cheese_burger.gd)’.

Can you please give an example of how to use it, and should it be in the main script or the enemy script?

Look at godot’s documentation and you will see how to use it. Learn to google before asking in forums, you will get nowhere otherwise.

I really don’t understand this set deferred stuff, do you recommend a tutorial on how to make enemies spawn/despawn and have variables in their code that connect to the main code?