This site is currently in read-only mode during migration to a new platform.
You cannot post questions, answers or comments, as they would be lost during the migration otherwise.
0 votes

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 randomenemy = enemyarray[randi() % enemyarray.size()]
var random
enemy2 = enemyarray[randi() % enemyarray.size()]
var randomenemy3 = enemyarray[randi() % enemyarray.size()]
node.calldeferred("addchild", randomenemy)
node.calldeferred("addchild", randomenemy2)
node.calldeferred("addchild", 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!

in Engine by (105 points)

2 Answers

0 votes

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
by (6,942 points)
edited by

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.

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

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

0 votes
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.

by (346 points)


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

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.