Preprocessor - passing a hash character
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.
- Spannerbag
- Posts: 535
- Joined: December 18th, 2016, 6:14 pm
- Location: Yes
Preprocessor - passing a hash character
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
This works fine.
However, the contrast on
It's probably me but I haven't been able to find a list of the colour names pango supports (besides
Hence I thought I'd use the Wesnoth brightgreen rgb values: 8CFF00 but this means crafting a macro with an argument of
Wesnoth complains and the code doesn't work. I tried putting brackets round the thing (
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
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
- Pentarctagon
- Project Manager
- Posts: 5564
- Joined: March 22nd, 2009, 10:50 pm
- Location: Earth (occasionally)
Re: Preprocessor - passing a hash character
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
take one down, patch it around
-2,147,483,648 little bugs in the code
Re: Preprocessor - passing a hash character
The color name you need is probably green1. https://github.com/wesnoth/wesnoth/issues/4348
Re: Preprocessor - passing a hash character
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
Developer of: Trapped, Five Fates, Strange Legacy, Epical, UR Epic Era
Dungeonmasters of Wesnoth, Wild Peasants vs Devouring Corpses, Dwarf Dwarfson Dwarvenminer
- Spannerbag
- Posts: 535
- Joined: December 18th, 2016, 6:14 pm
- Location: Yes
Re: Preprocessor - passing a hash character
That workedRavana wrote: ↑September 26th, 2021, 10:52 am The color name you need is probably green1. https://github.com/wesnoth/wesnoth/issues/4348
Thanks!
[whinge]
Why can't I ever find stuff like that? My Google-fu's lousy...
[/whinge]
Anyway, many thanks again, much appreciated.
Cheers!
--Spannerbag
- Spannerbag
- Posts: 535
- Joined: December 18th, 2016, 6:14 pm
- Location: Yes
Re: Preprocessor - passing a hash character
Sadly it still complained about invaid markup. But thanks for the suggestion!
Am sorted now (see post replying to Ravana).
Thanks again,
cheers!
--Spannerbag
- Spannerbag
- Posts: 535
- Joined: December 18th, 2016, 6:14 pm
- Location: Yes
Re: Preprocessor - passing a hash character
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
Many thanks for taking the time to make the suggestion though, it's appreciated.
Cheers!
--Spannerbag
- Celtic_Minstrel
- Developer
- Posts: 2211
- Joined: August 3rd, 2012, 11:26 pm
- Location: Canada
- Contact:
Re: Preprocessor - passing a hash character
I think you're completely misunderstanding the problem. I'm assuming you're writing code similar to this:
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.
{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.
- Spannerbag
- Posts: 535
- Joined: December 18th, 2016, 6:14 pm
- Location: Yes
Re: Preprocessor - passing a hash character
I am trying to pass an agument to a macro that contains the hash (#) character but I can't get it to work.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 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
Code: Select all
# Turn 1
[event]
name=turn 1
{MSGCTEST yellow}
[/event]
Then I changed
yellow
to "#FFF35A"
Code: Select all
# Turn 1
[event]
name=turn 1
{MSGCTEST "#FFF35A"}
[/event]
[/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:
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]
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
- Celtic_Minstrel
- Developer
- Posts: 2211
- Joined: August 3rd, 2012, 11:26 pm
- Location: Canada
- Contact:
Re: Preprocessor - passing a hash character
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.)Spannerbag wrote: ↑October 3rd, 2021, 11:33 pm I assumed the issue is with the hash - but not so sure now
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
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
{COLOUR}
into the actual text.)- Spannerbag
- Posts: 535
- Joined: December 18th, 2016, 6:14 pm
- Location: Yes
Re: Preprocessor - passing a hash character
You assume correctlyCeltic_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.)
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
Cheers!
--Spannerbag