Skip to content

Last component generated by a loop is changing the value of all objects in the array #5773

@FelipeAndrioli

Description

@FelipeAndrioli

I'm trying to create a UI component for every object that I have on an array so I could change the values of them, and I'm doing this by a loop:

    for (int i = 0; i < objs_array->size(); i++) {

        obj *current_obj = &objs_array->at(i);
        ImGui::PushID(current_obj->id.c_str()); 
        ImGui::Begin(current_obj->id.c_str());

        std::string label_x = "Translation x " + current_obj->id;
        std::string label_y = "Translation y " + current_obj->id;
        std::string label_z = "Translation z " + current_obj->id;
       
        ImGui::SliderFloat(label_x.c_str(), &current_obj->translation.x,-50.0f, 50.0f);
        ImGui::SliderFloat(label_y.c_str(), &current_obj->translation.y,-50.0f, 50.0f);
        ImGui::SliderFloat(label_y.c_str(), &current_obj->translation.z,-50.0f, 50.0f);

        ImGui::End();
        ImGui::PopID();
    }

Just a small example but the structure is pretty much the same. With only one component everything works fine, however with multiple objects the last component created is changing the values of the other objects in the array and the remaining created components get stuck on negative zeroes or with huge random numbers.

Am I missinterpreting something or is there some other way to do it? I've tried many solutions to this but any of them seems to work.

Any help or pointer to my stupidity would be appreciated.

Thanks.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions