Skip to content

Dictionaries return copies of stored TypedArrays #1508

@TokisanGames

Description

@TokisanGames

Godot version

4.2.2-stable

godot-cpp version

godot-4.2.2-stable

System information

Windows 11/64, RTX 3070, Vulkan

Issue description

I am receiving copies of TypedArrays in gdextension when storing arrays indexed via Vector2i.

TypedArray: copy

Dictionary test_dict;
TypedArray<Transform3D> ta;
test_dict[Vector2i(0, 0)] = ta;
ta.push_back(Transform3D(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1));
UtilityFunctions::print("Array A ptr: ", uint64_t(&ta), " size: ", ta.size());

TypedArray<Transform3D> tb = test_dict[Vector2i(0, 0)];
tb.push_back(Transform3D(0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1));
	
UtilityFunctions::print("Array B ptr: ", uint64_t(&tb), " size: ", tb.size());
UtilityFunctions::print("Array A ptr: ", uint64_t(&ta), " size: ", ta.size());
Array A ptr: 81925230416 size: 1
Array B ptr: 81925230424 size: 2    // note different memory address
Array A ptr: 81925230416 size: 1   // should be size 2

Array: reference

Dictionary test_dict2;
Array aa;
test_dict2[Vector2i(0, 0)] = aa;
aa.push_back(Transform3D(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1));
UtilityFunctions::print("Array A ptr: ", uint64_t(&aa), " size: ", aa.size());

Array ab = test_dict2[Vector2i(0, 0)];
ab.push_back(Transform3D(0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1));

UtilityFunctions::print("Array B ptr: ", uint64_t(&ab), " size: ", ab.size());
UtilityFunctions::print("Array A ptr: ", uint64_t(&aa), " size: ", aa.size());
Array A ptr: 87499460384 size: 1
Array B ptr: 87499460400 size: 2
Array A ptr: 87499460384 size: 2

Image: reference

Dictionary test_image;
Ref<Image> img = Image::create(1, 1, true, Image::FORMAT_RGB8);
img->set_pixel(0, 0, Color(1, 1, 1));
UtilityFunctions::print("Image A ptr: ", uint64_t(img.ptr()), " (0,0): ", img->get_pixel(0, 0));
test_image[Vector2i(0, 0)] = img;

Ref<Image> b = test_image[Vector2i(0, 0)];
b->set_pixel(0, 0, Color(.5, .5, .5));
UtilityFunctions::print("Image B ptr: ", uint64_t(b.ptr()), " (0,0): ", b->get_pixel(0, 0));
UtilityFunctions::print("Image A ptr: ", uint64_t(img.ptr()), " (0,0): ", img->get_pixel(0, 0));
Image A ptr: 3032189040112 (0,0): (1, 1, 1, 1)
Image B ptr: 3032189040112 (0,0): (0.498, 0.498, 0.498, 1)  // same memory pointer
Image A ptr: 3032189040112 (0,0): (0.498, 0.498, 0.498, 1)  // correct value

I replaced the Godot dictionary with std::map and it works properly with Godot TypedArrays, so I am using that for now. Using a local map while building, then converting to a Godot dictionary for storage within a Godot resource.

Metadata

Metadata

Assignees

No one assigned

    Labels

    archivedbugThis has been identified as a bug

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions