imgui: Unable to edit text in InputText

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’m trying to write dynamic text into input, but it’s impossible to edit it.

Screenshots/Video

ld2d_yu81gFlPlw

Standalone, minimal, complete and verifiable example: (see https://github.com/ocornut/imgui/issues/2261)

                ImGui::Text("Editing...: LevelObjectType::TEXT.");

                static char objectName[28];

                strcpy(objectName, object.lobjectname.c_str());

                static char text[MAX_OBJECTS_TEXT_SIZE];
                static char font[MAX_OBJECTS_TEXT_SIZE / 2];

                strcpy(text, object.text.value().c_str());
                strcpy(font, object.font.value().c_str());

                static int fontSize = object.fontSize.value();

                static int objectX = object.objectX;
                static int objectY = object.objectY;

                static int objectRotation = object.objectRotation.value_or(0);

                ImGui::InputText("Object Name.", objectName, IM_ARRAYSIZE(objectName));

                ImGui::InputText("Text.", text, IM_ARRAYSIZE(text));

                ImGui::InputText("Font.", font, IM_ARRAYSIZE(font));

How to make text input have already pre-defined content that you can edit?

About this issue

  • Original URL
  • State: closed
  • Created a year ago
  • Comments: 16 (8 by maintainers)

Most upvoted comments

What do you mean “impossible to edit”? You are editing the text in the animated GIF.

All of those static variables in your code suggest that you don’t know what the keyword static actually does. Better read up on it. Also, you omitted copying back the edited values. You are aware that they don’t copy themselves back, right? It looks like you just copy the value you want to edit into a buffer for editing every frame, edit that buffer and then discard it.

Thank you! Now it works! But I still don’t understand, is writing static bad? Because I saw in the ImGui manual that every variable is static.

Was about to post the comments regarding static that Omar just posted. Regardless, I would say it is “bad” to write code you don’t understand just because you saw it somewhere. Always try to understand code or snippets you copy, look up functions or keywords you don’t know and learn about them. Otherwise you are in for a lot of unwanted bugs and side effects.

Edit: there is nothing inherently good or bad about any programming concept. Things like static are great when used correctly and fatal when used incorrectly.

Hello @xzripper,

static essentially means global. As pointed by Daniel, and assuming you are referring to the “Text” field reverting, you are copying object.lobjectname.c_str() to objectName[] every frame, which acts as a revert as soon as the InputText() is inactive.

To edit std::string strings you may want to #include "misc/cpp/imgui_stdlib.h" + link with misc/cpp/imgui_stdlib.cpp` and use those versions: https://github.com/ocornut/imgui/blob/master/misc/cpp/imgui_stdlib.h