0 votes

Hi folks,

I'm using a set of 8 spawn points for targets to appear at, with the spawn location randomized from a group 'activespawn' (spawn points with no target) and a randrange which uses the size of the 'active_spawn' group as it's higher limit.

var possible_spawns = get_tree().get_nodes_in_group("active_spawn")
var spawn_range = get_tree().get_nodes_in_group("active_spawn").size()
var random_spawn = possible_spawns[rand_range(0, spawn_range)]

My issue is that once all 8 spawn points are occupied and the group 'active_spawn' is empty, any further atempts to spawn a target result in the error 'Invalid get index '0' (on base: 'Array')' which crashes the game.

Any attempts of mine to add exception handling have had no effect. I'm rather stuck, so would appreciate any insight you're able to provide! If I haven't provided enough information/code, please let me know.

Cheers,
Matt

Godot version 3.2.3
in Engine by (15 points)

1 Answer

+2 votes
Best answer

GDScript does not have exception handling. Just put an if statement that only spawns an enemy if there is at least one node in the group "active_spawn".

by (7,190 points)
selected by

This worked perfectly, thank you! I got so focused on sorting it within the array itself that I was a bit blinded to other ways of solving this.

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