Globals as default input for a method.

BlitzMax Forums/BlitzMax Beginners Area/Globals as default input for a method.

SSS(Posted 2006) [#1]
Actually, never mind. It was a stupid question. Now I am left with the question of, why doesn't the compiler consider this to be a valid constant?

Const GRAPHICS_WIDTH = 320*Sqr(2)

Thanks again for any help.


tonyg(Posted 2006) [#2]
You should get an error saying...
Function defaults must be a constant
because they're set at compile time and not runtime.


tonyg(Posted 2006) [#3]
Because it doesn't know what the return from sqr(2) is going to be at compile time.


fredborg(Posted 2006) [#4]
No, you cannot do that. You can use a Const as a default input, but not a Global. Like this:
Const test = 1

Type t
	Method a(b = test)
		Print b
	EndMethod
End Type


Another alternative is to use a redundant default parameter (ie. one which you know is incorrect), and then replace it with a global. This isn't exactly nice, but it works:
Global test = 1

Type t
	Method a(b = -9999)
		If b = -9999 Then b = test
		Print b
	EndMethod
End Type
[edit] DOH!


SSS(Posted 2006) [#5]
Thanks everyone for your help. The fact that people replied to my post before I even edited it says a lot about this community. The speed of the responses is staggering. For the moment I have just inputted sqr(2) numerically into the constant. Is there a better way?


tonyg(Posted 2006) [#6]
What do you want to use it for?


SSS(Posted 2006) [#7]
There are a whole bunch of graphics functions that need to know the graphics width and graphics height. Their pretty much the only two constants that use sqr(2) in the "game" though.


tonyg(Posted 2006) [#8]
I don't think I understand.
Why can't they use the standard graphicswidth() and graphicsheight() functions?


SSS(Posted 2006) [#9]
The Graphics Width and Height constants that I'm using are used to set the windows dimensions. However, they are also used for a lot of graphics functions that may occur before the window is created. I mean, the method works, it's just slightly cumbersome.


H&K(Posted 2006) [#10]
I think the question still remains why 320*sqr(2)?
Unless Im missing something realy obvious