Skip to content

Commit 71165ef

Browse files
committed
Fix loading new directory; retain not loading empty directory to save existing data
1 parent 90a5af0 commit 71165ef

File tree

8 files changed

+38
-19
lines changed

8 files changed

+38
-19
lines changed

project/demo/src/CodeGenerated.gd

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,14 @@ func create_terrain() -> Terrain3D:
2424
green_gr.set_color(1, Color.from_hsv(120./360., .4, .37))
2525
var green_ta: Terrain3DTextureAsset = await create_texture_asset("Grass", green_gr, 1024)
2626
green_ta.uv_scale = 0.1
27-
green_ta.detiling = 0.1
27+
green_ta.detiling_rotation = 0.1
2828

2929
var brown_gr := Gradient.new()
3030
brown_gr.set_color(0, Color.from_hsv(30./360., .4, .3))
3131
brown_gr.set_color(1, Color.from_hsv(30./360., .4, .4))
3232
var brown_ta: Terrain3DTextureAsset = await create_texture_asset("Dirt", brown_gr, 1024)
3333
brown_ta.uv_scale = 0.03
34-
green_ta.detiling = 0.1
34+
green_ta.detiling_rotation = 0.1
3535

3636
var grass_ma: Terrain3DMeshAsset = create_mesh_asset("Grass", Color.from_hsv(120./360., .4, .37))
3737

