GuiElement cannot be sub-classed?

  • As far as I can tell, GuiElement cannot be sub-classed from another package, as no constructor is public. I am curious to know which is the rationale behind this decision.


    My use case for sub-classing GuiElement directly rather than one of its net.risingworld.api.gui sub-classes: I am trying to implementing some form of dialogue box (kind of...) which 'auto-layout' itself (to some extent) and I need a GuiElement sub-class with no visible output but able to position its children. I can build on GuiPanel, but I thought to avoid the overhead of an intermediate class whose capabilities I do not need.


    Thanks!

  • Well, this doesn't work unfortunately. If you try to add a custom GuiElement to the player screen (or what's the reason behind sub-classing the GuiElement class?), the client doesn't know how to display it. Currently every API GuiElement is covered by a "real" element in our underlying gui, so if you create a custom GuiElement in your plugin, the client doesn't know what to do


    It's more preferable if you just create a regular class which consists of your particular GuiElements. Instead of adding it to the player gui directly (by calling Player.addGuiElement()), you might add a method to your gui class instead (something like "addToPlayerGui(Player)" for example) which adds the particular gui elements manually.

  • Thanks for the reply.

    If you try to add a custom GuiElement to the player screen (or what's the reason behind sub-classing the GuiElement class?), the client doesn't know how to display it. Currently every API GuiElement is covered by a "real" element in our underlying gui, so if you create a custom GuiElement in your plugin, the client doesn't know what to do

    Yes, it makes sense.


    Instead of adding it to the player gui directly (by calling Player.addGuiElement()), you might add a method to your gui class instead (something like "addToPlayerGui(Player)" for example) which adds the particular gui elements manually.

    This is more or less the way I have taken: I sub-class GuiPanel (the nearest type among the ones existing) to add specific child elements with specific relationships among them. A show(player) method adds the GuiPanel directly via Player.addGuiElement() and, in addition, a loop adds each child, again via Player.addGuiElement().


    So far, it worked...

  • This is more or less the way I have taken: I sub-class GuiPanel (the nearest type among the ones existing) to add specific child elements with specific relationships among them. A show(player) method adds the GuiPanel directly via Player.addGuiElement() and, in addition, a loop adds each child, again via Player.addGuiElement().

    This sounds like a good approach. Maybe we can also add a protected "onAttach()" method (or something like that) which will be called once a gui element is added to the player screen. If you sub-class a GuiPanel then, for example, you could just override this method, and add all childs via Player.addGuiElement() (so it wouldn't be necessary to call a specific method like show())

Participate now!

Don’t have an account yet? Create a new account now and be part of our community!