Preprocessor - passing a hash character

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
Spannerbag
Posts: 505
Joined: December 18th, 2016, 6:14 pm
Location: Yes

Preprocessor - passing a hash character

Post by Spannerbag »

Hi,
is it possible to have a macro argument that contains the hash (#) character?

I have a macro that generates a message that uses pango <font color='{MACRO_ARG}'> where {MACRO_ARG} is red, green etc.
This works fine.

However, the contrast on green text isn't great so to improve readability I'd like to use a brighter shade.
It's probably me but I haven't been able to find a list of the colour names pango supports (besides red, green, blue, yellow and white).
Hence I thought I'd use the Wesnoth brightgreen rgb values: 8CFF00 but this means crafting a macro with an argument of #8CFF00.
Wesnoth complains and the code doesn't work. I tried putting brackets round the thing (#8CFF00) but that had no effect.
I can work around this by not using a macro and simply typing in the WML direct but I just thought I'd ask the question.

So, how do I either:
a) tell the preprocessor that the hash is data and not a command identifier?
b) tell pango what rgb/colour values I want some other way?

Like I say I can workaround, I'm just curious :)

Any enlightenment gratefully received!

Cheers!
--Spannerbag
SP Campaigns: After EI (v1.14) Leafsea Burning (v1.17, v1.16)
I suspect the universe is simpler than we think and stranger than we can know.
Also, I fear that beyond a certain point more intelligence does not necessarily benefit a species...
User avatar
Pentarctagon
Project Manager
Posts: 5526
Joined: March 22nd, 2009, 10:50 pm
Location: Earth (occasionally)

Re: Preprocessor - passing a hash character

Post by Pentarctagon »

Try <<#8CFF00>>.
99 little bugs in the code, 99 little bugs
take one down, patch it around
-2,147,483,648 little bugs in the code
User avatar
Ravana
Forum Moderator
Posts: 2949
Joined: January 29th, 2012, 12:49 am
Location: Estonia
Contact:

Re: Preprocessor - passing a hash character

Post by Ravana »

The color name you need is probably green1. https://github.com/wesnoth/wesnoth/issues/4348
User avatar
Heindal
Posts: 1344
Joined: August 11th, 2011, 9:25 pm
Location: Germany, Karlsruhe
Contact:

Re: Preprocessor - passing a hash character

Post by Heindal »

You could use something like this: <font color='#{MACRO_ARG}'>.
The future belongs to those, who believe in the beauty of their dreams.
Developer of: Trapped, Five Fates, Strange Legacy, Epical, UR Epic Era
Dungeonmasters of Wesnoth, Wild Peasants vs Devouring Corpses, Dwarf Dwarfson Dwarvenminer
User avatar
Spannerbag
Posts: 505
Joined: December 18th, 2016, 6:14 pm
Location: Yes

Re: Preprocessor - passing a hash character

Post by Spannerbag »

Ravana wrote: September 26th, 2021, 10:52 am The color name you need is probably green1. https://github.com/wesnoth/wesnoth/issues/4348
That worked :D
Thanks!

[whinge]
Why can't I ever find stuff like that? My Google-fu's lousy... :annoyed:
[/whinge]

Anyway, many thanks again, much appreciated.

Cheers!
--Spannerbag
SP Campaigns: After EI (v1.14) Leafsea Burning (v1.17, v1.16)
I suspect the universe is simpler than we think and stranger than we can know.
Also, I fear that beyond a certain point more intelligence does not necessarily benefit a species...
User avatar
Spannerbag
Posts: 505
Joined: December 18th, 2016, 6:14 pm
Location: Yes

Re: Preprocessor - passing a hash character

Post by Spannerbag »

Pentarctagon wrote: September 26th, 2021, 1:33 am Try <<#8CFF00>>.
Sadly it still complained about invaid markup. But thanks for the suggestion!
Am sorted now (see post replying to Ravana).
Thanks again,
cheers!
--Spannerbag
SP Campaigns: After EI (v1.14) Leafsea Burning (v1.17, v1.16)
I suspect the universe is simpler than we think and stranger than we can know.
Also, I fear that beyond a certain point more intelligence does not necessarily benefit a species...
User avatar
Spannerbag
Posts: 505
Joined: December 18th, 2016, 6:14 pm
Location: Yes

