Is it better practice to have separate scripts for GUIs for their respective aspects of the game, or should GUI all be located in a single script, maybe with function calls for each GUI aspect of the game? Let me offer an example to help explain my question.
I have a main GUI, which has things like health, spell points, hotkeys, and a mini-map. I also have GUI elements for things like my resource gathering system. When you collect resources, a window pops up showing you what, and how much you collected. There’s also a button you push to start gathering resources.
In this example, should the main GUI and resource gathering elements/code all be in one GUI script, with function calls to MainGUI() and ResourceGUI(), or is it better to have the main GUI in one script, and the resource gathering GUI code in the resource gathering script?
If all these GUI elements are to be displayed at once, then it will probably be simpler to use a single script. However, you might extract particularly large or complicated bits of GUI into their own classes. If they are shown separately then the GUI screens might break down more logically into separate classes anyway.
What have you got so far?
So far, I have just what my example says: A main HUD that shows player hp, sp, and stam, a toolbar of hotkeys, a minimap and a target window. That will be persistent.
Then I have non-persistent GUI systems, like the resource gathering windows, inventory window, character sheet window, and crafting window. These GUIs are all graphical representations of significant systems in the game, so they are pretty complex in terms of how they interact with the game code.
While centralizing all GUI in one script seems easier in terms of lanes of communication between scripts, assigning system specific GUI to its respective system’s scripting makes more sense in terms of having direct access to the necessary functions and variables. Then ther is the issue of where a certain GUI controll belongs. For example, a button to gather resources uses the resource system’s scripting, but needs to be able to be placed on the hotkey toolbar on the main GUI as well.
I know I could get it to wrk either way. I just can’t determine which way is better. Every time I try it one way, I run into an obstacle that suggests I should do it a different way. This is my firsat attempt to design a GUI that is actually functional, so I have no experience to rely on except what I get from trial and error.
One way that I like is to have a main GUI script that calls a Display() function residing on other objects. That way you don’t need to build a whole bunch of accessory functions just for display purposes. You also don’t have to worry about an object’s GUI code interfering with the main GUI code. Buttons can send code directly to different objects.
The only problems I have had with it is that the information flow is more top down. ie its harder to have feedback from the called objects.
Another method is to have classes like “Resources” and “ResourcesDisplayer.” You could have your resource system send a “ResourceSystemMessage” to the main GUI code. The main GUI code might look like
var resourceMessages : Array = new Array();
function NewResourceMessage(newMessage : ResourceSystemMessage) {
resourceMessages.Add(newMessage);
}
function OnGUI() {
//various main GUI functions
for each message in resourceMessages
Display(message);// or maybe message.Display()
}
function Display(resourceMessage) {
GUI.Label(resoureMessage.name)
//etc.
}