Adventure Game Studio

AGS Support => Beginners' Technical Questions => Topic started by: Compozitor on Sun 12/05/2013 20:19:02

Title: [SOLVED]Can't get into functions... always get "Undefined Token" error.
Post by: Compozitor on Sun 12/05/2013 20:19:02
Hello. I'm trying to get along with scripting and can't find answer to my problem . I spended 3 hours in internet surfing for it and finally came here for help..

Here is my script.

Code (AGS) Select
function game_start()
{
  MainMenuGUI(gNewGame);
  cCaptain.SetIdleView(2, 0);
}


and the function itself

Code (AGS) Select
function MainMenuGUI(GUI *theGui)
{
  if (theGui == gNewGame)
  {
  PauseGame();
  gNewGame.Visible = true;
  }
}


When i trying to run game i get:
Code (AGS) Select
GlobalScript.asc(7): Error (line 7): Undefined token 'MainMenuGUI'


Please help to acknowledge this..
With Best Regards
Title: Re: Can't get into functions... always get "Undefined Token" error.
Post by: Khris on Sun 12/05/2013 21:05:21
You can only call a function after you have defined it. Move your custom function above game_start().
Title: Re: Can't get into functions... always get "Undefined Token" error.
Post by: Compozitor on Sun 12/05/2013 21:11:26
Thank you very much! It was soo simple... ^) By the way i think it's nowhere documented about placing custom functions before game_start().
Thank you again!
Title: Re: [SOLVED]Can't get into functions... always get "Undefined Token" error.
Post by: Ghost on Sun 12/05/2013 21:20:30
Not before game_start- every function must be above any other function that calls it. Modern(er) programming languages allow you to place functions whereever your want, but AGs (being RETRO!) requires this style of declaration  :P
Title: Re: [SOLVED]Can't get into functions... always get "Undefined Token" error.
Post by: DoorKnobHandle on Sun 12/05/2013 21:28:14
To elaborate on this issue: Modern programming languages such as Java and C# that do not require functions to be implemented before they are called do this at the expense of compile time. They use a so-called two-pass compiler, which scans every code file twice - once just to grab all the signatures of all functions/classes/etc and then a second time for the actual implementation. C/C++ and AGS script only runs through your script once, so you need to pay attention to the order of functions or put in prototypes - but your code, as a result, compiles a bit faster. So, long story short, it's not a bug - it's a FEATURE! :)