giving names to integers (SOLVED)

Started by EnterTheStory (aka tolworthy), Wed 26/03/2008 13:24:34

Previous topic - Next topic

EnterTheStory (aka tolworthy)

Code: ags
[code]Character names are sometimes interpreted as integers, e.g.
[code] int i =EGO; 

will compile. Is it possible to manually define other strings as equal to integer values?

E.g. now:
Code: ags

runCutscene(278)
runCutscene(279)


Desired:
Code: ags

runCutscene(EGO_ENTERS)
runCutscene(STRANGE_THINGS_HAPPEN)


The 'runScene' code would convert to an integer, and other code would convert back, so I could happily type names, but know that I could pass them to global integer arrays. I've tried workarounds - such as writing hundreds of seperate functions, or adding extra arguments, but they end up ugly and awkward. I'd prefer to just use integers with meaningful names. Is it possible?[/code][/code]

DoorKnobHandle

Is...

Code: ags

#define NUM_BIRTHDAY_SCENE 278
#define NUM_FUNERAL_SCENE  279

runCutscene ( NUM_BIRTHDAY_SCENE );
runCutscene ( NUM_FUNERAL_SCENE );


...what you're looking for?

EnterTheStory (aka tolworthy)

YES!

Can I use "define" at any place in the code, or just in a header?
Don't tell me, It's in the manual. :)

DoorKnobHandle

You can use it in both scripts and headers, just not in functions (if I remember correctly) and definitely BEFORE you're going to use it. I would advise you to place all defines at the top of the script or header so it's easy to change some variables around.

It's good practice to use them heavily, by the way. Say you were making one of your characters partially transparent, you could do this without defines like this:

Code: ags

cEgo.Transparency = 56;


Or you could use a define like this:

Code: ags

// top of the script or header
#define EGO_TRANSPARENCY 56

// in a function
cEgo.Transparency = EGO_TRANSPARENCY;


With the second way, if you find out later in development that your character is not transparent enough for example, you don't have to scroll through your old code and find that line with that "random" number 56 in it, you could easily change it by changing the EGO_TRANSPARENCY define at the top of the file. Much easier for big projects.

EnterTheStory (aka tolworthy)

Quote from: dkh on Wed 26/03/2008 13:44:42
You can use it in both scripts and headers, just not in functions (if I remember correctly)

Thanks. If it can't be used in functions then I may have to dfind another solution to my particular need - the whole point was to create aliases automatically so I can refer meaningfully to strings in global arrays. But it might be useful for later, so it's worth remembering.

Thanks again.

SSH

If you put the define in a header you can use the definition anywhere. If you put it ina script you can only use it in that script.

If you have a lot of numbers with contiguous numbering, enums might work better. Look 'em up;)
12

EnterTheStory (aka tolworthy)

Update: this thread was inspired by the help file in 2.72, which says you cannot have global arrays of strings. So I spent all day finding a way around this problem. Then discovered that the help file was really for 2.70, and in 2.72 you CAN have global arrays of Strings (witha  capital S).

Can someone confirm that this is true? I've tried it and it seems to work, but I hope I'm not doing something naughty, memory-wise.

While on the subject, how does an array of Strings actually work? I can understand an array of ints or bools, because the computer just sets aside one byte per variable. But if I declare an array of type String, and only add the Strings at runtime, how does the computer know how much space to set aside?

SSH

Yes, arrays of Strings work. And they work by magic ;)

Its actually an array of pointers to dynamically allocated areas on the heap. But that is made transparent to AGS scripters...
12

EnterTheStory (aka tolworthy)

#8
Quote from: SSH on Thu 27/03/2008 09:57:36
Yes, arrays of Strings work. And they work by magic ;)

Its actually an array of pointers to dynamically allocated areas on the heap. But that is made transparent to AGS scripters...

Ah, magic. Now it all makes sense. I am guessing that these "pointers" you speak of are pointing sticks, the "heap" is the heap of entrails, and "transparent" refers to the fact that only the initiated can see these invisible spirits. Obviously Pumaman is a shaman (story teller whose avatar goes on spirit adventures) who takes on the form of a puma, thus forcing the spirits of the computer to obey him. I always suspected it would be something like that.

SMF spam blocked by CleanTalk