Two functions need to call each other, How?

Started by mishels, Sat 07/10/2006 14:53:51

Previous topic - Next topic

mishels

Hi,

I have a module in which a few functions call each other:

function A()
{
   ...
}

function B()
{
   ...
   C();
   ...
}

function C()
{
   ...
   A();
   ...
}

Now AGS tells me that it doesn't know function C() from the code of function B(). There is an import in the header but it hasn't stummbled upon the function itself.
I know that if I switch the places of function B and C the problem will be solved but I want to keep this order. Can I?
I know that in C or C++ I can just declare the function at the top and use it everywhere I want.
Can I do this in AGS?

And is there a way to get over this situation in AGS:

function A()
{
   ...
   B();
   ...
}

function B()
{
   ...
   A();
   ...
}


Thanks,
Mishel.

Khris

#1
A function can only be called if it was declared beforehand, i.e. its code is above in the script.
Why do you want to keep this order? For optical reasons? That's...weird, to say the least ;) You'll have to switch them.

The second situation is impossible for a good reason: As soon as you called one of the functions, your game would hang. Obviously.
Well, not if B() doesn't call A() everytime, of course, and the other way round, but it would help to get a look at the code.

mishels

Well, KhrisMUC,
when the code file gets large, optical reasons are really not that weird. I have several structs to gather my functions in groups that handle different dialogs. I don't want the functions scattered around all over the file.
This is a standard issue in any programming language so the weird things is NOT being able to do that.

I have a workaround for my specific situation, I just asked a general question, in case there is a way to define the function before actually declaring it.

I guess recursion is out of the question :-)
I know, I am spoiled.

Thanks,
Mishel.

Khris

Sorry, I didn't mean to offend you, and I know it's important to keep the code readable and organised. I had similar problems when I was writing a larger module.

Recursion is possible, I think, as long as it's
function A(...) {
Ã,  if (...) {
Ã,  Ã,  A(...);
Ã,  }
}
If you pasted B's code into A instead of calling B, it should work. Granted, that's not as elegant, but if it's the only way, why not.

DoorKnobHandle

#4
Can't you just use prototypes?

Like this:

Code: ags

// this is the prototype - the compiler will now know that function B does exist, although it doesn't yet know where it is...
function B (...);

// define function A
function A ( ... )
{
   // do stuff
   // then call B
   B ( ... );
}

// define function B
function B ( ... )
{
   // do stuff
   // then call A
   A ( ... );
}


This should work if I am not mistaken.

EDIT: Oops, I should've read your original post better, I guess. Sorry, I now see your problem, I don't know if AGS supports prototypes yet, and I am too lazy to try it out, but you should... ;)

monkey0506

You can't do that in AGS...though it would be a wonderful addition.

SilverWizard_OTF

I think there is a way to do that, well it is a bit weird but i suppose it will help you:

The problem of having a function b() {
                                      c();
                                     }
                                function c() {
                                      b();
                                      }
is that AGS won't recognize the command c();, since it is defined below. The idea is to try "confuse" the engine so it won't undrstand that trick.
   
In the room that you want this situatuion to be made, create a character e.g. EMPTY. Then to his interaction "look at character" have the script command "c();"

Now, your first script should be modified as:
function b() {
   RunCharacterInteraction(//character EMPTY, interaction "Look");
}
function c() {
   b();
}

Was this helpful?
"All we have to decide is what to do, with the time that is given to us"

mishels

Thanks for the suggestion SilverWizard, but I am using the code in a module and it will be very ugly to use a character to do that, plus i don't know if it will work.

There is an easier and a tad less ugly workaround for this that uses only the module code.

function b() {
      ... some stuff before c ...
      c();
      ... some stuff after c ...
}

function c() {
      ... some stuff before b ...
       b();
      ... some stuff after b ...
}


can be done like this:

function stuffbeforeb()
{
      ... some stuff before b ...
}

function stuffafterb()
{
     ... some stuff after b ...
}


function b() {
      ... some stuff before c ...

       stuffbeforeb(); \
       b();                }   This simulates function c()
       stuffafterb()    /

      ... some stuff after c ...
}

function c() {
       stuffbeforeb();
       b();
       stuffafterb()
}


This way it works and the code duplication is reduced to a minimum.

And I did a check and recursive functions (functions that call themseves) work just fine in AGS.

Mishel.

SMF spam blocked by CleanTalk