imgui: InputText doesn't update its content
Version/Branch of Dear ImGui:
Version: v1.89.8 Branch: master
Back-end/Renderer/Compiler/OS
Back-ends: SFML, OpenGL (https://github.com/SFML/imgui-sfml) Compiler: GCC (G++) Operating System: Windows 10
My Issue/Question:
I observed that if the variable is declared as static (variable required: std::string* str in imgui_stdlib InputText, second argument), the content within InputText does not update, but it can be edited. Conversely, if the variable is not static, the content within InputText updates, but it cannot be edited.
Screenshots/Video
Reference on static variable:
Reference on non-static variable:
Standalone, minimal, complete and verifiable example: (see https://github.com/ocornut/imgui/issues/2261)
// Includes.
#include "SFML\include\SFML\Graphics.hpp"
#include "imgui\include\imgui.h"
#include "imgui\include\imgui-SFML.h"
#include "imgui\include\imgui_stdlib.h"
#include <iostream>
int main() {
sf::RenderWindow window(sf::VideoMode(500, 500), "Hello!");
ImGui::SFML::Init(window);
sf::Clock clock;
while(window.isOpen()) {
ImGui::SFML::Update(window, clock.restart());
sf::Event event;
ImGui::Begin("yummi");
while(window.pollEvent(event)) {
ImGui::SFML::ProcessEvent(event);
if(event.type == sf::Event::Closed) {
window.close();
}
}
// Main issues begin.
static bool check = true;
ImGui::Checkbox("checkbox", &check);
static std::string inputContent = check ? "hello world" : "bye world"; // remove / or add 'static' to see difference
ImGui::InputText("input", &inputContent);
// Main issues end.
ImGui::End();
window.clear();
ImGui::SFML::Render(window);
window.display();
}
return 0;
}
When inputContent is static, the content does not get updated. However, it is possible to edit the content inside the Input. Whereas when inputContent is not static, the content gets updated and, in that case, it is impossible to edit the content in the input. Any thoughts?
About this issue
- Original URL
- State: closed
- Created 10 months ago
- Comments: 21 (10 by maintainers)
Things are behaving exactly as they should.
This is a C++ understanding issue and on your insistence to open issues without reading our answers I am going to close this. Please investigate by yourself.
It seems like you don’t understand the
statickeyword. If you usestatic, it’s only initiated once, consider the following code:Will output:
0 1 2, this is becausevalueis only set to0once. Without the static keyword, the output would be:0 0 0, becausevalueis set to 0 every time.So with
static, the text can be edited, but the checkbox doesn’t work because= check ? "hello world" : "bye world";is only executed once. And withoutstatic, the text couldn’t be edited because it’d reset every loop. This is expected behavior.To achieve your goal, do this instead:
If your string is static, it gets initialized once when the static is first encountered. It isn’t “updated” (or rather reinitialized) each iteration. That is how static works, don’t use static if that is not your intended behavior.
Without the static, the string is not impossible to edit. You are editing it. But then on the very next iteration of your loop, you overwrite the string yourself, loosing your edit again. It is your code doing this, it is not a problem with ImGui itself.
The same goes for your other issue. It is a bug in your code (despite your insistence that you don’t have any bugs, that would be a bold claim even for experienced programmers) that stems from a wrong understanding of the C++ language.
I’ve been trying to fix this for a few days. Is it that difficult to write a solution that, I wager, will take no more than one line?