Adventure Game Studio

AGS Support => Advanced Technical Forum => Topic started by: Alynn on Thu 31/08/2006 09:17:27

Title: SUGGESTION: Constant Variables at Compile Time
Post by: Alynn on Thu 31/08/2006 09:17:27
When making my charfont module I noticed some behavior which I found rather odd.

In my module, I wanted the game to do a character count and place that number as the number of slots to use for the array used in the charfont module. I was told by AGS that I had to use a static variable.

This confused me for one reason, at compile time, character count IS static, unless there is some function out there that lets you create new characters on the fly, the last I checked, you couldn't do that.

So my suggestion is, for those functions that return variables that at run time, no longer change (pretty much any of the Count functions, since that number cannot be changed once the game compiles). There may be a few other functions that return items that will not change during the game as they can only be changed in the editor and not in the script.

Advantages: Allows module creators to make modules that use less memory, in the instance of my CharFont module, it would only allocate enough memory for the number of characters actually in the editor, instead of MAX_CHARACTERS.
Can also help those that make use of the Count functions in their game scripts.

Cons: Could cause some confusion as to what returns a compile time static, and what does not. Could be hard to implement code wise.

I think I explained what I mean decently enough...
Title: Re: SUGGESTION: Static Variables at Compile Time
Post by: SSH on Thu 31/08/2006 09:32:12
What would be better and more flexible is some kind of general managed object, so you could use dyanmic lists or hashes instead of static sized arrays...
Title: Re: SUGGESTION: Static Variables at Compile Time
Post by: monkey0506 on Fri 01/09/2006 01:20:03
Well as for the character count, that's already been implemented as the static variable:

readonly static int Game.CharacterCount

As of AGS 2.72.
Title: Re: SUGGESTION: Static Variables at Compile Time
Post by: Alynn on Fri 01/09/2006 13:05:13
Actually it isn't.

int fontNumbers[Game.CharacterCount];

Will give you Array must be a constant size value error.
Title: Re: SUGGESTION: Static Variables at Compile Time
Post by: scotch on Fri 01/09/2006 13:36:44
Difference of terminology I think. Yes, it is a static variable, but that doesn't mean it is defined at compile time, it also doesn't mean it won't change during the game. It just means it's a singular static storage variable rather than a member variable of the Game struct. (the word static makes you think of values that won't change, but actually in C any global variable is by default static).

What you want is a constant, and AGS doesn't support const on variables. For a similar effect we have to make use of #defined values. So basically what you want is for the AGS editor to automatically do some things like

#define AGS_NUMCHARACTERS x
#define AGS_NUMROOMS x

when compiling. Sounds reasonable enough... I thought it did something like this already, but I probably imagined it.

Edit: ah yes, it does the AGS_MAX_CHARACTERS type symbols, but not these ones. They'd be a handy complement I think.
Title: Re: SUGGESTION: Static Variables at Compile Time
Post by: Alynn on Fri 01/09/2006 13:48:34
Ahh coming from Java background, Static is a constant, you can't change static vars... so that's where I was coming from.

But yeah you cant even bypass it by doing a define of the charcount and trying to pass that to the array.

PS (changed the title to reflect constant instead of static)
Title: Re: SUGGESTION: Constant Variables at Compile Time
Post by: scotch on Fri 01/09/2006 14:01:51
Actually you can, the Java usage is the same as the C++. The Java version of "const" would be "final" (but we're off topic now ;)).
Title: Re: SUGGESTION: Constant Variables at Compile Time
Post by: Alynn on Fri 01/09/2006 15:09:05
Actually it's static final, so we are both wrong.... and right... but yes offtopic. I forgot that you need final after static to make it constant.. static just makes it a class variable and not an instance variable...
Title: Re: SUGGESTION: Constant Variables at Compile Time
Post by: monkey0506 on Fri 01/09/2006 18:00:20
Well that is what static means...but....enough about terminology discussion.

What I think should be done is that if the variable is readonly (such as Game.CharacterCount) then it should be accepted as a constant. I'm not sure exactly how this value is set by the engine, but it is readonly which means that it won't change (well...in addition to being readonly, you can't create characters at run-time with AGS...so...).

Which brings up the interesting point that readonly is implemented for regular variables, but const is used for function parameters (or does const only work with old-style strings? I'm not entirely clear on this)...in any case...it seems that they both essentially serve the same purpose, so, why two keywords?
Title: Re: SUGGESTION: Constant Variables at Compile Time
Post by: scotch on Fri 01/09/2006 18:29:45
It's because readonly and const are different things. The engine gives no guarantee that the readonly values will stay the same, it just makes sure that they can't be modified by external code. I don't know how many readonly variables that change there are in the built in code, but I've put them in plugins before. It's the same thing as making a "int MyClass::getCount() {return count;}" type function, without a setCount function to go with it, just less to type.

Also it's just not technically possible to allow the compiler to assume that readonly attributes are const and use them as constants in compilation, because all these built in data members and functions don't exist at compile time, they're calls into the runtime engine.
Title: Re: SUGGESTION: Constant Variables at Compile Time
Post by: monkey0506 on Fri 01/09/2006 19:25:47
Okay I suppose that makes sense...but still...we should be able to use these values as if they were constants... :D
Title: Re: SUGGESTION: Constant Variables at Compile Time
Post by: Pumaman on Tue 05/09/2006 21:47:12
The problem with adding some sort of AGS_NUM_CHARACTERS constant is that if it were used in room scripts, and then the number of characters was to change, the room script would need to be recompiled to pick up the change.

This would then mean that whenever a new character/inventory/etc was added, all rooms would have to be recompiled "just in case", which could prove rather unpopular especially when dealing with larger games.
Title: Re: SUGGESTION: Constant Variables at Compile Time
Post by: Alynn on Wed 06/09/2006 13:08:45
hrm... I was under the impression that the entire game recompiles anyway. I didn't know that the rooms don't recompile unless you save them again.

Could this be coupled with a new Save Every Damn Thing command (should probably be shortened) that recompiles everything flagged dirty or not. We have a rebuild speech and sound... could a Rebuild Rooms command be added.

Just a thought.
Title: Re: SUGGESTION: Constant Variables at Compile Time
Post by: strazer on Wed 06/09/2006 13:26:57
Quote from: Alynn on Wed 06/09/2006 13:08:45could a Rebuild Rooms command be added.

You mean like the "Rebuild all room files" command in the Game menu? ;)
Title: Re: SUGGESTION: Constant Variables at Compile Time
Post by: Alynn on Wed 06/09/2006 15:00:03
Bah... my brain working faster than my fingers again... That and my boss was coming so I had to finish my post and close my browser quick...

This sentence:
"We have a rebuild speech and sound... could a Rebuild Rooms command be added."

Should have read

"We have a rebuild speech, sound, and room... could a rebuild All command be added."