working with varaibles, got nested function error...

Started by Matt Brown, Thu 05/06/2003 17:09:03

Previous topic - Next topic

Matt Brown

I'm trying to learn how to use variables, and I got an error in my first script.

// room script file
int computer_cd;

function hotspot2_a() {
 // script for hotspot2: Interact hotspot
 if (computer_cd == 0) {
   DisplayMessage(7);
   AddInventory(2);
 }
 if (computer_cd == 1) {
   Display("you have taken all the CD's there are.");  
 }
 if (computer_cd == 2) {
   Display("Yo. listen to what I'm saying foo. no CD.");
 }
 if (computer_cd == 3) {
   Display("Teh Computer has nothing left to give.");  
 }
 if (computer_cd < 3) {
   computer_cd += 1;
 }
function object0_a() {
 // script for object0: Interact object
DisplayMessage(15);
ObjectOff(0);
GiveScore(10);
AddInventory(1);  
}


have an error in line 22, saying nested functions not supported....

help?
word up

Scummbuddy

whats going on above this function? are you sure that one is closed properly?
- Oh great, I'm stuck in colonial times, tentacles are taking over the world, and now the toilets backing up.
- No, I mean it's really STUCK. Like adventure-game stuck.
-Hoagie from DOTT

Scorpiorus

#2
Mr.Panda, the first function is not closed. ;)

// room script file
int computer_cd;

function hotspot2_a() {
// script for hotspot2: Interact hotspot
if (computer_cd == 0) {
DisplayMessage(7);
AddInventory(2);
}
if (computer_cd == 1) {
Display("you have taken all the CD's there are.");
}
if (computer_cd == 2) {
Display("Yo. listen to what I'm saying foo. no CD.");
}
if (computer_cd == 3) {
Display("Teh Computer has nothing left to give.");
}
if (computer_cd < 3) {
computer_cd += 1;
}

} <----- declaration of the function has to be closed

function object0_a() {
// script for object0: Interact object
DisplayMessage(15);
ObjectOff(0);
GiveScore(10);
AddInventory(1);
}




Ok, about nested function error:

Nesting of functions is ability to declare functions inside of another function, example:

function main_func() {

function sub_func1() {
...
...
} //end of sub_func1

function sub_func2() {
...
...
} //end of sub_func2


} //end of main_func

So we have a main_func() and two nested: sub_func1() & sub_func2()

AGS, however, doesn't support nested functions therefore it displays the error message if you try to add some.

As the function declaration (as well as many other statements) involves the usage of braces it can lead to mistakenly declearing of nested functions, example:

function test_func(int x) {

if (x==1) {
Display("X value: 1");
}

function another_func() {

MoveCharacter(EGO, ....);

}

Here we get a nested function error as the declaration of test_func() is not closed and AGS rightly thinks that we are trying to nest the function another_func() into test_func().

To get rid of error we just have to close the first function properly:

function test_func(int x) {

if (x==1) {
Display("X value: 1");
}

} <-------------- brace added!

function another_func() {

MoveCharacter(EGO, ....);

}

Now the compiling process should go without problems.



The monologue I have adduced is to help you understand the entire problem and the direction of how such kind of scripting errors should be fixed. ;)

-Cheers

Matt Brown

okay, thanks for all of your help. quite a bit for just a little typo!

thanks guys  ;D
word up

Scorpiorus

Quotequite a bit for just a little typo!
It's because if someone else would get the similar error he can figure out what that mysterious nested error means and thereby take measures to eliminate the mistake. ;)

-Cheers

SMF spam blocked by CleanTalk