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

Creating checkboxes from a list of values #7472

Closed
duhnyel opened this issue Apr 4, 2024 · 4 comments
Closed

Creating checkboxes from a list of values #7472

duhnyel opened this issue Apr 4, 2024 · 4 comments
Labels
label/id and id stack implicit identifiers, pushid(), id stack

Comments

@duhnyel
Copy link

duhnyel commented Apr 4, 2024

Version/Branch of Dear ImGui:

Version 1.90.4 WIP, Branch: master

Back-ends:

imgui_impl_sdl3.cpp + imgui_impl_sdlrenderer3.cpp

Compiler, OS:

Linux + cmake(ninja + gcc)

Full config/build information:

Dear ImGui 1.90.4 WIP (19031)
--------------------------------
sizeof(size_t): 8, sizeof(ImDrawIdx): 2, sizeof(ImDrawVert): 20
define: __cplusplus=201402
define: __linux__
define: __GNUC__=10
--------------------------------
io.BackendPlatformName: imgui_impl_sdl3
io.BackendRendererName: imgui_impl_sdlrenderer3
io.ConfigFlags: 0x00000003
 NavEnableKeyboard
 NavEnableGamepad
io.ConfigInputTextCursorBlink
io.ConfigWindowsResizeFromEdges
io.ConfigMemoryCompactTimer = 60.0
io.BackendFlags: 0x0000000E
 HasMouseCursors
 HasSetMousePos
 RendererHasVtxOffset
--------------------------------
io.Fonts: 1 fonts, Flags: 0x00000000, TexSize: 512,64
io.DisplaySize: 1280.00,720.00
io.DisplayFramebufferScale: 1.00,1.00
--------------------------------
style.WindowPadding: 8.00,8.00
style.WindowBorderSize: 1.00
style.FramePadding: 4.00,3.00
style.FrameRounding: 0.00
style.FrameBorderSize: 0.00
style.ItemSpacing: 8.00,4.00
style.ItemInnerSpacing: 4.00,4.00

Details:

I'm trying to create checkboxes from an array of values but for some reason it doesn't set the value of the boolean inside the array. The checkbox just stays blank or quickly flashes checked and the value of the boolean doesn't change. What to do?

Screenshots/Video:

No response

Minimal, Complete and Verifiable Example code:

// here is a snippet to reproduce the issue
// as you can see it doesn't keep the checkbox checked 
bool foo[2] = {false, false};
ImGui::Begin("Hello, world!");                   
for(int i = 0; i < sizeof(foo); i++){
    ImGui::Checkbox("test", &foo[i]);
}
ImGui::End();
@inobelar
Copy link

inobelar commented Apr 4, 2024

@duhnyel This seems to be due to the lifetime of the variables. Apparently, you have foo existing "at frame time", that is - initialized to false , (possibly) modified during ImGui::Checkbox(), and rendered, and so on in a loop. Try to increase the lifetime of the variable: make it static, or store it outside the “main loop”:

// Static way, prefered for quick hacking
static bool foo[2] = {false, false};
ImGui::Begin("Hello, world!");                   
for(int i = 0; i < sizeof(foo); i++){
    ImGui::Checkbox("test", &foo[i]);
}
ImGui::End();
// Store-out-the-loop way
bool foo[2] = {false, false};

while(run_main_loop)
{
    // ... Inputs handling ...

    ImGui::Begin("Hello, world!");                   
    for(int i = 0; i < sizeof(foo); i++){
        ImGui::Checkbox("test", &foo[i]);
    }
    ImGui::End();

    // ... Rendering ...
}

@duhnyel
Copy link
Author

duhnyel commented Apr 4, 2024

That has seemed solve the problem but only partially. After moving the declaration outside the loop the top checkbox works but any others underneath, don't. I also can't make the list static for the actual implementation in my project.
The code i use is this:

bool foo[4] = {false, false, false, false};
while (run_main_loop){
        ImGui::Begin("Hello, world!");
        for(int i = 0; i < sizeof(foo); i++){
             ImGui::Checkbox("test", &foo[i]);
        }
        ImGui::End();
}

@inobelar
Copy link

inobelar commented Apr 4, 2024

@duhnyel Its an issue with same Checkbox label (all checkboxes have "test" label/ID). You can solve it by wrapping Checkbox() call with PushID()/PopID(). You can read about ID system here.

bool foo[4] = {false, false, false, false};
while (run_main_loop)
{
    if( ImGui::Begin("Hello, world!") )
    {
        for(size_t i = 0; i < IM_ARRAYSIZE(foo); i++)
        {
            ImGui::PushID(&foo[i]);
            ImGui::Checkbox("test", &foo[i]);
            ImGui::PopID();
        }
    }
    ImGui::End();
}

@ocornut ocornut added the label/id and id stack implicit identifiers, pushid(), id stack label Apr 5, 2024
@ocornut
Copy link
Owner

ocornut commented Apr 5, 2024

Thank you Yurii for your answers. They are both correct answers. Closing this.
Good luck Alyssa with your work ahead.

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