Constant Variables Help?

BlitzPlus Forums/BlitzPlus Beginners Area/Constant Variables Help?

SebHoll(Posted 2005) [#1]
Hi,
I want to make some of my Global variables into Constant variables because it is faster in the program. However I get and error of "Values must be constant" when I try to put the following definition in:

Const starttime = MilliSecs()
There isn't another variable with the same name and it is written right after the Graphics command. I don't know why it is coming up, anyone have any ideas?

Thanks

Seb


DH(Posted 2005) [#2]
Constant variables are just that, constant.. You can only define constants with a number, decimal, or string value. You can't define them using functions or other variables.

Besides, if your worrying about speed between global and constant, then your about as optimal as you can get.. the difference isn't going to make or break you IMO.

:-)


SebHoll(Posted 2005) [#3]
Thanks.
Why doesn't it accept function returned values?
I am a bit of a rookie at this programming stuff.
:P


Beaker(Posted 2005) [#4]
Precisely because function returned values are not constant.


SebHoll(Posted 2005) [#5]
Sorry, but what do you actually mean by "constant". Doesn't "constant" mean it stays the same, so when you return a value from a function and store it in a constant variable it stays the same. I'm confused.... E.g. if you wrote this:

Const starttime = MilliSecs()

The function "MilliSecs()" at that precise moment would return the number of MilliSeconds passed since system startup and that is assigned to the constant variable "Starttime". In that process the MilliSecs() function hasn't been requested again and thus hasn't been changed, - it's remained constant.

Any help would be much appreciated,

Thanks

Seb


DH(Posted 2005) [#6]
You are correct, the value returned by a function is constant for that moment in time, but then so is every variable for THAT moment in time...

In fact, there is no clear definition of "LIFE" at any given moment.. ANimation doesn't exist..

Anywho. Toget back to the topic at hand.

A constant variable is a var that will never change (through the course of the program). Regardless of wheere you define constant values, the compiler goes through and takes them all to the begining when compiling your code..

Directly from the manual:
This declares a variable as a constant (a variable whose value will never change and cannot be changed) and assigns the value to it.

Assign constants to values you know will not change in your game (screen width, height - scoring values - etc). This reduces the load of the variable memory, since Blitz knows it will never change size unlike other variables which can grow and shrink in size based on what value it holds.


Function return values are given during the execution of a program, whereas the const memory allocation is at the beg of the program (first thing done before the program is ran).

Basically its like going to the store and getting a shotglass, then going home and trying to dump a pail of water into the shotglass (and getting the pail of water to fit).

you have to know what your going to use it for prior to setting it up.

I could be horribly mistaken, however this is how I understand constant variables.


DH(Posted 2005) [#7]
Besides, your not really going to see too much an improvement using constant values anyhow over global or local...

Personally, I only use constants where its convienant. Like in a select case where each case can be defined as a constant var so I can use a naming convention for values rather then remembering what the values stand for. or for certian areas of the game that I know are always the same (IE gravity, bounce factors, the iq of the user, etc).

Hope this helps some.


Floyd(Posted 2005) [#8]
A Const must be a value which the compiler knows when it builds the program. This happens before the resulting exe is run, so the value can't depend on a function call like Millisecs().


SebHoll(Posted 2005) [#9]
Thanks! That's answered a lot of questions... :P