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

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

Closed
FelipeAndrioli opened this issue Oct 13, 2022 · 11 comments
Labels
label/id and id stack implicit identifiers, pushid(), id stack

Comments

@FelipeAndrioli
Copy link

FelipeAndrioli commented Oct 13, 2022

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.

@PathogenDavid
Copy link
Contributor

It looks like you're on the right track, but the way you're handling the ID disambiguation isn't quite right.

The problem is your use of PushID/PopID is not actually helping when the obj::id is the same between objects. If you have two objects named the same, the full ID will still be the same. (To put it another way: Your use of PushID isn't actually doing much of anything here.)

The easiest solution here would be to use the ImGui::PushID(void*) with your object pointers. IE: ImGui::PushID(current_obj);

If you haven't read the FAQ entry on the ID stack you definitely should. The ID stack tool mentioned there is helpful for understanding what's happening in these situations.

@FelipeAndrioli
Copy link
Author

FelipeAndrioli commented Oct 13, 2022

Thanks for the suggestion, I'm just starting with Dear ImGui and I didn't knew how useful the FAQ was, I'm definitely going to digg deeper into that.

Now regarding use the actual object on PushID, the result was that now the first component isn't with huge random number anymore, it's fixed with negative zeroes, and I'm filling the id's of each object manually so I thought that it would be enough.

image

After doing this change the behaviour changed a little, the last component still changing all the object's values but when I tried to use the first one the application crashed what wasn't happening before.

Note: I already fixed the same id's (Translation y)

@FelipeAndrioli
Copy link
Author

I've just realize I said a wrong statement when I said "last component created is changing the values of the remaining components", the correct is "the last component created is changing the values of all the objects in the array, the other components get stuck at negative zeroes or with huge random values"

@FelipeAndrioli FelipeAndrioli changed the title UI component generated by loop get stuck on the last changing the values of all the others Last component generated by a loop is changing the value of all objects in the array Oct 14, 2022
@ocornut
Copy link
Owner

ocornut commented Oct 14, 2022

The PushID/PopID is absolutely unnecessary here as you are Begin-ing into a new window anyway.
The posted code should work, you have a problem elsewhere in your code which we can't guess without more details.

@ocornut
Copy link
Owner

ocornut commented Oct 14, 2022

err, notice that in your screenshot it does show what looks like an ID Conflict
image
And it's not matching what the posted code says.

@ocornut
Copy link
Owner

ocornut commented Oct 14, 2022

As the mistake is here:

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);

ID conflict. Closing.

@ocornut ocornut closed this as completed Oct 14, 2022
@ocornut ocornut added the label/id and id stack implicit identifiers, pushid(), id stack label Oct 14, 2022
@FelipeAndrioli
Copy link
Author

This is already fixed as I've mentioned on the original post, but you're right it's some other problem on my code. Thanks anyway.

@ocornut
Copy link
Owner

ocornut commented Oct 14, 2022

Well

@ocornut ocornut reopened this Oct 14, 2022
@ocornut
Copy link
Owner

ocornut commented Oct 14, 2022

Well we cannot really help without additional info. If you find/understand your issue good to post details here.

@FelipeAndrioli
Copy link
Author

Sure, I'm still investigating it and once I find out I'll post here but it's definitely not related to Imgui. I put it to print the positions and the problem was there, now this is fixed but both objects are still moving together even thought the translations values are different.

Anyway I think it might keep as closed, once it's not related to ImGui. Thank you very much anyway!

@FelipeAndrioli
Copy link
Author

hey hi, I've found two issues with my code.

  1. I was using a vector pointer to my objects and not a vector to my objects pointers, so once it went out of the iteration the object was being deleted accessing some random values.

  2. I was updating all the objects before calling the draw function what is wrong when rendering multiple objects on OpenGL at least. The correct is to update each object individually before rendering it.

Now everything is working perfectly.

Thank you very much for all the help with ImGui even though all the issues were on me haha. Awesome project btw.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
label/id and id stack implicit identifiers, pushid(), id stack
Projects
None yet
Development

No branches or pull requests

3 participants