Spawning objects in loops issue

Godot Version

4.1.3stable

Question

I’m having issues trying to spawn many instances in. I’m trying to instantiate the object “free” right now, (before it was "var paczka_1= preload(“res://placeholder_obj/Free_Paczka_obj.tscn”).instantiate() ") when trying for a loop i have to move var into _ready or _process but then the var isn’t available in other functions and i’m trying to assign values to it, later trying to manipulate z_index. Any tips on how to change the code? I NEED to be able to modify the values and have it being accesed in other functions. I also need to be able to spawn certain ammount of instances too. Here’s the code(that works because my attempts at doing it myself break the code) so far:

Hi, this is an example of what you could use.

have a export and place (several)your packedscene in it in the inspector
@export var nodeIwanttoinstantiate : Array[PackedScene] =

Use onready
@onready var nodeIwanttoinstantiate: Node = preload(“”)

In this case I use an array where I place x amount of enemies in and use a timer to start the loop (timer has signal to on_timer_timeout):

@export var enemies : Array[PackedScene] =
@onready var timer: Timer = $Timer

func _ready() → void:
timer.start()

func _on_timer_timeout() → void:
for enemy in enemies:
var e = enemy.instantiate()
add_child(e)

Hey, first i’m sorry I didn’t anwser in a timely manner(i’ve been burned out recently)
secondly, I’d like to try the code you’ve provided me with but i’m really quite unsure about few parts of it all. I’d like to inform you that I’m really rather new to Godot and game making in general.

For example : “@export var nodeIwanttoinstantiate : Array[PackedScene] =
where did array come from? what’s the square at the end?

“Use onready” i’m not quite sure what @onready does in comparison to func _ready():

All in all i’d greatly appreaciate if you could explain the WHOLE thing to me like i was a 5 year-old(not really lol). I just found out about @export from some random youtube tutorial one day before you gave me your suggestion so all this still feels like balck magic to me a little bit.

Sorry for the inconvenience.

Hi and no problem, let me try to explain it, hopefully you will understand. Otherwise, just ask.

First, this is just an example and there are other ways to achieve what I think you want to do. :slight_smile:

On de annotations part (@onready) the docs are pretty good in explaining. (GDScript reference — Godot Engine (stable) documentation in English)

Ok, in my case I have multiple enemies.tscn and want to spawn them in the moment a certain time has expired. I want to control how many enemies and what type will spawn for each spawner.

So in my case I used this line to set it up:

@export var nodeIwanttoinstantiate : Array[PackedScene] = []

Here I want to have a reference in the editor to an Array which will only accept PackedScenes. So the first part is setting up the variable, then I am telling Godot what kind of variable it is (Array) then I am telling Godot You can only receive PackedScenes. For my own sanity I create it as an empty Array () instead of nothing which could be error prone.

So, after setting this up, in the inspector you should have an Array where you can drop 1,2 or 12 enemies or objects to populate this Array.

After that you could place the loop in the _ready function, but that means it will trigger the moment this scene is loaded.

So you use a for loop to itterate over your variable you created for the Array, in my case enemies. and for each object in it (enemy) you instantiate that scene and give it some other information prior spawning.

for enemy in enemies:
    var e = enemy.instantiate()
    #some more functionality#
    add_child(e)

So first you instaniate the scene and put it in variable I called e, then I used the add_child function to add it to the scene tree.

If you have questions, let me know. I encourage you to also look to the documention, its pretty detailed.

wow thanks that explains A LOT like you don’t even know how much. But, although i get most of it i still don’t know where did you define “enemy”. Did you just ommit that definition?

Also i’m having issues with “@onready var nodeIwanttoinstantiate: Node = preload(“”)” as it says that variable “nodeIwanttoinstantiate” has the same name as a previously declared variable.

Also also, any way to use func _on_reset_button_pressed(): here? it says that e was not delcared (and i know its true to that function) but its a real headscrather for me on how to use an event like that with instantieted children

PS. i realise that “enemies” is not the same as “enemy” and i see where “enemies” was defined

Hi,

the name or variable enemy, is just part of the loop. You could write anything there, but I like to keep a close reference to what it’s looping through. So, each index in the Array (enemies) I place in the temperary var (enemy) and do something with it before the next index is called upon.

you probably had another variable with the same name. the var name nodeIwanttoinstantiate, just replace it with what you want. it not important what name is it.

Now, I see you are confusing local variables with scene/ script/ global ones.
If you declare a variable on top of your script above your functions. It will live/ persist throughout your script, you can call is everywhere.

If you declare a variable in a function, you can only use it within that function.

So you declared var e in the _ready function, so var e is only accessable within that function. :slight_smile:

What are you trying to achieve when clicking that reset button?

Hey there!

I’m trying to return objects to a designated place using Marker2D. Having issues with few things connected to that functionality and also drag and drop.

I think my explaining is not up to the task through text. Would it be okay for us to talk on Discord or any simmilar app about it? Maybe if i could show you what i mean and the rest of the code that would help.

I’d like to add that its totally fine if you decline. You’ve helped me plenty, but i can’t help to realise that it would be a great learning experience for me.

Hi,

Do these object use the same Marker2d (just 1) or do they all have their own Marker2d?

I use the same handle on Discord, so you can connect to me. I am nog online a lot. usually on friday evenings. (UTC+1)