0

I'm trying to print in the screen the name of a Pin, but it's showing strange characters:

The pin with the strange characters

My class is like this:

struct Pin
{
    int ID;
    char Name[32];

    Pin(int id, const char *name)
    {
        ID = id;
        strcpy(Name, name);
    }
};

struct Node
{
    int ID;
    char Name[32];
    ImVec2 Pos, Size;
    float Value;
    ImVector<Pin> Inputs, Outputs;

    Node(int id, const char *name, const ImVec2 &pos, const ImVector<Pin> inputs, const ImVector<Pin> outputs)
    {
        ID = id;
        strcpy(Name, name);
        Pos = pos;
        Inputs = inputs;
        Outputs = outputs;
    }

    ImRect GetInputFlowSlotPos(ImVec2 offset) const
    {
        return ImRect(offset + ImVec2(Pos.x + 24.0f, Pos.y + 44.0f), offset + ImVec2(Pos.x + 4.0f, Pos.y + 24.0f));
    }
    ImRect GetOutputFlowSlotPos(ImVec2 offset) const
    {
        return ImRect(offset + ImVec2(Pos.x + Size.x - 24.0f, Pos.y + 24.0f), offset + ImVec2(Pos.x + Size.x - 4.0f, Pos.y + 44.0f));
    }
    ImVec2 GetInputSlotPos(int slot_no) const { return ImVec2(Pos.x, Pos.y + Size.y * ((float)slot_no + 1) / ((float)Inputs.Size + 1)); }
    ImVec2 GetOutputSlotPos(int slot_no) const { return ImVec2(Pos.x + Size.x, Pos.y + Size.y * ((float)slot_no + 1) / ((float)Outputs.Size + 1)); }
};

And I initialize like this:

inputsPin.push_back(Pin(0, "X"));
inputsPin.push_back(Pin(1, "Y"));
inputsPin.push_back(Pin(2, "Z"));

nodes.push_back(Node(0, "Di", ImVec2(40, 50), inputsPin, outputsPin));
nodes.push_back(Node(1, "Di", ImVec2(40, 150), inputsPin, outputsPin));
nodes.push_back(Node(2, "Di", ImVec2(270, 80), inputsPin, outputsPin));

When I print:

for (int slot_idx = 0; slot_idx < node->Inputs.Size; slot_idx++)
{
    Pin *pin = &node->Inputs[slot_idx];

    ImGui::SetCursorScreenPos(offset + node->GetInputSlotPos(slot_idx));
    ImGui::Text("%s", pin->Name);
}

*Edit: After I changed to std::string, the printed string is even more weird:

Another bug

6
  • Any specific reason you're not using std::string? Commented Jul 1, 2024 at 18:54
  • Tbh, I don't. I'm new in C++ world, should I change the char to std::string? Commented Jul 1, 2024 at 19:06
  • 2
    "I tried many things, however anything worked." - I believe you mean that "nothing worked". If that's the case, please provide more details on exactly how things you tried did not work. If indeed anything worked, then congratulations, but why are you then asking a question? Commented Jul 1, 2024 at 19:11
  • It was a typo, sorry. I changed all my char* to std::string, it's easier to work, but it's still showing me strange characters. Commented Jul 1, 2024 at 19:21
  • If you want debugging help, please post a minimal reproducible example. Commented Jul 1, 2024 at 19:39

2 Answers 2

1

I believe your issue was related to the fact that you assumed ImVector would behave like std::vector<>. ImVector<> is used internally by Dear ImGui and is NOT expected to be used by you. Note that its description says:

// - Important: our implementation does NOT call C++ constructors/destructors, we treat everything as raw data! This is intentional but be extra mindful of that,
//   Do NOT use this class as a std::vector replacement in your own code! Many of the structures used by dear imgui can be safely initialized by a zero-memset.
Sign up to request clarification or add additional context in comments.

Comments

1

I got to solve the problem, I had to change all my ImVector to std::vector.

Also, I changed my Node constructor to:

        Node(int id, const std::string name, const ImVec2 &pos, const std::vector<Pin> &inputs, const std::vector<Pin> &outputs)
        {
            ID = id;
            Name = name;
            Pos = pos;
            Inputs = inputs;
            Outputs = outputs;
        }

Adding the & before my vectors.

Besides that, I also changed my Pin constructor to:

    struct Pin
    {
        int ID;
        std::string Name;

        Pin(int id, const std::string &name) : ID(id), Name(name){};
    };

Now it's printing all the three pins. If I don't change to std::vector, it was not printing the first positions of pins.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.