Creative uses for #define

Talk about making games.

Creative uses for #define

Postby Fuzzy » Sat Apr 22, 2006 7:03 pm

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!
Mortal Enemy of IF....THEN(and Inspector Gadget)

Still ThreeFingerPete to tekdino
User avatar
Fuzzy
 
Posts: 1068
Joined: Thu Mar 03, 2005 9:32 am
Location: Plymostic Programmer
Score: 95 Give a positive score

Postby DilloDude » Sat Apr 22, 2006 11:22 pm

So putting a formula int #define is better than using a function that returns it?
Image
User avatar
DilloDude
 
Posts: 866
Joined: Tue Jan 24, 2006 9:51 am
Location: Nyerellion
Score: 58 Give a positive score

Postby Fuzzy » Sun Apr 23, 2006 4:25 am

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.
Mortal Enemy of IF....THEN(and Inspector Gadget)

Still ThreeFingerPete to tekdino
User avatar
Fuzzy
 
Posts: 1068
Joined: Thu Mar 03, 2005 9:32 am
Location: Plymostic Programmer
Score: 95 Give a positive score

Postby WauloK » Sun Apr 23, 2006 6:53 am

in the compiled code, the formula will appear. it's just for your own convenience.
Available for Beta-testing.. PM me!
Black 16Gb iPhone 3GS
Windows 7: 2Gb RAM. Dell 9150.
WauloK
 
Posts: 42
Joined: Wed Apr 19, 2006 3:19 am
Location: Sydney, Aussieland
Score: 0 Give a positive score

Postby Mardi-Gras » Sun Apr 23, 2006 12:47 pm

#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.
User avatar
Mardi-Gras
 
Posts: 57
Joined: Sun Jan 22, 2006 10:10 pm
Location: Glenrothes, Scotland
Score: 0 Give a positive score

Postby willg101 » Sun Apr 23, 2006 11:42 pm

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!
http://www.wish-games.com
Old betas now available!!
User avatar
willg101
 
Posts: 473
Joined: Thu Dec 16, 2004 12:08 am
Location: West Michigan
Score: 5 Give a positive score

Postby Fuzzy » Tue Apr 25, 2006 7:21 am

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!
Mortal Enemy of IF....THEN(and Inspector Gadget)

Still ThreeFingerPete to tekdino
User avatar
Fuzzy
 
Posts: 1068
Joined: Thu Mar 03, 2005 9:32 am
Location: Plymostic Programmer
Score: 95 Give a positive score


Return to Game Development

Who is online

Users browsing this forum: No registered users and 1 guest