add_chunk will error out now if a chunk is already owned by an another world. Also smaller improvements to set_chunks and the exit tree notification.

This commit is contained in:
Relintai 2020-04-22 15:57:00 +02:00
parent a2a9c1f714
commit af0c4d6586

View File

@ -260,6 +260,7 @@ void VoxelWorld::set_voxel_structures(const Vector<Variant> &structures) {
void VoxelWorld::add_chunk(Ref<VoxelChunk> chunk, const int x, const int y, const int z) {
ERR_FAIL_COND(!chunk.is_valid());
ERR_FAIL_COND_MSG(chunk->get_voxel_world() != NULL && chunk->get_voxel_world() != this, "Chunk is already owned by an another world!");
IntPos pos(x, y, z);
@ -484,6 +485,7 @@ Vector<Variant> VoxelWorld::get_chunks() {
void VoxelWorld::set_chunks(const Vector<Variant> &chunks) {
if (is_inside_tree()) {
for (int i = 0; i < _chunks_vector.size(); ++i) {
Ref<VoxelChunk> chunk = Ref<VoxelChunk>(_chunks_vector[i]);
@ -495,21 +497,27 @@ void VoxelWorld::set_chunks(const Vector<Variant> &chunks) {
}
}
//add the difference
for (int i = 0; i < chunks.size(); ++i) {
Ref<VoxelChunk> chunk = Ref<VoxelChunk>(chunks[i]);
if (!chunk.is_valid())
continue;
if (_chunks_vector.find(chunk) != -1) {
continue;
}
if (chunk->get_voxel_world() != NULL && chunk->get_voxel_world() != this)
if (_chunks_vector.find(chunk) != -1)
continue;
add_chunk(chunk, chunk->get_position_x(), chunk->get_position_y(), chunk->get_position_z());
}
} else {
_chunks_vector.clear();
for (int i = 0; i < chunks.size(); ++i) {
Ref<VoxelChunk> chunk = Ref<VoxelChunk>(chunks[i]);
_chunks_vector.push_back(chunk);
}
}
}
//Props
@ -876,7 +884,10 @@ void VoxelWorld::_notification(int p_what) {
Ref<VoxelChunk> chunk = _chunks_vector[i];
if (chunk.is_valid()) {
if (chunk->get_voxel_world() == this) {
chunk->exit_tree();
chunk->set_voxel_world(NULL);
}
}
}