Re: Preprocessor - passing a hash character

Post by Spannerbag »

Heindal wrote: September 26th, 2021, 11:33 am You could use something like this: <font color='#{MACRO_ARG}'>.
Thanks for the suggestion - and sorry, wasn't fully clear but I still use colour names in other places (I use white in one place and yellow in another it was just the rather deep shade of green that didn't work too well on-screen).

I did consider that but (assuming it worked) I'd need separate macros because if I passed red as the colour arg. it would become #red in the macro...

Happily got sorted and am using green1 now :D

Many thanks for taking the time to make the suggestion though, it's appreciated.

Cheers!
--Spannerbag
SP Campaigns: After EI (v1.14) Leafsea Burning (v1.17, v1.16)
I suspect the universe is simpler than we think and stranger than we can know.
Also, I fear that beyond a certain point more intelligence does not necessarily benefit a species...
User avatar
Celtic_Minstrel
Developer
Posts: 2166
Joined: August 3rd, 2012, 11:26 pm
Location: Canada
Contact:

Re: Preprocessor - passing a hash character

Post by Celtic_Minstrel »

I think you're completely misunderstanding the problem. I'm assuming you're writing code similar to this: {SOME_MACRO #8cff00}. That won't work because # starts a comment. The solution is just to put it in quotes: {SOME_MACRO "#8cff00"}. Or am I misunderstanding what you were doing?

I understand that you've solved it by using green1 instead, but I'd like to know exactly what you were trying that failed, just in case there's an actual bug here somewhere.
Author of The Black Cross of Aleron campaign and Default++ era.
Former maintainer of Steelhive.
User avatar
Spannerbag
Posts: 505
Joined: December 18th, 2016, 6:14 pm
Location: Yes

Re: Preprocessor - passing a hash character

Post by Spannerbag »

Celtic_Minstrel wrote: October 3rd, 2021, 4:43 pm I think you're completely misunderstanding the problem. I'm assuming you're writing code similar to this: {SOME_MACRO #8cff00}. That won't work because # starts a comment. The solution is just to put it in quotes: {SOME_MACRO "#8cff00"}. Or am I misunderstanding what you were doing?

