Page 1 of 1

Creative uses for #define

PostPosted: Sat Apr 22, 2006 7:03 pm
by Fuzzy
First off, #define is a special keyword used in Global code only.

Define is a way to substitute aa string of characters everytime a word appears. In this regard, it is somewhat like a variable, and can be used as one. However, it is much more versatile than a variable.

To use #define, you go to the global code script, and type #define followed by a unique name, much as you would name a variable. Immediately after the name, you put the text that will be substituted every time GE sees the unique name.

You do not normally use a = sign immediately after the unique name. You do not normally use " marks around the text either. Note that you CAN use these. You can use any characters, but they are going to be inserted in the code, so be wary, you can cause errors. Also, dont end the line with ;

#define MAXINT 2147483647

Now say you are making a cheat code, and want to give the player the maximum amount of gold. Just use
player.gold = MAXINT;

That seems to be just like a variable, right? well, its not. Lets look at another example.

say you have this in your players code...

ChangeAnimationDirection("Event Actor", FORWARD);

Now thats kind of long, eh? Good thing makslane lets use select it from a menu, and has GE fill it in. A lot of people dont like how the edit window is limited in size. So we can do this:

#define CAD ChangeAnimationDirection

and write this into the players code....
CAD("Event Actor", FORWARD);

Lets shrink it a little further... Event Actor appears all over in scripts...

#define EA "Event Actor"

note the "" marks. You need them here.
CAD(EA, FORWARD);

See, Makslane used FORWARD as a define. Its good practice to use ALLCAPS for letters in define keywords. Still, lets reduce it further.

#define FORWARD FWD

and our final line becomes...

CAD(EA,FWD);
compared to the original...
ChangeAnimationDirection("Event Actor", FORWARD);

Its about 1/3 as long, and still somewhat readable. It saves script window line space and program size, if you substitute words that are used lots, like "Event Actor".

So thats pretty cool, I think. Still, there is more that you can do..

#define INVISIBILITY power*skill-rainlevel-noise+blindness

Note the lack of =.

Now everywhere you use INVISIBILTY, the formula gets inserted and calculated. Lets see a variable do THAT!

Please share with us other uses you have found for #define!

PostPosted: Sat Apr 22, 2006 11:22 pm
by DilloDude
So putting a formula int #define is better than using a function that returns it?

PostPosted: Sun Apr 23, 2006 4:25 am
by Fuzzy
sometimes its better to use #define. In other cases, it could make your code longer and slower. if you need to do an if statement, or while, use a function.

remember that its going to expand, and that you only get one line for the define.

I'd say try both and learn which works better.

PostPosted: Sun Apr 23, 2006 6:53 am
by WauloK
in the compiled code, the formula will appear. it's just for your own convenience.

PostPosted: Sun Apr 23, 2006 12:47 pm
by Mardi-Gras
#define INVISIBILITY power*skill-rainlevel-noise+blindness

Note the lack of =.

Now everywhere you use INVISIBILTY, the formula gets inserted and calculated. Lets see a variable do THAT!


I did not know that. That's remarkably useful. Thanks for sharing that, nice topic.

PostPosted: Sun Apr 23, 2006 11:42 pm
by willg101
Yeah, that is helpful. A few months ago I tried to do that with variables, which did not turn out so hot. But this looks like it should be just what I need! Thanks, ThreeFingerPete!

PostPosted: Tue Apr 25, 2006 7:21 am
by Fuzzy
If you use #defines in formulas, please be careful that you dont cause excessive recursion. An example would be...

#declare SOMETHING this+that+SOMETHING


you'll make an infinite string of this+that+this+that.... at least until your computer runs out of memory!