Script organization best practices 2024

Started by FauxSang, Mon 22/07/2024 00:13:05

Previous topic - Next topic

FauxSang

I've been searching the forums, docs, and google and I've found many discussions on this topic. But many are old, some suggestions seems fraught or impractical, etc.  So I'm looking for advice on the latest & greatest best practices for code organization, with particular emphasis on the global script.

I'm using the BASS structure, which means every character in my game will have _Look, _Interact, and _UseInv hooks (at least).  The interact and useInv functions in particular will contain lengthy switches and conditionals for lots of contexts and different inventory items, etc. etc.  I'm only like 25% into dev and the global script is already over 1000 lines.

You see where I'm going with this.  I don't want all of this is one huge file.  I want to at least create separate script files for each character and have some semblance of code separation / organization.  Exporting/importing every function by name in script headers seems...wild. I know I'm not alone. How are folks handling this in the latest versions of AGS?

As an aside, this begs the question why ags scripting doesn't have a simple "include" statement, like #include otherfile.asc so humans can split things up but the compiler can cram it all back together into one huge script if it wants. Obviously this was thought of years ago; I'm curious about the technical reason why it's not there.

eri0o

I don't understand, just create a script function to deal this in a new script and call from global script.

Khris

You don't have to export functions (only variables), and you can organize stuff with structs.

Example module code:

Code: ags
// header
struct Roger {
  import static void handleInteract();
};

// main module script

static void Roger::handleInteract() {
  Display("Interacting with NPC1");
}

In the global script you can now do
Code: ags
function cRoger_Interact(Character *theCharacter, CursorMode mode)
{
  Roger.handleInteract();
}

Crimson Wizard

#3
Quote from: FauxSang on Mon 22/07/2024 00:13:05As an aside, this begs the question why ags scripting doesn't have a simple "include" statement, like #include otherfile.asc so humans can split things up but the compiler can cram it all back together into one huge script if it wants. Obviously this was thought of years ago; I'm curious about the technical reason why it's not there.

No specific technical reason prevents this, it was just never prioritized and addressed.

OTOH, "include" is a C/C++ kind of thing, while some people suggested to make AGS script more C#-kind, where you don't use headers at all, and declarations are imported either automatically or by a sort of "import" statement, rather than joining script files verbatim. So there are multiple options to consider.

Then, there's of course a idea to let users put event functions in arbitrary scripts, not only global script. If that is done, then there will be no or less need to "include" asc files into each other.

SMF spam blocked by CleanTalk