Unfortunately, you can’t write to DataTable unless you invest in a plugin or C++. (if **you **can, give me a shout!, my knowledge might be outdated here)
When it comes to performance, widgets that are hidden do not tick, the same is true for widgets that are not on the screen (like a healthbar that follows the enemy who is currently not on the screen). The widget will reside in memory, though. Depending on how often you’ll be seeing that widget, you’ll need to decide whether it’s better to create and then destroy it or to just keep it hidden. If it’s the main menu item that will be accessed once per game session, I’d say create, save its state and get rid of it. I’d deem the cost of keeping a simple widget in memory negligible at best, although if it’s not to be used again, it’s still good practice to get rid of it. I would not do it for the inventory full of items. Generally speaking, from my (limited) experience, focus on writing the code, optimise it later once you know how the pieces interconnect.
You have several solutions:
- The one you went for, where you nest one widget in the other and keep both.
- Create them separately, set a reference and hide the settings one (not much difference from the 1st one)
- Create, save state and destroy the settings widget (I believe that’s what you’re really after)
Not going to mention **1. **as you have it figured out already.
As for ****2. You’re going to need direct reference sooner or later anyway for something else so I’ll mention it quickly. Let’s say you have Main and Settings menu. To create a direct reference, create a variable in the Main menu of type Settings menu, the following was done on the Main menu widget:
The variable currently does not reference anything, if you try to access it now, you’ll get a null pointer (accessed none). When creating widgets/objects, reference one by the other:
You can do it the other way round, too or you can reference both ways (which is not always recommended as it may lead to circular dependencies), again this depends which way you’d like the information to flow.
As for 3. The other posters here mentioned saving that data and destroying the widget and that would definitely work. It’s also the cleanest solution but it would require you recreated the widget from scratch based on the saved data.
Since your Main “menu with many buttons” seems to be persistent you can store the state of the settings there. Here’s one way to do it:
- Create an enumerator for the settings, you may need more than one, depending on complexity:
Add that variable to your Main.
-
I’ll assume here that in your Main you have a button that opens the Settings. Have that button create the Settings menu and feed it the enumerator (give us a shout should you have doubts about that) which can set its (remembered) values. When you create the Settings (onButtonClick), set up a direct reference in the Settings that links back to the Main. (what I mentioned in 2. but in the other direction) so the Settings can send the data to be stored in Main.
-
When you apply a graphical setting in the Settings, use the reference to the Main to set the enumerator to the desired state. Destroy Settings onMenuClose() equivalent. Next time it’s created it will fetch the saved data from the Main.
The above can be achieved with EventDispatchers, too which work quite well with all sorts of widgets, whether they’re persistent or not.