Define a constant on commandline for Inform 6 compiler?

Sometimes it would be very convenient if you could define a constant and set its value on the commandline. Is there a way which I just fail to find?

Nope, you correctly found nothing.

3 Likes

Thanks.

Is there a suggested way of building different versions of a game without altering the source code? Let’s say I want to build a demo version, a version for a competition and a commercial version? I can do it by defining one or more constants at the beginning of the source and using #ifdef etc to make the versions different, but that requires modifying the source between each build.

I could also define the constant(s) in an .inf file which I then include, and have my make script alter the file before each build, but that’s really a hack.

1 Like

I’d get it down to one constant defined at the top of the source, and then modify that for each build. Comment or uncomment the Constant line, probably.

One option would be inverting the include direction there - rather than having a single main file which includes a constants file made for each version, have a different root source file for each version, which defines the constants for that version, then includes the main source. This would avoid having the change the source code between builds, instead changing which source file you’re starting the compilation at.

Some quick example code to show what I mean:

MyGame-Demo.inf

Constant DEMO;
Include "MyGame";

MyGame-Commercial.inf

Constant COMMERCIAL;
Include "MyGame";

MyGame.h

#ifdef COMMERCIAL;
[ Main;
  print "This is the commercial version.";
  new_line;
];
#endif;

#ifdef DEMO;
[ Main;
  print "This is the Demo version.";
  new_line;
];
#endif;
3 Likes

So long as you are considering hacks: This rude set of hacks for the Inform 6.34 source allows switches -U1 to -U3 to automatically define constants USER_OPTION_1 through USER_OPTION_3, respectively. It shouldn’t be hard to adapt to any other version that you might be using.

634-user-option-constants.zip (1.8 KB)

2 Likes

This is a case for the seldom-used ICL (DM4 par.39), or I’m missing something here ?

Best regards from Italy,
dott. Piergiorgio.

1 Like

ICL seems to offer exactly the same options that you can do on the commandline.

I would welcome a switch to define a constant. Maybe like -AMY_CONSTANT=5

(“A” for “Assign a value to a constant”)

2 Likes

Is there more discussion on this? Do other people have a positive need for this feature?

It’s a bit messy fitting it into Inform’s argument syntax. -AMY_CONSTANT=5 would complicate the current parsing of single-dash arguments (which are all -{letter} or -{letter}{digit}.) Otis’s suggestion of -U1 through -U3 fits that scheme but it feels too limited.

1 Like

Maybe something more like the way you specify include paths then, with an initial character which isn’t used for anything else?

#MY_CONSTANT=5,YOUR_CONSTANT=7

Or $#KEY=5. Plus Unix-style --define KEY=5.

But I still feel the need for this feature is below the threshold, unless more uses for it turn up.

1 Like

It is limited. My thinking was three-fold:

  1. As someone who almost entirely lacks the skill to be tampering with the compiler code, this approach seemed safely at the shallow end of the pool.

  2. A first layer of #Ifdef in the source code can be used up front to translate the drably-named USER_OPTION_* constants into anything more to the author’s liking (e.g. #Ifdef USER_OPTION_1; Constant DEMO_VERSION; #Endif;). Should the 8 configuration states afforded by U1 to U3 not be enough, it can be expanded up to U9 with very little effort.

  3. In the absence of a proper feature in the open style suggested by fredrik, something was better than nothing.

I’d love to see the feature as requested by @fredrik to be honest. For my upcoming work, this would be very useful. I was thinking about an easy way of bundling demo-, regular and enhanced editions for supporters of a game but came to the conclusion: yes, there are ways and no, none is easy. I am also not a friend of maintaining different codebases.

Another cheesy way of doing this with the current compiler: add #include "const.h"; to your source, put different const.h files in different subdirectories, and then compile with ++subdir referring to the const.h that you want in this build.

I’m generally in favor of the proposed feature; I’ve added it to the issue tracker: https://github.com/DavidKinder/Inform6/issues/69 . But there are several possible ways to get what you want today.

2 Likes

I worked up a prototype of this that can handle up to 64 arbitrary constants (with total length up to 512 chars). I am certain that it is not up to spec for inclusion with the main code as-is, but it works.

If anyone would like a copy, let me know.

1 Like

I could see myself using this feature a lot.
This feature is included in TADS 3, where I use it quite a bit, and it would be nice to have this in Inform.
The + sign is already used for path settings, and the $ sign is already used for memory settings; I think out of all the possible syntaxes, the #MYCONSTANT,YOURCONSTANT,NUM=2 syntax would make the most sense.

There is an implementation in the source repo now, as described here: Command-line option to set a (numeric) constant · Issue #69 · DavidKinder/Inform6 · GitHub

2 Likes

Thanks, this is great news!

When is $#IDENTIFIER=NUM used and when is --define IDENTIFIER=NUM used?

Whenever you want. There’s no difference.

If both are used, which takes precedence?