Added get_or_create_chunk to VoxelWorld.

This commit is contained in:
Relintai 2020-04-10 13:39:11 +02:00
parent f6ed4505e7
commit 03f9041eb8
2 changed files with 15 additions and 3 deletions

View File

@ -292,6 +292,16 @@ void VoxelWorld::clear() {
_generating.clear();
}
Ref<VoxelChunk> VoxelWorld::get_or_create_chunk(int x, int y, int z) {
Ref<VoxelChunk> chunk = get_chunk(x, y, z);
if (!chunk.is_valid()) {
chunk = create_chunk(x, y, z);
}
return chunk;
}
Ref<VoxelChunk> VoxelWorld::create_chunk(int x, int y, int z) {
Ref<VoxelChunk> c = call("_create_chunk", x, y, z, Ref<VoxelChunk>());
@ -367,11 +377,11 @@ void VoxelWorld::on_chunk_mesh_generation_finished(Ref<VoxelChunk> p_chunk) {
Vector<Variant> VoxelWorld::get_chunks() {
Vector<Variant> 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);

View File

@ -127,6 +127,7 @@ public:
void clear();
Ref<VoxelChunk> get_or_create_chunk(int x, int y, int z);
Ref<VoxelChunk> create_chunk(int x, int y, int z);
void generate_chunk(Ref<VoxelChunk> chunk);