WML/Preprocessor trouble

The place to post your WML questions and answers.

Moderator: Forum Moderators

Forum rules
  • Please use [code] BBCode tags in your posts for embedding WML snippets.
  • To keep your code readable so that others can easily help you, make sure to indent it following our conventions.
Post Reply
User avatar
zookeeper
WML Wizard
Posts: 9742
Joined: September 11th, 2004, 10:40 pm
Location: Finland

WML/Preprocessor trouble

Post by zookeeper »

I'm puzzled.

I have defined this macro:

Code: Select all

#define SOMETHING_ON_DIFFICULTIES ON_EASY ON_NORMAL ON_HARD
#ifdef EASY
{ON_EASY}
#endif

#ifdef NORMAL
{ON_NORMAL} # <-- line 234
#endif

#ifdef HARD
{ON_HARD} # <-- line 238
#endif
#enddef
...and I attempt to use it, for example, like this (with and without the space after =):

Code: Select all

gold= {SOMETHING_ON_DIFFICULTIES 45 35 25}
...which works on EASY difficulty, but on NORMAL produces this:

Code: Select all

ERROR DETAILS:
Unexpected characters after variable name (expected , or =) at D:/Games/Wesnoth/userdata/data/campaigns/mycamp/scenarios/utils.cfg:234 included from D:/Games/Wesnoth/userdata/data/campaigns//mycamp.cfg:28
And the same on HARD, but with "utils.cfg:238" instead of "utils.cfg:234".

On the other hand, a macro like this behaves perfectly:

Code: Select all

#define GOLD_ON_DIFFICULTIES ON_EASY ON_NORMAL ON_HARD
#ifdef EASY
gold={ON_EASY}
#endif

#ifdef NORMAL
gold={ON_NORMAL}
#endif

#ifdef HARD
gold={ON_HARD}
#endif
#enddef
Why doesn't GOLD_ON_DIFFICULTIES produce the same errors as SOMETHING_ON_DIFFICULTIES? Looks like a syntax error of some sort - is it a bug in the preprocessor or is my WML somehow illegal? How should I write the macro to make it work?
claus
Posts: 186
Joined: April 4th, 2005, 5:51 am

Re: WML/Preprocessor trouble

Post by claus »

No idea what causes the error, but changing the definition to

Code: Select all

#define SOMETHING_ON_DIFFICULTIES ON_EASY ON_NORMAL ON_HARD
#ifdef EASY
{ON_EASY}
#else
#ifdef NORMAL
{ON_NORMAL}
#else
{ON_HARD}
#endif
#endif
#enddef
did work on my PC.
User avatar
zookeeper
WML Wizard
Posts: 9742
Joined: September 11th, 2004, 10:40 pm
Location: Finland

Post by zookeeper »

Thanks a million, that seems to work all right.
User avatar
turin
Lord of the East
Posts: 11662
Joined: January 11th, 2004, 7:17 pm
Location: Texas
Contact:

Post by turin »

In any case, there is already a game-wide macro called {QUANTITY } that works the way you want. Use it like this: {QUANTITY what_to_set_based_on_difficulty= {ON_EASY} {ON_NORMAL} {ON_HARD}}. The code used to define it is in utils.cfg, in the main data directory.

Code: Select all

#this file contains utility macros

#macro to define a 'quantity' differently based on difficulty levels
#define QUANTITY ATTRIBUTE ON_EASY ON_NORMAL ON_HARD
#ifdef EASY
{ATTRIBUTE}={ON_EASY}
#endif

#ifdef NORMAL
{ATTRIBUTE}={ON_NORMAL}
#endif

#ifdef HARD
{ATTRIBUTE}={ON_HARD}
#endif
#enddef
PS: Your WML was illegal because you had newlines between the #ifdefs (which was completely unavoidable). Claus's worked because it was all one ifdef, so the result was put on the same line. But it doesn't matter, because to make your WML more readable and reusable you should use {QUANTITY}, not a self-created macro, whenever possible.
For I am Turin Turambar - Master of Doom, by doom mastered. On permanent Wesbreak. Will not respond to private messages. Sorry!
And I hate stupid people.
The World of Orbivm
User avatar
zookeeper
WML Wizard
Posts: 9742
Joined: September 11th, 2004, 10:40 pm
Location: Finland

Post by zookeeper »

In any case, there is already a game-wide macro called {QUANTITY } that works the way you want. Use it like this: {QUANTITY what_to_set_based_on_difficulty= {ON_EASY} {ON_NORMAL} {ON_HARD}}. The code used to define it is in utils.cfg, in the main data directory.
I know, I'm just much more comfortable having the macros I use exactly the way I want them. IMHO, my way is also slightly more elegant, pretty and easier to read (well, after replacing the name SOMETHING_ON_DIFFICULTIES with BY_DIFFICULTY, anyway).
scott
Posts: 5243
Joined: May 12th, 2004, 12:35 am
Location: San Pedro, CA

Post by scott »

It's more elegant than {GOLD 1 2 3}?
Hope springs eternal.
Wesnoth acronym guide.
bruno
Inactive Developer
Posts: 293
Joined: June 26th, 2005, 8:39 pm
Contact:

Post by bruno »

I don't know that there is much you can do about this, but
it would be nice if macros for difficulty levels could
have a variable number of arguments (but fixed within
a campaign).
I am going to have 4 difficulty levels for the new TDH
(5 during testing) and I found I had to make my own
versions of these macros. I first tried redefining the
supplied ones protected by the campaign ifdef, but that
didn't work.
Post Reply