Spawning enemies using dictionary

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

If i have dictionary called

const FLOOR = -1
var spawn_points = { Vector2(59,7) : -1,  Vector2(5,8) : -1, Vector2(9,6) : -1, Vector2(4,24) : -1, Vector2(44,36) : -1}

How do i spawn enemies in a Vector2(x, y) by picking a random item from the list and then append that item from the list ?

:bust_in_silhouette: Reply From: njamster

First of all, a dictionary is not the same as a list.

You can pick a random key by using this function:

func get_random_key():
    var random_id = randi() % spawn_points.size()
    var random_key = spawn_points.keys()[random_id]
    return random_key

Don’t forget to call randomize() in _ready once!

You can then get the value of said key using this:

var random_key = get_random_key()
var value = spawn_points[random_key]

However, I’m not sure what you mean by “append that item from the list”. The value? The key? Both? And where do you want to append it? To spawn_points? Why?