Skip to content

Commit

Permalink
SimpleSpawner: Rename frequency to period
Browse files Browse the repository at this point in the history
This property is the time, in seconds, to wait between spawning scenes. This is
a period/interval/wavelength, not a frequency, which would be measured in Hz,
or 1/seconds.

Add a new property and corresponding blocks with the correct definition.

Adjust the existing property to delegate to the new property. Use
@export_storage to allow its value to be set from existing serialized scenes
without it being visible in the inspector. This annotation is new in Godot 4.3
so bump our minimum supported version.

When a scene that overrides the old property is loaded in the editor, the value
will be propagated to the new property, which is then saved into the scene.
Adjusting the new property also adjusts the old property's value. The old
property will hang around, harmlessly mirroring the new property, in the .tscn
file until it is removed in a text editor.

The instances of “frequency” that I have not changed are in the example scene's
on-screen documentation. In this case it was actually used correctly: the
keyboard action to increase the frequency reduces the property which is now
called period, and the decrease action respectively increases the period.
  • Loading branch information
wjt committed Oct 22, 2024
1 parent 57b06bc commit 17d897e
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 10 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/checks.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ jobs:
name: Tests
strategy:
matrix:
godot-version: [4.2.2, 4.3.0]
godot-version: [4.3.0]
runs-on: ubuntu-latest
steps:
- name: Checkout
Expand Down
47 changes: 39 additions & 8 deletions addons/block_code/simple_spawner/simple_spawner.gd
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ enum LimitBehavior { REPLACE, NO_SPAWN }

## The period of time in seconds to spawn another component. If zero, they won't spawn
## automatically. Use the "Spawn" block.
@export_range(0.0, 10.0, 0.1, "or_greater", "suffix:s") var spawn_frequency: float = 0.0:
set = _set_spawn_fraquency
@export_range(0.0, 10.0, 0.1, "or_greater", "suffix:s") var spawn_period: float = 0.0:
set = _set_spawn_period

## How many spawned scenes are allowed. If zero, there is no limit.
@export_range(0, 50, 0.1, "or_greater", "suffix:scenes") var spawn_limit: int = 50
Expand All @@ -34,6 +34,15 @@ enum LimitBehavior { REPLACE, NO_SPAWN }
## - No Spawn: No spawn happens until any spawned scene is removed by other means.
@export var limit_behavior: LimitBehavior

## Old name for [member spawn_period].
##
## @deprecated: Use [member spawn_period] instead
@export_storage var spawn_frequency: float:
get:
return spawn_period
set(value):
spawn_period = value

var _timer: Timer
var _spawned_scenes: Array[Node]

Expand All @@ -53,20 +62,20 @@ func _remove_oldest_spawned():
spawned.get_parent().remove_child(spawned)


func _set_spawn_fraquency(new_frequency: float):
spawn_frequency = new_frequency
func _set_spawn_period(new_period: float):
spawn_period = new_period
if not _timer or not is_instance_valid(_timer):
return
_timer.wait_time = spawn_frequency
_timer.wait_time = spawn_period


func spawn_start():
if spawn_frequency == 0.0:
if spawn_period == 0.0:
return
if not _timer or not is_instance_valid(_timer):
_timer = Timer.new()
add_child(_timer)
_timer.wait_time = spawn_frequency
_timer.wait_time = spawn_period
_timer.timeout.connect(spawn_once)
_timer.start()
spawn_once.call_deferred()
Expand Down Expand Up @@ -107,8 +116,9 @@ func spawn_once():
spawned.position = global_position


## @deprecated: Set the [member spawn_period] property instead
func do_set_spawn_frequency(new_frequency: float):
_set_spawn_fraquency(new_frequency)
spawn_period = new_frequency


static func setup_custom_blocks():
Expand Down Expand Up @@ -159,6 +169,7 @@ static func setup_custom_blocks():
block_definition.type = Types.BlockType.STATEMENT
block_definition.display_template = "set spawn frequency to {new_frequency: FLOAT}"
block_definition.code_template = "do_set_spawn_frequency({new_frequency})"
block_definition.hidden = true
block_list.append(block_definition)

block_definition = BlockDefinition.new()
Expand All @@ -169,6 +180,26 @@ static func setup_custom_blocks():
block_definition.variant_type = TYPE_FLOAT
block_definition.display_template = "spawn frequency"
block_definition.code_template = "spawn_frequency"
block_definition.hidden = true
block_list.append(block_definition)

block_definition = BlockDefinition.new()
block_definition.name = &"simplespawner_set_spawn_period"
block_definition.target_node_class = _class_name
block_definition.category = "Lifecycle | Spawn"
block_definition.type = Types.BlockType.STATEMENT
block_definition.display_template = "set spawn period to {new_period: FLOAT}"
block_definition.code_template = "spawn_period = {new_period}"
block_list.append(block_definition)

block_definition = BlockDefinition.new()
block_definition.name = &"simplespawner_get_spawn_period"
block_definition.target_node_class = _class_name
block_definition.category = "Lifecycle | Spawn"
block_definition.type = Types.BlockType.VALUE
block_definition.variant_type = TYPE_FLOAT
block_definition.display_template = "spawn period"
block_definition.code_template = "spawn_period"
block_list.append(block_definition)

BlocksCatalog.add_custom_blocks(_class_name, block_list, [], {})
2 changes: 1 addition & 1 deletion asset-template.json.hb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"title": "Block Coding",
"description": "Create games using a high-level, block-based visual programming language.\r\n\r\nIntended as an educational tool for learners in the earlier stages of their journey towards becoming game developers. This plugin lets you create your first games with high-level blocks, avoiding the immediate need to learn to code in GDScript. Building games in this way provides a gentle introduction to programming concepts and allows you to focus your efforts on becoming familiar with the rest of the Godot Editor UI.",
"category_id": "5",
"godot_version": "4.2",
"godot_version": "4.3",
"version_string": "{{ context.release.name }}",
"cost": "MIT",
"support_level": "community",
Expand Down

0 comments on commit 17d897e

Please sign in to comment.