Altering rooms from dialogue?

Started by Tyr, Sat 08/08/2009 17:22:48

Previous topic - Next topic

monkey0506

Can you please post the code you're using? Because I just verified (again) that it does in fact work if you do it properly. It should look like this:

Code: ags
// GlobalScript.ash - ASH is the HEADER

import bool knowdog;

// GlobalScript.asc - ASC is the MAIN SCRIPT

bool knowdog;
export knowdog;

// dialog script
  knowdog = true;

Tyr

Aha, that works great, thanks.
I think the problem was that I had the knowdog and the export in the header. I guessed that was the main thing.

So.... the header is just there to carry stuff and actual functions should all be the main one?

monkey0506

#22
Think of it like this...every header gets attached to the top of every script. So your GlobalScript.ash file will get attached to the top of each of your dialog scripts and your room scripts.

If you define a variable or function in any header (*.ash) file then it is going to create a new copy of that variable/function for every script.

If you import the variable or function in a header file, then the one and only copy of that variable/function that is defined is going to become accessible in every script.

The difference? If you define "knowdog" in the header, then change its value from one script, then check it in another script, you are no longer checking the same variable. If you were to import it (which is what you should do) then any changes to the "knowdog" variable will be reflected in all of your scripts.

Note that as I said before, imports only work on subsequent scripts. So if you create a script called "SomeScript" (which gives you SomeScript.ash, the header, and SomeScript.asc, the main script) and then you have an import in GlobalScript.ash, SomeScript.asc will never have access to that import. However, if you have an import in SomeScript.ash, GlobalScript.asc will have access. The headers only get included in the scripts that come after that header in the list of scripts; dialog and room scripts are always considered to be at the end of the list, after GlobalScript.

Further, although you must use the export keyword when working with variables, you do not use it for functions. Variables must be exported, both variables and functions must be imported, in order for these items to be accessible from a different script than the one in which they are defined.

Also, although it might not seem like that big of a deal for functions to have separate copies, don't put functions into any *.ash header file. All that will do is create duplicate copies of the function which will bloat the size of your compiled EXE, not to mention taking longer to compile since you would have to rebuild every single script every time (which can take a while if you have a large number of rooms). Always use imports in header files and define the variables/functions in the main script files.

Tyr

Aha right thanks, so its like a header included in web pages...I get it.

SMF spam blocked by CleanTalk