Adventure Game Studio

AGS Support => Beginners' Technical Questions => Topic started by: SilverWizard_OTF on Wed 01/09/2004 20:31:45

Title: Already referenced...?
Post by: SilverWizard_OTF on Wed 01/09/2004 20:31:45
Hello
My problem is a bit strange.

I have created some custom function and i have put them to the top of my Global Script. Well:
  function do_something() {
     //some code
     do_this();
     }

Do_this is an other custom function that i created. Well, when i do something like this (when i running a custom function via an other custom function), AGS engine gives this error: Error, the problem was in Main Script: Already referenced name as import.

Thanks for your help.

EDIT: Well, there is more! Now sometimes it gives an error when i create custom functions and tells the same things! It is a bit  madding
Title: Re: Already referenced...?
Post by: Pumaman on Wed 01/09/2004 20:59:23
You have to declare a function before you use it.

Therefore, you cannot have this:

do_func();

in the script before this:

function do_func() {
// function code
}

Check the ordering of your functions.
Title: Re: Already referenced...?
Post by: SilverWizard_OTF on Thu 02/09/2004 14:06:48
Yes, but:

I want to do something like this:

function do_this();
  //somecode
  do_that();                     //a custom function

function do_that();
   //some code
   do_this();

I want to do this for to create a "cycle". What shall i do for it?
Title: Re: Already referenced...?
Post by: TerranRich on Thu 02/09/2004 14:32:41
I don't believe you can even do that. What exactly are you trying to do anyway? Maybe there is a workaround.
Title: Re: Already referenced...?
Post by: Ashen on Thu 02/09/2004 17:21:55
For a workaround, you could try:

// main global script file

function do_this(); {
  //somecode
  RunCharacterInteraction (BLANK, MODE_USE);
}

function do_that(); {
   //some code
   do_this();
}



then import do_that() as a global function (if it isn't already), create a dummy character (BLANK), and have do_that() in its USE interaction. Then, when you call either of them, itt'l start the loop.
Since do_that() is called from outside the global script, it doesn't matter where you put it, so long as it's after do_this().
Title: Re: Already referenced...?
Post by: SilverWizard_OTF on Thu 02/09/2004 20:07:03
Well, here is a problem which ONLY a "Go to" script command could solve!

function do_this();
  //somecode
  do_that();                     //a custom function

function do_that();
   //some code
   do_this();

E.g. do_this is a function where Player1 do certain things.
E.g. do_that is a function where Player2 do some other things.
  Then, game returns to Player's1 function.

Something like that i wanted to do.
Personally, i think that problem is a kind of bug.
   Engine should scan the WHOLE script, before gives error, or at least the script header first.

Title: Re: Already referenced...?
Post by: Pumaman on Thu 02/09/2004 21:52:06
Sounds like you'd get yourself into a recursive loop like that.

Why not have something like:

function do_all() {
  do_this();
  do_that();
}

I can't see a situation in which two functions need to call each other, there's usually a better way of doing it.
Title: Re: Already referenced...?
Post by: Snarky on Fri 03/09/2004 03:29:01
Actually, there are many cases where it makes sense to write mutually recursive functions. It often provides very elegant solutions to tricky problems.

The question of which function is first in the code is usually (and by usually I mean in C and C++) solved not with GOTO, but by splitting up the declaration and the definition of the functions, like this:

function do_this();
function do_that();

function do_this() {
Ã,  Ã, // some code...
Ã,  Ã, do_that();
}

function do_that() {
Ã,  Ã, // some other code...
Ã,  Ã, do_this();
}

That way, the compiler knows about all the functions in advance, and you can let them reference each other in any pattern you like. AGS doesn't allow this, though (at least not with the syntax I tried). In my opinion, it should. Or, as SilverWizard suggested, it should scan the entire script first so that the order doesn't matter.
Title: Re: Already referenced...?
Post by: SilverWizard_OTF on Fri 03/09/2004 13:06:40
Well, i think Snarky has right. AGS engine should allow that trick.