From 97949d6b4482ad2fdeb38fa32eafd58213fc8eec Mon Sep 17 00:00:00 2001 From: coder-bot Date: Tue, 18 Aug 2026 00:08:24 +0000 Subject: [PATCH] WIP checkpoint: Implement chunk streaming system in GDScript --- scenes/main.tscn | 6 ++++++ scripts/chunk_stream_manager.gd | 32 +++++++++++++++++++++++--------- 2 files changed, 29 insertions(+), 9 deletions(-) create mode 100644 scenes/main.tscn diff --git a/scenes/main.tscn b/scenes/main.tscn new file mode 100644 index 0000000..a1be944 --- /dev/null +++ b/scenes/main.tscn @@ -0,0 +1,6 @@ +[gd_scene load_steps=2 format=3 uid="uid://main_scene"] + +[ext_resource type="Script" path="res://scripts/chunk_stream_manager.gd" id="1_chunk_stream"] + +[node name="Main" type="Node3D"] +script = ExtResource("1_chunk_stream") diff --git a/scripts/chunk_stream_manager.gd b/scripts/chunk_stream_manager.gd index 27d04bc..f19f9b2 100644 --- a/scripts/chunk_stream_manager.gd +++ b/scripts/chunk_stream_manager.gd @@ -77,18 +77,32 @@ var _server_rpc: Object = null ## Internal: track which chunks are currently being loaded (to avoid duplicates). var _loading_in_progress: Dictionary = {} +## Internal: time accumulator for throttled streaming updates. +var _update_accumulator: float = 0.0 + +## Internal: interval between streaming updates (seconds). +const UPDATE_INTERVAL: float = 0.1 + func _ready() -> void: - _process_thread = Thread.create(_streaming_loop, Thread.PRIORITY_LOW) + # Streaming is driven from _process (main thread) so we can safely + # manipulate the scene tree. No background thread is used. + set_process(true) -func _streaming_loop() -> void: - while is_inside_tree(): - _update_streaming() - _process_load_queue() - _process_unload_queue() - streaming_updated.emit() - await get_tree().create_timer(0.1).timeout # Check every 100ms +func _process(delta: float) -> void: + if not is_active or player == null: + return + + _update_accumulator += delta + if _update_accumulator < UPDATE_INTERVAL: + return + _update_accumulator = 0.0 + + _update_streaming() + _process_load_queue() + _process_unload_queue() + streaming_updated.emit() func _update_streaming() -> void: @@ -132,7 +146,7 @@ func _compute_target_chunks() -> Dictionary: var half := streaming_radius for dx in range(-half, half + 1): for dy in range(-half, half + 1): - for dz in range(-Chunk.CHUNK_SIZE_Z, Chunk.CHUNK_SIZE_Z + 1): + for dz in range(-half, half + 1): var coord := Vector3i( _player_chunk_coord.x + dx, _player_chunk_coord.y + dy,