[RESOLVED} <Changed title> In what order are the scripts compiled?

Started by Joacim Andersson, Mon 18/11/2024 10:06:29

Previous topic - Next topic

Joacim Andersson

Edit: I solved this part but it raised another question, read my own reply post.

Hello, in my GlobalScript file, I have a function like this:
Code: ags
bool KnownNPC(Character* char)
{
  // Some code that loops through an array and checks
  // if the character is in there.
}
In the GlobalScript.ash header file I then have the import statement.
Code: ags
import bool KnownNPC(Character* char);
I then call this function from a Room script, but I get an error with the undefined symbol 'KnownNPC'. Why is that?

I have other functions in a separate script and the import statement in that header file, but I need this function in my GlobalScript.asc file for a specific reason.

Joacim Andersson

I'm sorry, I was wrong above. The call to the function does work from a Room script, but I also needed to call it from another script. I noticed that I had to put the import in that script's header. I'm guessing this is because of how and in what order the scripts are compiled if the header is included or not.

But if I have several of my own scripts (ergo not Room scripts) how will I know in which order they will be compiled?

Snarky

They are compiled in the order:

  • Scripts from the Script node of the project tree, in the order listed
  • Dialog scripts
  • Room scripts

AGS 3 requires you to declare things before you use them.

(Edit: In some quick testing, the crossed-out part doesn't seem to be correct, and it appears possible to import both functions and variables to script above where they are defined. The issue is within scripts, where you have to define things above where you use them.) which means that other scripts (other than the room scripts and dialog scripts) cannot use things that are declared in GlobalScript, because GlobalScript is always at the bottom of the script list. If you put an import statement in the header of the script above it will compile, but I am almost certain it will crash when it runs.

(In the new compiler in AGS 4, you can use forward declaration if you want to use something before the script that defines it, so this limitation no longer applies.)


See further discussion (including about how headers work) here: https://www.adventuregamestudio.co.uk/forums/advanced-technical-forum/ags-scripting-language-architecture/

Khris

Quote from: Joacim Andersson on Mon 18/11/2024 10:06:29I have other functions in a separate script and the import statement in that header file, but I need this function in my GlobalScript.asc file for a specific reason.

Can you elaborate on this?

Also, the word "char" is a keyword and not allowed as variable name. Changing it to "cha" makes the code work as expected.

Crimson Wizard

#4
Maybe not as important here, but I'd like to clarify, that this is not the order of compilation that we are discussing here, but the order of dependency.

Compilation is a process of changing a script into a program runnable by the engine. In order to compile a script AGS does not need other compiled scripts, it needs only to read their headers. Thus compilation may be done in any order, even done in parallel.
After all scripts are compiled, they are linked, resolving function imports, and it is here where their dependency becomes important.

The fact that AGS restricts their dependency to a top-down list is just AGS own quirk. I suppose that it could be redesigned with certain effort.

But this all is just a general information, and does not mean to answer the questions posted here.

Joacim Andersson

Quote from: Khris on Mon 18/11/2024 12:54:30Also, the word "char" is a keyword and not allowed as variable name. Changing it to "cha" makes the code work as expected.
I know that char is reserved, I wrote this directly when I posted the question and used the wrong name.

I have a separate file apart from the GlobalScript file from which I have used the import in the header and it worked fine. Re-read my own reply to my original question for more information.

Joacim Andersson

Quote from: Crimson Wizard on Mon 18/11/2024 16:35:08Maybe not as important here, but I'd like to clarify, that this is not the order of compilation that we are discussing here, but the order of dependency.
You're correct, if you want to be pedantic about it  :smiley: but all the preprocessing is still part of the compilation process even if it's not done by the actual compiler.

Crimson Wizard

Well, Snarky's reply provides necessary info then.

The only ammendment that I may make is the optional hidden script generated from "Global Variables" panel.
If you have any variables added there, then AGS will autogenerate a script with some internal name (like "__GlobalVariables.ash"/"__GlobalVariables.asc"), where these vars are declared and initialized.

This hidden script is processed above all others.

Joacim Andersson

Quote from: Crimson Wizard on Mon 18/11/2024 18:47:26This hidden script is processed above all others.
That makes sense. I'm trying to avoid global variables as much as possible, however some functions I want to have global access to. I also want global access to some named constants but I haven't figured out how to do that, but I've posted about that in a different thread.

SMF spam blocked by CleanTalk