[Action] Toggle Object Visibility [Solution Included]

I saw old post about this, but it was posted 3 years ago, I didn’t wanted to revive it.
Anyway, why I think that this is very useful and time-saving action to have?

For example, in my game I have options like resolution selection, and other dropdown-like menus.
So instead of creating animations, boolean variables, and other that would help with that, this would be the easier solution, especially when used with Panel Buttons extension.

https://streamable.com/mxbiyb


With one option, that maybe wouldn’t be an issue, but as far as I saw, there is no quick and easy workaround if I need about 10-15 of these or even more.

While the usecase is pretty subjective, you can make this action yourself. Just build a function using the extension builder.

Have that function have three events. The first event to toggle a boolean obiect variable of “togglevisible” or something like that as an action (with a trigger once condition), the second event would check if the boolean is false (and trigger once) and show the object. The third event would be the same but check for true and hide the object.

https://wiki.gdevelop.io/gdevelop5/extensions/create

I thought of that, but the whole point of my request is that we don’t have to add variable to all the objects we create. Because even if I do create that extension, I would have to create variables to all the objects I currently have and will have in future.
Unless there is a way to avoid this with extension, it would be really helpful to have such action.

EDIT: My bad. I just tried, and even if object that I am toggling with this extension doesn’t have “visible” boolean created, this does works.

No?

Variables that don’t exist are created when called via actions. They default to false for booleans, 0 for values, or “” for strings.

Edit: Just saw your edit. Yes, this is expected behavior. Variables do not have to be precreated.

If object “A” that I want to toggle is already hidden, I need to click object “B” 2 times in order to display (toggle) object “A”. And after I’ve done that once, it works normally. Any ideas why is that happening?

EDIT: Object “A” is group of 2 objects.

That’s likely going to depend on your events (conditions/etc). you might need to use it in a “For each GroupA” event.

If the object variable doesn’t exist, the toggle action both creates and toggles it to true. If hidden, it becomes visible. If visible, it stays visible the first time until it’s in sync. Unless, as previously mentioned, multiple objects are selected then you might need a for each object.

Alternatively, you could uses a changed state variable in the function instead of object variables. Either with or without a for each object

For each object
   Set changed boolean = false

  [Inverted] object is visible
       show object
      changed = true
  
   object is visible
   changed=false
        hide object
         

(The changed boolean is needed so, the first change doesn’t create a domino effect and cause the 2nd event to trigger)

This fixed the issue. Now I can use object groups, or objects separately, or even mix of those two and everything works fine. Thank you both for your help.