From 03f9041eb82d26230b5f8d0825eaf86a7188121c Mon Sep 17 00:00:00 2001 From: Relintai Date: Fri, 10 Apr 2020 13:39:11 +0200 Subject: [PATCH] Added get_or_create_chunk to VoxelWorld. --- world/voxel_world.cpp | 17 ++++++++++++++--- world/voxel_world.h | 1 + 2 files changed, 15 insertions(+), 3 deletions(-) diff --git a/world/voxel_world.cpp b/world/voxel_world.cpp index 3a75c26..c254342 100644 --- a/world/voxel_world.cpp +++ b/world/voxel_world.cpp @@ -292,6 +292,16 @@ void VoxelWorld::clear() { _generating.clear(); } +Ref VoxelWorld::get_or_create_chunk(int x, int y, int z) { + Ref chunk = get_chunk(x, y, z); + + if (!chunk.is_valid()) { + chunk = create_chunk(x, y, z); + } + + return chunk; +} + Ref VoxelWorld::create_chunk(int x, int y, int z) { Ref c = call("_create_chunk", x, y, z, Ref()); @@ -367,11 +377,11 @@ void VoxelWorld::on_chunk_mesh_generation_finished(Ref p_chunk) { Vector VoxelWorld::get_chunks() { Vector r; for (int i = 0; i < _chunks_vector.size(); i++) { - #if VERSION_MAJOR < 4 +#if VERSION_MAJOR < 4 r.push_back(_chunks_vector[i].get_ref_ptr()); - #else +#else r.push_back(_chunks_vector[i]); - #endif +#endif } return r; } @@ -666,6 +676,7 @@ void VoxelWorld::_bind_methods() { BIND_VMETHOD(MethodInfo("_prepare_chunk_for_generation", PropertyInfo(Variant::OBJECT, "chunk", PROPERTY_HINT_RESOURCE_TYPE, "VoxelChunk"))); BIND_VMETHOD(MethodInfo("_generate_chunk", PropertyInfo(Variant::OBJECT, "chunk", PROPERTY_HINT_RESOURCE_TYPE, "VoxelChunk"))); + ClassDB::bind_method(D_METHOD("get_or_create_chunk", "x", "y", "z"), &VoxelWorld::get_or_create_chunk); ClassDB::bind_method(D_METHOD("create_chunk", "x", "y", "z"), &VoxelWorld::create_chunk); ClassDB::bind_method(D_METHOD("_create_chunk", "x", "y", "z", "chunk"), &VoxelWorld::_create_chunk); diff --git a/world/voxel_world.h b/world/voxel_world.h index 35812ef..63675d7 100644 --- a/world/voxel_world.h +++ b/world/voxel_world.h @@ -127,6 +127,7 @@ public: void clear(); + Ref get_or_create_chunk(int x, int y, int z); Ref create_chunk(int x, int y, int z); void generate_chunk(Ref chunk);