0 votes

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 ?

in Engine by (60 points)

1 Answer

+2 votes

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?

by (10,483 points)
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 webmaster@godotengine.org with your username.