calling or using function inside dialog

Started by geork, Sun 05/07/2009 21:11:54

Previous topic - Next topic

geork

 hey all!
   I have come across a problem to do with calling a function inside dialog i have a function inside the global scripts GoBack(int gg). I tabbed in dialog, then coded:
 
Code: ags

    GoBack(beforetalk1);
  

  when that wouldn't work, I searched the index, and tried
 
Code: ags

  import function GoBack(beforetalk1);
  

   all this at the end of a dialog.
   first time it complained "Undefined token 'GoBack' " fair enough
   Second time however, it said "import not allowed inside function body"
    Is there a way around this?
    thanks

GuyAwesome

Try putting the import line in the Global script header (GlobalScript.ash). I'd guess functions have to be globally accessible to be used in Dialogs (based on the first error), and that will fix that.

geork

 Thanks!
  Yep, it worked fine! I never thought that script was particularely useful, but now I know better...

monkey0506

#3
To clarify in order to use any script variable or function that is declared in another script you must import it into a script header which comes prior in the list of scripts to where you actually want to use it.

For example if in your GlobalScript.asc file you have:

Code: ags
int somevar;
export somevar; // allows our int to be imported into a header

function some_func() {
  // ...
}

// functions don't have to be exported


Then in GlobalScript.ash (the GlobalScript header file) you would put:

Code: ags
import int somevar;
import function some_func();


Then you can use somevar and some_func in GlobalScript.asc, any room script, and any dialog script.

If you want to use them in a script listed higher than GlobalScript.asc, you would simply move the import lines to that script header (*.ash file).

;)

Further, make sure you never include a function/variable declaration in a script header! Your headers are automatically included before every script so if you put a variable directly into a script header (instead of importing it) then it would create a separate variable for every single script! Then you'd be left wondering why the values didn't match!!! := So make sure if you need to use a function/variable in multiple scripts to declare it in the first script you need it in, and import it into the header (remember of course variables must be exported to be imported).

geork

 Thanks for the tips!
   In fact, I had to call a function again (outside diolog), and would have been stuck without the advice ;)

SMF spam blocked by CleanTalk