pkdawson / imgui-godot

Dear ImGui plugin for Godot 4
MIT License
429 stars 24 forks source link

Can't make a closable window with GDScript #73

Closed Haewen closed 3 months ago

Haewen commented 3 months ago

I can't seem to find a way to make the built-in close buttons on the imgui windows usable.

It might be an issue with everything that uses the p_open parameter, as it's originally supposed to take a reference, but in the GDScript version it expects an array instead.

bool Begin(name: StringName, p_open: Array = [], flags: BitField[WindowFlags] = 0) static

I can make the button show up, if I pass it an array with a true value in it, but even if it's a variable, it doesn't seem to get updated.

pkdawson commented 3 months ago

When you're using an Array parameter that corresponds to a pointer in the original ImGui API, it's the Array itself that gets modified. So you typically want to store it as a member variable.

var win_open := [true]

func _process(delta: float) -> void:
    if win_open[0]:
        ImGui.Begin("a closeable window", win_open)
        ImGui.End()

If you tried something like this:

# doesn't work
ImGui.Begin("a closeable window", [open_bool])

That creates a temporary array, and the bool variable is unmodified, because that's not possible.

It's a little clunky, but those are the limitations of GDScript.

Haewen commented 3 months ago

Oh, that absolutely works. Never thought to check the first element of the array. Thank you!