# Defining constants/avoiding magic numbers?

In Very Vile Fairy File I have a table where one column is a number. It acts as a sort of database of good guesses by the programmer. After some programming, I realized in some cases that number wasn’t always right, and it would be too hard to change it via normal programming methods. Here is an example

``````table of good rhyme guesses
user-input-topic	expected-number	rule-for-valid
"flake flee"	43	is-jake-g-present rule [this indicates that the desired command is 4 letters, then 3. Yes, I could use 2 columns for this, but see below...]
``````

Now, the commands to get Jake moving change, and they change length, So what I did was to change expected-number to 100, then have

``````to decide which number is actual-word-length of (n - a number):
if n is 100:
if jake is sleepy, decide on 44;
if jake is awake, decide on 43;
if jake is fed, decide on 43;
if jake is paid, decide on -54; [this is some more magic number trickery. It means the command is an LLP] [more examples but I'll cut it off here
decide on 0; [give no hints]
say "Uh oh, I didn't find a desired word length for this good guess.";
decide on -1; [hint error]
``````

Now, all this works, and it’s been tested. And I don’t have too many cases to keep track of, so magic numbers aren’t a huge deal. But I was wondering how I could track them in general. If I were programming in C or its variants, for instance, I would say

#define JAKE_G_BRANCH 100
#define GOLD_GAOL_BRANCH 101
#define STARK_STUMP_BRANCH 102

And this would make the code more readable.

Is there an easy way to do this in Inform 7 (or do stuff with I6 that would work in I7) so I could put these constants in a table?

Thanks!

``````Jake-g-branch is always 100.
Gold-gaol-branch is always 101.
Stark-stump-branch is always 102.
``````

But is there a reason to use, in C-speak, #defines rather than an enum?

No there isn’t. I just wanted to put all the information out there people might need.

Looks like this code doesn’t work in 6E59, which I’m using, but it makes a whole lot of sense. It’s good to know it’s in Inform going forward. The error below:

``````Problem. You wrote 'jake-g-branch is always 100'  : again, this seems to say that a thing is a value.
``````

BTW, when you say enum, is that what your code above is doing? Or is it something else I’m missing? Thanks!

Oh wow, I didn’t know anyone still used 6E59 at this point! Yeah, the “is always” syntax postdates that.

Enums are older, and should work fine in the 6Es:

``````A branch is a kind of value. The branches are Jake-branch, gold-gaol-branch, and stark-stump-branch.
``````

Basically equivalent to (in C):

``typedef enum { Jake_branch, gold_gaol_branch, stark_stump_branch } branch;``
1 Like

mmmm…

A pair of celebrated console role-playing games of the 90s marked the game story & narration progession thru a “master variable”, increased after every major and minor story progession, and I’m actually mulling about the usage of a master variable(s) instead of flags (or, better for the sake of non-linearity, a mix of master variables and flags) in IF.

In this context, having named the value of a master variable should help immensely (in the said celebrated console RPGs there was unreachable minor events because the master variable jumped above the value associated to those minor events…)

my 2 €c, and

Best regards from Italy,
dott. Piergiorgio.

1 Like

Oh, of course! I’ve used enums. I just never thought about them as enums.

I suppose it’s up to me to see if I want to make a new column for enums, since I wouldn’t be able to mix enums and actual numbers.

@Piergiorgio_d_errico the idea of a master variable seems nice, but I guess it needs maintenance in its own way, in case the story progressed in ways the developer didn’t expect. This is one of those things I’d like to get right, but I don’t want to beat myself up over making it perfect, because that’s draining.

Generally, I would recommend the use of scenes, rather than progress flags (or counters). They’re a better fit for this sort of thing in the IF context, and I7 comes out of the box running them and providing tools to troubleshoot them.

1 Like

Scenes are handy, but they’re a high-level mechanism and are intended for a particular use model. (Most obviously, you can never start a scene directly; you can only set conditions which cause a scene to start between turns.)

If scenes fit what you’re doing, great. If not, you should be comfortable with dropping in some variables and doing what you need by hand.

3 Likes