project/demo/src/UI.gd

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ func _process(p_delta) -> void:
2727
UI toggle: F9
2828
Render mode: F10
2929
Full screen: F11
30-
Mouse toggle: Escape
30+
Mouse toggle: Escape / F12
3131
"""
3232

3333

@@ -47,7 +47,7 @@ func _unhandled_key_input(p_event: InputEvent) -> void:
4747
KEY_F11:
4848
toggle_fullscreen()
4949
get_viewport().set_input_as_handled()
50-
KEY_ESCAPE:
50+
KEY_ESCAPE, KEY_F12:
5151
if Input.get_mouse_mode() == Input.MOUSE_MODE_VISIBLE:
5252
Input.set_mouse_mode(Input.MOUSE_MODE_CAPTURED)
5353
else:

src/terrain_3d.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -425,9 +425,12 @@ void Terrain3D::set_debug_level(const int p_level) {
425425
void Terrain3D::set_data_directory(String p_dir) {
426426
LOG(INFO, "Setting data directory to ", p_dir);
427427
if (_data_directory != p_dir) {
428-
if (_data_directory.is_empty()) {
428+
if (_data_directory.is_empty() && Util::get_files(p_dir, "terrain3d*.res").size() == 0) {
429+
// If _data_directory was empty and now specified, and has no data
430+
// assume we want to retain the current data
429431
_data_directory = p_dir;
430432
} else {
433+
// Else clear data and if not null, load
431434
_initialized = false;
432435
_destroy_labels();
433436
_destroy_collision();
@@ -897,7 +900,7 @@ void Terrain3D::_notification(const int p_what) {
897900
// Editor Node is about to save the current scene
898901
LOG(INFO, "NOTIFICATION_EDITOR_PRE_SAVE");
899902
if (_data_directory.is_empty()) {
900-
LOG(ERROR, "Data directory is empty. Set it to write data to disk.");
903+
LOG(ERROR, "Data directory is empty. Set it to save regions to disk.");
901904
} else if (!_data) {
902905
LOG(DEBUG, "Save requested, but no valid data object. Skipping");
903906
} else {

src/terrain_3d_assets.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -609,7 +609,6 @@ void Terrain3DAssets::update_mesh_list() {
609609

610610
Error Terrain3DAssets::save(const String &p_path) {
611611
if (p_path.is_empty() && get_path().is_empty()) {
612-
LOG(ERROR, "No valid path provided");
613612
return ERR_FILE_NOT_FOUND;
614613
}
615614
if (!p_path.is_empty()) {

src/terrain_3d_data.cpp

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -351,24 +351,21 @@ void Terrain3DData::save_region(const Vector2i &p_region_loc, const String &p_di
351351

352352
void Terrain3DData::load_directory(const String &p_dir) {
353353
if (p_dir.is_empty()) {
354-
LOG(ERROR, "Specified data directory is blank");
354+
LOG(ERROR, "Specified directory name is blank");
355355
return;
356356
}
357-
Ref<DirAccess> da = DirAccess::open(p_dir);
358-
if (da.is_null()) {
359-
LOG(ERROR, "Cannot read Terrain3D data directory: ", p_dir);
357+
358+
LOG(INFO, "Loading region files from ", p_dir);
359+
PackedStringArray files = Util::get_files(p_dir, "terrain3d*.res");
360+
if (files.size() == 0) {
361+
LOG(INFO, "No Terrain3D region files found in: ", p_dir);
360362
return;
361363
}
362-
_clear();
363364

364-
LOG(INFO, "Loading region files from ", p_dir);
365-
PackedStringArray files = da->get_files();
365+
_clear();
366366
for (int i = 0; i < files.size(); i++) {
367-
String fname = files[i].trim_suffix(".remap");
367+
String fname = files[i];
368368
String path = p_dir + String("/") + fname;
369-
if (!fname.begins_with("terrain3d") || !fname.ends_with(".res")) {
370-
continue;
371-
}
372369
LOG(DEBUG, "Loading region from ", path);
373370
Vector2i loc = Util::filename_to_location(fname);
374371
if (loc.x == INT32_MAX) {

src/terrain_3d_material.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -737,7 +737,6 @@ void Terrain3DMaterial::set_show_vertex_grid(const bool p_enabled) {
737737

738738
Error Terrain3DMaterial::save(const String &p_path) {
739739
if (p_path.is_empty() && get_path().is_empty()) {
740-
LOG(ERROR, "No valid path provided");
741740
return ERR_FILE_NOT_FOUND;
742741
}
743742
if (!p_path.is_empty()) {

src/terrain_3d_util.cpp

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
// Copyright © 2025 Cory Petkovsek, Roope Palmroos, and Contributors.
22

3+
#include <godot_cpp/classes/dir_access.hpp>
34
#include <godot_cpp/classes/engine.hpp>
45
#include <godot_cpp/classes/file_access.hpp>
56
#include <godot_cpp/classes/time.hpp>
@@ -108,6 +109,25 @@ String Terrain3DUtil::location_to_string(const Vector2i &p_region_loc) {
108109
return x_str + y_str;
109110
}
110111

112+
PackedStringArray Terrain3DUtil::get_files(const String &p_dir, const String &p_glob) {
113+
PackedStringArray files;
114+
Ref<DirAccess> da = DirAccess::open(p_dir);
115+
if (da.is_null()) {
116+
LOG(ERROR, "Cannot open directory: ", p_dir);
117+
return files;
118+
}
119+
PackedStringArray dir_files = da->get_files();
120+
for (int i = 0; i < dir_files.size(); i++) {
121+
String fname = dir_files[i].trim_suffix(".remap");
122+
if (!fname.matchn(p_glob)) {
123+
continue;
124+
}
125+
LOG(DEBUG, "Found file: ", p_dir + String("/") + fname);
126+
files.push_back(fname);
127+
}
128+
return files;
129+
}
130+
111131
Ref<Image> Terrain3DUtil::black_to_alpha(const Ref<Image> &p_image) {
112132
if (p_image.is_null()) {
113133
return Ref<Image>();

src/terrain_3d_util.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ class Terrain3DUtil : public Object {
3333
static Vector2i string_to_location(const String &p_string);
3434
static String location_to_filename(const Vector2i &p_region_loc);
3535
static String location_to_string(const Vector2i &p_region_loc);
36+
static PackedStringArray get_files(const String &p_dir, const String &p_glob = "*");
3637

3738
// Image operations
3839
static Ref<Image> black_to_alpha(const Ref<Image> &p_image);

0 commit comments

Comments
 (0)