Skip to content

Commit e084f5a

Browse files
committed
Add deliberately-broken enemy spawner
This adds a SpawnerBroken script, and an enemy_spawner_broken scene using that script configured to spawn enemy.tcsn. The intention is that it spawns the given scene periodically, but something is wrong: it spawns the scene way too frequently, which makes the game unplayably slow. The bug – signposted with a FIXME comment – is intentional and contrived. It's meant to be a simplified version of a very common class of bug: if you specify a time internal as a `float`, the type alone doesn't tell you whether the units are seconds, milliseconds, etc., and if you get it wrong you will only find out through testing. The idea is that a learner can fix the issue in at least 4 ways: 1. Increase the spawn_interval property on the instance of the scene in main.tscn. 2. Increase the spawn_interval property in enemy_spawner_broken.tscn. 3. Increase the spawn_interval property in spawner_broken.gd. 4. (The correct fix) Address the FIXME in the code by removing the erroneous conversion to milliseconds.
1 parent 0e24cbc commit e084f5a

3 files changed

Lines changed: 40 additions & 0 deletions

File tree

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
[gd_scene load_steps=3 format=3 uid="uid://bkkta0sx62l3t"]
2+
3+
[ext_resource type="Script" uid="uid://5k8i85vjjqgp" path="res://components/spawner/spawner_broken.gd" id="1_h3ru7"]
4+
[ext_resource type="PackedScene" uid="uid://dk0xon0k7ga23" path="res://components/enemy/enemy.tscn" id="2_7irji"]
5+
6+
[node name="EnemySpawnerBroken" type="Node2D"]
7+
script = ExtResource("1_h3ru7")
8+
scene_to_spawn = ExtResource("2_7irji")
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
class_name SpawnerBroken
2+
extends Node2D
3+
## @experimental
4+
## Periodically spawns a scene at this node's position.
5+
##
6+
## This could be used, for example, to have a new enemy appear every few seconds.
7+
## This class is marked experimental because it seems to be buggy.
8+
9+
## Number of seconds to wait between spawning [member scene_to_spawn].
10+
@export_range(1.0, 600.0, 1.0, "suffix:s", "or_greater") var spawn_interval := 5.0
11+
12+
## Scene to spawn every [member spawn_interval] seconds.
13+
@export var scene_to_spawn: PackedScene
14+
15+
16+
func _ready() -> void:
17+
var timer := Timer.new()
18+
add_child(timer)
19+
20+
timer.timeout.connect(spawn)
21+
22+
# Convert seconds to milliseconds for timer.start() method.
23+
# FIXME: Is this correct?? It seems to spawn too fast!
24+
timer.start(spawn_interval / 1000)
25+
26+
27+
## Spawns an instance of [member scene_to_spawn] at the same position as this node, as a sibling.
28+
func spawn() -> void:
29+
var scene := scene_to_spawn.instantiate()
30+
scene.global_position = global_position
31+
get_parent().add_child(scene)
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
uid://5k8i85vjjqgp

0 commit comments

Comments
 (0)