Adventure Game Studio

AGS Support => Beginners' Technical Questions => Topic started by: EnterTheStory (aka tolworthy) on Wed 26/03/2008 13:24:34

Title: giving names to integers (SOLVED)
Post by: EnterTheStory (aka tolworthy) on Wed 26/03/2008 13:24:34
[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:
runCutscene(278)
runCutscene(279)


Desired:
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]
Title: Re: giving names to integers
Post by: DoorKnobHandle on Wed 26/03/2008 13:37:58
Is...


#define NUM_BIRTHDAY_SCENE 278
#define NUM_FUNERAL_SCENE  279

runCutscene ( NUM_BIRTHDAY_SCENE );
runCutscene ( NUM_FUNERAL_SCENE );


...what you're looking for?
Title: Re: giving names to integers
Post by: EnterTheStory (aka tolworthy) on Wed 26/03/2008 13:39:14
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. :)
Title: Re: giving names to integers (SOLVED)
Post by: DoorKnobHandle 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) 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:


cEgo.Transparency = 56;


Or you could use a define like this:


// 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.
Title: Re: giving names to integers (SOLVED)
Post by: EnterTheStory (aka tolworthy) on Wed 26/03/2008 13:48:16
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.
Title: Re: giving names to integers (SOLVED)
Post by: SSH on Wed 26/03/2008 13:53:07
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;)
Title: Re: giving names to integers (SOLVED)
Post by: EnterTheStory (aka tolworthy) on Wed 26/03/2008 20:32:52
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?
Title: Re: giving names to integers (SOLVED)
Post by: 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...
Title: Re: giving names to integers (SOLVED)
Post by: EnterTheStory (aka tolworthy) on Thu 27/03/2008 19:47:17
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.