Adventure Game Studio

AGS Support => Advanced Technical Forum => Topic started by: Monsieur OUXX on Thu 21/06/2007 13:21:59

Title: macros with parameters
Post by: Monsieur OUXX on Thu 21/06/2007 13:21:59
Is it possible to write MACROS with parameters in the AGS script?

For example, one can do this in C :

#define MYMACRO(param) printf("this is the parameter of my macro : %d", (param) );



Then, everytime i write "MYMACRO(12)", this will have the same effect as if i was writing "printf("...", 12)";

This would be of great help for my AGSH module (syntax would be much lighter), but if it's not available, i'll find anoterh way  ;D
Title: Re: macros with parameters
Post by: Khris on Thu 21/06/2007 13:51:17
Why don't you use a custom function?

function myfunction(int param) {
  Display("this is the paramter of my function: %d", param);
}


Now call myfunction(12);

(I might have misunderstood you though.)
Title: Re: macros with parameters
Post by: Monsieur OUXX on Thu 21/06/2007 14:08:42
Quote from: KhrisMUC on Thu 21/06/2007 13:51:17
Why don't you use a custom function?
(I might have misunderstood you though.)

What you say is totally alright with the example i gave,  BUT let's say i want this :


#define CALL_PARENT(className)   String result = MyInheritanceSystem.TellMeWhoIsYourParent( (className) );
#define IF_PARENT(potentialParentName)   if (result.CompareTo( (potentialParentName) ) == 0) {
#define ELSIF_PARENT(potentialParentName)   } else if (result.CompareTo( (potentialParentName) ) == 0) {
#define END_PARENT } else { Debug.Print("Error : cannot find this class's parent"); }


then you must admit that :

{
     ...
    CALL_PARENT("myClass")
    IF_PARENT("parent1")
           Parent1.function();
    ELSIF_PARENT("parent2")
           Parent2.function();
    END_PARENT
   ...
}


...is better than :


{
    ...
    String result = MyInheritanceSystem.TellMeWhoIsYourParent( "myClass" );
    if (result.CompareTo( "parent1" ) == 0) {
           Parent1.function();
    } else if (result.CompareTo( "parent2" ) == 0) {
           Parent2.function();
    } else {
         Debug.Print("Error : cannot find this class's parent");
    }
    ...
}




...i want my meta-language to be used by end-users... so the simplier it is, the better it is.
Title: Re: macros with parameters
Post by: Monsieur OUXX on Thu 21/06/2007 14:30:42
HAHAHAHAHA.  :D


Macros with parameters don't seem to exist in AGS,    ...BUT i've found another way to handle this :


#define WHAT_PARENT_TO_USE_FOR  String result = MyInheritanceSystem.TellMeWhoIsYourParent(

#define IF ); if (result.CompareTo(
#define THEN_CALL ) == 0) {

#define ELSE_IF  } else if (result.CompareTo(

#define IF_NONE } else { Debug.Print("Error : you forgot to handle inheritance for %s class",
#define HAS_ERROR  ); }


...then the following code works !!! :  :o


{
  ...
  WHAT_PARENT_TO_USE_FOR "myClass"
  IF       "parent1"   THEN_CALL   Parent1.function();
  ELSE_IF  "parent2"   THEN_CALL    Parent2.function();
  IF_NONE  "myClass"   HAS_ERROR
  ...
}


8)
Title: Re: macros with parameters
Post by: Pumaman on Thu 21/06/2007 19:40:47
No, you can't do macros with parameters; I dislike macros in general since they can make the code hard to debug and mistakes hard to spot, and I don't think there's the need to add support for this :)
Title: Re: macros with parameters
Post by: monkey0506 on Thu 21/06/2007 20:27:33
Perhaps a more elegant method (using macros):

#define WHAT_PARENT_TO_USE_FOR  String result = MyInheritanceSystem.TellMeWhoIsYourParent

#define IF if (result.CompareTo
#define THEN_CALL == 0)

#define ELSE_IF else if (result.CompareTo

#define ERROR "Error: you forgot to handle inheritance for class %s"
#define IF_NONE else Debug.Print


WHAT_PARENT_TO_USE_FOR("myClass");
IF ("parent1") THEN_CALL Parent1.function();
ELSE_IF ("parent2") THEN_CALL Parent2.function();
ELSE_IF ("parent3") THEN_CALL {
  Parent3.function();
  /* and stuff */
  }
IF_NONE (ERROR, "myClass");


That way things like WHAT_PARENT_TO_USE_FOR are independent from IF, THEN_CALL is independent of ELSE_IF, etc.

[EDIT:]

I knew it wasn't real code (i.e., Parent1.function? :P) I was simply showing how all these parentheses and braces could be better dealt with through macros.
Title: Re: macros with parameters
Post by: Monsieur OUXX on Thu 21/06/2007 23:17:32
Quote from: monkey_05_06 on Thu 21/06/2007 20:27:33
Perhaps a more elegant method (using macros):

It was only an example; in fact i didn't really code that; the idea was to show that sometimes macros can do more than a custom function, because you can put several instructions in a one-word-macro, and also native elements from the syntax (brackets, etc.)
Nevertheless, your approach is indeed much more elegant, i'll keep it in mind for future code!

As you can see, Chris, a workaround was found, so nobody will pester you to add this feature  ;D