I understand that you've solved it by using green1 instead, but I'd like to know exactly what you were trying that failed, just in case there's an actual bug here somewhere.
I am trying to pass an agument to a macro that contains the hash (#) character but I can't get it to work.
I assumed the issue is with the hash - but not so sure now :?
Probably easiest if I show you my latest test code.

I fired up the old stub campaign (a minimalist campaign I use for testing) and put the followng code into scenario 1:

Code: Select all

#define MSGCTEST COLOUR
[message]
  speaker=narrator
  image=wesnoth-icon.png
  message= _ "<span color='{COLOUR}'>This message is in colour={COLOUR}</span>"
[/message]
#enddef
Then later on I added:

Code: Select all

# Turn 1
  [event]
    name=turn 1
{MSGCTEST yellow}
  [/event]
This worked as expected:
msg-yellow-OK.png
msg-yellow-OK.png (8.65 KiB) Viewed 516 times

Then I changed yellow to "#FFF35A"

Code: Select all

# Turn 1
  [event]
    name=turn 1
{MSGCTEST "#FFF35A"}
  [/event]
This produced an error on line 100 ([/event]):
Unexpected characters at line start
at ~add-ons/stub/./scenarios/01_stub.cfg:100
included from ~add-ons/stub/_main.cfg:62
(I suspect we have a double quotes conflict here maybe?)

Putting the thing in brackets made no difference (same error)

Code: Select all

# Turn 1
  [event]
    name=turn 1
{MSGCTEST ("#FFF35A")}
  [/event]

Using angle brackets at least allowed the scenario to load but gave an in-game error:
msg-bad-01.png

As a sanity check I hard coded the colour thus:

Code: Select all

# Turn 1
  [event]
    name=turn 1
[message]
  speaker=narrator
  image=wesnoth-icon.png
  message= _ "<span color='#FFF35A'>This message is in colour=#FFF35A</span>"
[/message]
  [/event]
Which worked as expected:
msg-gold-OK.png
msg-gold-OK.png (10.23 KiB) Viewed 516 times


There are an awful lot of permutations here so if there are any you'd like me to try, please let me know.

Oh, and I upgraded from 1.15.17 to 1.15.18 and now the add-ons server doesn't work? (Remote host disconnected)
Also tried add-ons.wesnoth.org/1.15 and add-ons.wesnoth.org/1.15/ - both gave an invalid address error.
Is this just me, or is something broken?

Cheers,
--Spannerbag
SP Campaigns: After EI (v1.14) Leafsea Burning (v1.17, v1.16)
I suspect the universe is simpler than we think and stranger than we can know.
Also, I fear that beyond a certain point more intelligence does not necessarily benefit a species...
User avatar
Celtic_Minstrel
Developer
Posts: 2166
Joined: August 3rd, 2012, 11:26 pm
Location: Canada
Contact:

Re: Preprocessor - passing a hash character

Post by Celtic_Minstrel »

Spannerbag wrote: October 3rd, 2021, 11:33 pm I assumed the issue is with the hash - but not so sure now :?
Hash is a comment character, so your assumption is quite reasonable. Seeing your code, I can now see why it becomes an issue and why quoting the hash doesn't work. (I'm not exactly sure how quoting the hash leads to the error it leads to though.)

As you guessed, it's a double-quotes conflict. Quoting the hash fixes it from being interpreted as a comment, but when it's substituted into a double-quoted string, the opening quote (if I understand correctly) becomes a closing quote for the containing string, which likely means it's interpreted as a comment once again, although as I said I'm not sure how that leads to "Unexpected characters at line start".

I think this isn't a bug, exactly, just an unfortunate side-effect of how the preprocessor works.

I think the only way to fix it is to use string concatenation:

Code: Select all

#define MSGCTEST COLOUR
[message]
  speaker=narrator
  image=wesnoth-icon.png
  message= _ "<span color='" + {COLOUR} + "'>This message is in colour=" + {COLOUR} + "</span>"
[/message]
#enddef
It's a bit awkward that way, but it should work for arbitrary colours. For translation purposes you'd probably want to go a bit further though and split the actual text into a standalone translatable string:

Code: Select all

#define MSGCTEST COLOUR
[message]
  speaker=narrator
  image=wesnoth-icon.png
  message= "<span color='" + {COLOUR} + "'>" + _ "This message is in colour=" + {COLOUR} + "</span>"
[/message]
#enddef
(I assume the real code wouldn't be substituting {COLOUR} into the actual text.)
Author of The Black Cross of Aleron campaign and Default++ era.
Former maintainer of Steelhive.
User avatar
Spannerbag
Posts: 505
Joined: December 18th, 2016, 6:14 pm
Location: Yes

Re: Preprocessor - passing a hash character

Post by Spannerbag »

Celtic_Minstrel wrote: October 6th, 2021, 1:40 pm ...I think the only way to fix it is to use string concatenation...
For translation purposes you'd probably want to ... split the actual text into a standalone translatable string...

(I assume the real code wouldn't be substituting {COLOUR} into the actual text.)
You assume correctly :)
I'll use your idea if/when needed in the future.
Thanks for looking into this and taking the time and trouble to provide a fix/explanantion, much appreciated :D

Cheers!
--Spannerbag
SP Campaigns: After EI (v1.14) Leafsea Burning (v1.17, v1.16)
I suspect the universe is simpler than we think and stranger than we can know.
Also, I fear that beyond a certain point more intelligence does not necessarily benefit a species...
Post Reply