Posted: 11th Nov 2021 20:09
Not sure if considered a bug or just how the system works, but either way it's just yet another annoyance I have with how constants work in AGK.
Essentially, when you use a constant it doesn't hold any value at all but is a placeholder for whatever is assigned to it. In my function below, it doesn't see 0.2 (1280/6400), it literally inserts 1280 / 6400 into the middle of the equation, which unless I wrap RATIO_X in parenthesis, throws off the whole value.

+ Code Snippet
Global _targetZoom# = 3.0

#CONSTANT RATIO_X = 1280 / 6400.0

print(getScreenToWorldX(640))

function getScreenToWorldX(x as float)
	x = getRawMouseX() / (RATIO_X) / _targetZoom#
endfunction x


Another weird example:
If I do the following, I can't use the constant inside a function because "thing" isn't seen unless I make it global.
[code]
thing = 1280
#CONSTANT RATIO_X = thing / 6400.0



[/code]
+ Code Snippet
thing = 1280
#CONSTANT RATIO_X = thing / 6400.0



[/code]
Posted: 11th Nov 2021 20:16
Here's another problem.

#CONSTANT SCREEN_WIDTH = 1280
#CONSTANT RATIO_X = SCREEN_WIDTH / 6400.0

Any place I try to use RATIO_X tells me screen_width is being used without being defined or initialized.
Posted: 11th Nov 2021 21:14
We have been over this a few times in the last few months, it must defined with a constant static value ie, a number or a literal string, the pre-processor does not replace macros inside other macros, to get around this pipe your division through a function

+ Code Snippet
Function Div(v#)
EndFunction v#

#CONSTANT SCREEN_WIDTH = 1280
#CONSTANT RATIO_X = Div(SCREEN_WIDTH / 6400.0)


This will force the pre-processor to replace the macro
Posted: 11th Nov 2021 22:19
But then it's doing the calculation every time I use the constant. It's just annoying when the entire language passes things by value except for this one case. I just said screw it and used globals for everything, which I hate doing but at least they work as one would expect.
Posted: 11th Nov 2021 23:12
If the macro worked as intended it would also be doing the calculation every time its used, the macro is basic search and replace, the only way to not have the calculation run every time is store the result in a global
Posted: 11th Nov 2021 23:40
It will embed the equals sign as well