How to assign a number to a node, and instance that node when the number is mentioned?

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

I’m not sure how to assign a number to an enemy, and refer to it using the numbers generated by an array. For instance, if the array generates a 1, 2, and 3, I want to spawn one enemy with the assigned number 1, another with the number 2, and another with 3.
Here’s the code for choosing a random enemy (I know it’s bad, I’m still new to RNG):

var Spitter = preload(“res://Spitter.tscn”).instance()
var Zombie = preload(“res://Zombie.tscn”).instance()
var Hunter = preload(“res://Hunter.tscn”).instance()
var enemyarray = [Zombie, Hunter, Spitter]
var random_enemy = enemyarray[randi() % enemyarray.size()]
var random_enemy2 = enemyarray[randi() % enemyarray.size()]
var random_enemy3 = enemyarray[randi() % enemyarray.size()]
parent_node.call_deferred(“add_child”, random_enemy)
parent_node.call_deferred(“add_child”, random_enemy2)
parent_node.call_deferred(“add_child”, random_enemy3)

I also have an issue with spawning multiple enemies of the same kind; it says they’re already children of node “Enemies” (the node that I spawn them under). This only means one enemy of each kind gets spawned. Help would be very appreciated!

:bust_in_silhouette: Reply From: Wakatta

Use a Dictionary instead of an array and only instance your nodes when you’re ready to use them. A work around for your initial question is to name your nodes with the numbers to be referenced.

var enimies = {
    "Spitter" : preload("res://Spitter.tscn")
    "Zombie" : preload("res://Zombie.tscn")
    "Hunter" : preload("res://Hunter.tscn")

func random_spawn():
    var enemy = enimies.values()[randi() % enemies.size()]
    enemy.instance() = + str(parent_node.get_children().size())
    parent_node.call_deferred("add_child", enemy)
return enemy

Decent code, unfortunately not very applicable to what I’m trying to do. I can’t spawn multiple enemies with this, and the number in the name is always defaulted to “1”. Also, in order for the naming system to work, the enemies must be instanced in the dictionary itself. This also doesn’t tell me how to reference the number given to an enemy, and spawn the enemy using that number.

OiKeTTLe | 2021-06-01 07:59

Oh but you can Indeed spawn multiple enemies by calling the function multiple times

For example

func _ready():
    for itr in range(10):

This line = + str(parent_node.get_children().size()) gets the Enemies node children size and assigns that number to the instanced node name.

So you’ll have something like Zombie4, Spitter1, Hunter2
And in that case you can use something likeparent_node.get_node("Zombie4")

Wakatta | 2021-06-04 23:54

If that’s still not what you want.
You can add your instances to an array array.append(enemy1)
and use array[number] to get that reference
Number = any number lower than array.size()

Wakatta | 2021-06-05 00:01

:bust_in_silhouette: Reply From: jandrewlong
var Spitter = preload("res://Spitter.tscn")
var Zombie = preload("res://Zombie.tscn")
var Hunter = preload("res://Hunter.tscn")
var enemy_array = [Zombie, Hunter, Spitter]
var random_enemy = enemy_array[randi() % enemy_array.size()].instance()
var random_enemy2 = enemy_array[randi() % enemy_array.size()].instance()
var random_enemy3 = enemy_array[randi() % enemy_array.size()].instance()
parent_node.call_deferred("add_child", random_enemy)
parent_node.call_deferred("add_child", random_enemy2)
parent_node.call_deferred("add_child", random_enemy3)

Put the preloads in the array, not instances. Your method was trying to add the same instance if the random number selected the same type of enemy. This way only instances when the random number generator selects the type, so the same enemy can be used multiple times.


Now I’m confused as to what he/she is trying to achieve as their terminology is all mixed up

Wakatta | 2021-06-04 20:14