ID:155525
 
I have two preprocessor macros defined for the size of the client's view area, VIEW_WIDTH and VIEW_HEIGHT. I'm trying to set world.view to a value determined by those values, but world.view must be a constant expression. Is there any way to make a text string, using preprocessor macros, that would be considered a constant expression? This is basically what I'm trying to do:
#define VIEW_WIDTH 15
#define VIEW_HEIGHT 13

world{
view = "[VIEW_WIDTH]x[VIEW_HEIGHT]"
}


I tried making a preprocessor function (whatever they're called) that would accept those two values and create the appropriate string, but preprocessor magic isn't my area. Any ideas?
This works for me, but probably isn't entirely what you were looking for...

#define MOO "1x19"
world/view = MOO
In response to Murrawhip
That doesn't help at all. I need to have numbers for both width and height for use in other calculations.
In response to IainPeregrine
All I can manage is this:

#define moo(WIDTH,HEIGHT) ((WIDTH) + "x" + (HEIGHT))
#define WIDTH "2"
#define HEIGHT "10"

world/view = moo(WIDTH,HEIGHT)


Not sure if it's possible to combine integers into a string inside a macro.
In response to Murrawhip
Murrawhip wrote:
Not sure if it's possible to combine integers into a string inside a macro.

It is not. The code you provided will not compile.

When you answer a question in Dev How-To, other forum browsers see a response and think that the problem is solved, especially when there's a long back and forth, so they ignore it. I appreciate you trying to help, but when you answer a question with a non-working solution, you're hurting the original poster's chance of getting a working answer.
Short of some undocumented voodoo (there may be some), there doesn't seem to be any way to use the preprocessor to place the numbers in quotes without embedding them (which it considers non-constant).

As I'm sure you've already considered, the simplest workaround would be to just have two versions of the macros, one for text and one for numbers:

#define VIEW_WIDTH 15
#define VIEW_HEIGHT 13
#define VIEW_WIDTH_TEXT "15"
#define VIEW_HEIGHT_TEXT "13"

#define GET_VIEW (VIEW_WIDTH_TEXT+"x"+VIEW_HEIGHT_TEXT)

world/view = GET_VIEW


It shouldn't be too difficult to keep VIEW_* and VIEW_*_TEXT synced.
In response to IainPeregrine
IainPeregrine wrote:
Murrawhip wrote:
Not sure if it's possible to combine integers into a string inside a macro.

It is not. The code you provided will not compile.

When you answer a question in Dev How-To, other forum browsers see a response and think that the problem is solved, especially when there's a long back and forth, so they ignore it. I appreciate you trying to help, but when you answer a question with a non-working solution, you're hurting the original poster's chance of getting a working answer.

When you post a question that isn't very specific in what you actually want to accomplish, you're going to have trouble getting the exact answer you want. Both of my solutions accomplish your vaguely described issue so perhaps instead of relying on people reading your mind when they attempt to help you, put more effort into your initial post.

P.S: Compiles just fine on my end.
In response to IainPeregrine
Murrawhip's example compiles and executes correctly.
If you don't want to use a preprocessor function like Murrawhip's "moo" example or you never need to use that concatenated string again you could alternatively use"

world/view = VIEW_WIDTH + "x" + VIEW_HEIGHT
In response to Cody123100
You are both correct that the example provided compiles. Where you are both wrong is that it is a solution. I need VIEW_WIDTH and VIEW_HEIGHT to be numbers, not strings.
BYOND preprocessor lacks directives, as for example Visual Studio one can turn numbers/variables into string. DarkCampainger provided good solution to your problem.

Alternatively you could set world.view to some high value, then set client/view = "[VIEW_WIDTH]x[VIEW_HEIGHT]", it doesn't have to be constant unlike world.view does.
In response to IainPeregrine
IainPeregrine wrote:
I need VIEW_WIDTH and VIEW_HEIGHT to be numbers, not strings.

Then specify that instead of running around posting that people are "wrong". It's rude, and not appreciated at all.