-3

How can I output some text if checkbox is checked without having unlimited outputs?

I'm using C++ ImGui for my project.

So when checkbox in ImGui menu is checked, it should output some text in console.

CheckBox:

ImGui::Checkbox("mycheckbox", &bES_active);

This is how I did it:

if(bES_active)
{
    std::cout << "Checkbox Clicked!";
}

How can I stop it at first output message?

image

EDIT: It was in a while loop, so that's why it gave unlimited outputs. It doesn't work outside of it, though.

Whole code:

       


    // Main loop
    while (!glfwWindowShouldClose(Window)) {
        glfwPollEvents();

        // Start the Dear ImGui frame
        ImGui_ImplOpenGL3_NewFrame();
        ImGui_ImplGlfw_NewFrame();
        ImGui::NewFrame();

          if (bESP_active)
          {
              std::cout << "ESP Activated\n";

          }

        if (GetAsyncKeyState(VK_INSERT) & 1) {
            bMenuVisible = !bMenuVisible;
            if (bMenuVisible) {
                ShowMenu(Window);
            }
            else {
                HideMenu(Window);
            }
        }

//-------------

->here is imgui menu drawn...
...
ImGui::Checkbox("ACTIVATE ESP", &bESP_active); 
...
//----------------
        if (bESP_active) {
            
            do {
                LocalPlayer = Mem.readMem<uintptr_t>(Client + dwLocalPlayer);
            } while (LocalPlayer == NULL);

            int getLocalHealth = Mem.readMem<uintptr_t>(Client + m_iHealth);

            ViewMatrix = Mem.readMem<Matrix>(Client + dwViewMatrix);
            int LocalTeam = Mem.readMem<int>(LocalPlayer + m_iTeamNum);

            for (short int i = 0; i < 64; ++i) {
                uintptr_t Entity =
                    Mem.readMem<uintptr_t>(Client + dwEntityList + i * 0x10);
                if (Entity == NULL || Entity == LocalPlayer) continue;
                if (Mem.readMem<bool>(Entity + m_bDormant)) continue;

                int EntityTeam = Mem.readMem<int>(Entity + m_iTeamNum);
                //    int Health = Mem.readMem<int>(Entity + m_iHealth);
                bool OnSameTeam = EntityTeam == LocalTeam;
                //    bool OnSameTeam = EntityTeam == Health;

                if (OnSameTeam && !bShowTeam) continue;  // nor this !2

                if (OnSameTeam)  // not really used !1
                {
                    Color->R = 0.0f;
                    Color->B = 1.0f;
                }
                else

                {
                    Color->R = 1.0f;
                    Color->B = 0.0f;
                }

                Vec3 EntityLocation = Mem.readMem<Vec3>(Entity + m_vecOrigin);

                Vec2 ScreenCoords;

                if (!WorldToScreen(EntityLocation, ScreenCoords, ViewMatrix.VMatrix))
                    continue;
                //(0, -1) Start from the center, and from the button | ScreenCoords

                if (bDrawEnemyLines)
                {
                    DrawLine(LineOrigin, ScreenCoords, Color);
                }
            }
        }

        glfwSwapBuffers(Window);
        // Rendering
        ImGui::Render();
        int display_w, display_h;
        glfwGetFramebufferSize(Window, &display_w, &display_h);
        glViewport(0, 0, display_w, display_h);
        glClear(GL_COLOR_BUFFER_BIT);
        ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData());
luckioo
  • 13
  • 2
  • @paulsm4 How is it not a C++ question? It asks specifically about C++ code using a C++ library. – Yksisarvinen Jul 15 '22 at 22:12
  • @paulsm4 Looks like it was in a while loop. How can I execute it outside of it so it works? I tried doing it right above my main loop but now it doesn't output anything when checkbox is clicked. – luckioo Jul 15 '22 at 22:17
  • 1
    @luckioo The code you provided is insufficient to resolve your problem. You need to provide more code (preferably with explanation) that is relevant here. As it currently is, it is very difficult to analyze how the `if` statement is being triggered. – akaAbdullahMateen Jul 16 '22 at 00:22
  • @akaAbdullahMateen if statement triggers ESP cheat for csgo...So When the checkbox is clicked, it should activate a ESP hack (which works) but it should also print only ONE message that it's actually checked. I updated a question so you can see the whole code that is being triggered when checkbox is checked. – luckioo Jul 16 '22 at 17:18

2 Answers2

2

In ImGui you can do this:

if (ImGui::Checkbox("mycheckbox", &bES_active))
{
    std::cout << "Checkbox Clicked!";
}

Or generally, you can do what @kuba-hasnt-forgotten-monica proposed:

static bool bES_active_old = bES_active;
ImGui::Checkbox("mycheckbox", &bES_active);

if(bES_active_old != bES_active)
{
    bES_active_old = bES_active;
    std::cout << "Checkbox Clicked!";
}
thedemons
  • 1,139
  • 2
  • 9
  • 25
0

It’s quite simple: when you wish to detect a state change, you need to know what the previous state was.

So, each time you check the state, compare it to a variable where you put the previous state. If they differ, that’s the single trigger you need. Right after the comparison you have to store the current state in the previous state variable.

Kuba hasn't forgotten Monica
  • 95,931
  • 16
  • 151
  • 313