Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make sure MeshLibrary shape array has correct number of elements #56891

Merged
merged 1 commit into from
Jan 25, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 29 additions & 4 deletions scene/resources/mesh_library.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@

#include "mesh_library.h"

#include "box_shape_3d.h"
rafallus marked this conversation as resolved.
Show resolved Hide resolved

bool MeshLibrary::_set(const StringName &p_name, const Variant &p_value) {
String name = p_name;
if (name.begins_with("item/")) {
Expand Down Expand Up @@ -255,12 +257,35 @@ int MeshLibrary::get_last_unused_item_id() const {
}

void MeshLibrary::_set_item_shapes(int p_item, const Array &p_shapes) {
ERR_FAIL_COND(p_shapes.size() & 1);
Array arr_shapes = p_shapes;
int size = p_shapes.size();
if (size & 1) {
ERR_FAIL_COND_MSG(!item_map.has(p_item), "Requested for nonexistent MeshLibrary item '" + itos(p_item) + "'.");
int prev_size = item_map[p_item].shapes.size() * 2;

if (prev_size < size) {
// Check if last element is a shape.
Ref<Shape3D> shape = arr_shapes[size - 1];
if (shape.is_null()) {
Ref<BoxShape3D> box_shape;
box_shape.instantiate();
arr_shapes[size - 1] = box_shape;
Comment on lines +270 to +272
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure about this, that seems pretty arbitrary, and could lead to a lot of instantiations that will then need to be freed and replaced by what the user wants to use.

Wouldn't it be sufficient to just make the step 2 for the SpinBox?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wouldn't it be sufficient to just make the step 2 for the SpinBox?

Well not sure that's actually possible currently in the array editor.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The array is expecting to have a shape. If there is anything else, even an empty Variant() the array won't update and the size won't change. A few lines below that piece of code you can find

if (sd.shape.is_valid()) {
	shapes.push_back(sd);
}

So, if it isn't a valid shape, nothing happens. Could you think of other way of doing it? It is absolutely arbitrary but is the way I found to do it.

}

// Make sure the added element is a Transform3D.
arr_shapes.push_back(Transform3D());
size++;
} else {
size--;
arr_shapes.resize(size);
}
}

Vector<ShapeData> shapes;
for (int i = 0; i < p_shapes.size(); i += 2) {
for (int i = 0; i < size; i += 2) {
ShapeData sd;
sd.shape = p_shapes[i + 0];
sd.local_transform = p_shapes[i + 1];
sd.shape = arr_shapes[i + 0];
sd.local_transform = arr_shapes[i + 1];

if (sd.shape.is_valid()) {
shapes.push_back(sd);
Expand Down