Adventure Game Studio

AGS Support => Beginners' Technical Questions => Topic started by: FortressCaulfield on Fri 24/01/2025 00:32:36

Title: Global vs not global
Post by: FortressCaulfield on Fri 24/01/2025 00:32:36
Is there any sort of rule of thumb regarding how much should go in your globalscript before you consider making it it's own separate add-on script? What are the advantages or disadvantages of doing so, and how would I set that up?
Title: Re: Global vs not global
Post by: Khris on Fri 24/01/2025 01:39:06
Keeping your code tidy is always a good idea, so unless you're just adding a few lines to the global script, I'd start putting everything in a separate script from the get-go. There are no disadvantages, you're just organizing your code.

There's nothing to set up really; just add a script and put functions inside.
https://adventuregamestudio.github.io/ags-manual/MultipleScripts.html

Handler functions for character or inventory events still need to be inside the global script unfortunately, but IIRC AGS4 will address this.
Title: Re: Global vs not global
Post by: Crimson Wizard on Fri 24/01/2025 01:48:08
Separating the code among multiple modules is a matter of organization and convenience, and AGS is not much different in this regard compared to other programming systems.

I don't think there's any common agreement on "how much" code should be in a single script, it's a subjective view. But I'd say that besides the amount, there's a strong reason to split the code belonging to different features. To give a quick example: math functions, drawing functions, functions for mini-games (not related to regular gameplay), and so on.

Advantages of having code in separate scripts are:
1. Easier to work with the big project.
2. Enforce encapsulation (https://en.wikipedia.org/wiki/Encapsulation_(computer_programming)): the idea that a feature hides its internal work from other code, which helps making it consistent and stable.
3. Having a module with certain feature, or thematically related functions, which you may share among multiple projects.

The only disadvantage I may think of is that this may require some extra work organizing things, but it usually pays off by making it easier to work further.

In AGS there's a cap of total script modules that you may have, for technical reasons: it's 128 in the current stable version and 1024 starting with 3.6.2.
Title: Re: Global vs not global
Post by: Crimson Wizard on Fri 24/01/2025 01:48:46
Quote from: Khris on Fri 24/01/2025 01:39:06Handler functions for character or inventory events still need to be inside the global script unfortunately, but IIRC AGS4 will address this.

3.6.2 already lets select the script module for the handlers:
https://www.adventuregamestudio.co.uk/forums/ags-engine-editor-releases/ags-3-6-2-beta-1-a-wip-3-6-update/
Title: Re: Global vs not global
Post by: FortressCaulfield on Fri 24/01/2025 13:40:29
This is all very helpful, thanks! I assumed all the char and inv stuff had to be in global but between that and my custom stuff it's getting really crowded!