Posted: 17th Nov 2022 18:15
In any program , information needs to be changed and you can not change a constant value so why use a constant at all? From what I can understand there is no reason to make anything unchangable?

So I guess I am asking, is there a way to make a constant value change per level? If not there is no reason to use them for information that should be chaged and I have seen in many examples people making inportant values constans and I do not get why.

As a programer please explain this to me.

I had to change a whole example compleatly two years ago becouse I could not change a constant value, so I was thinking about this today as I saw another example lol. So I need to or want to know as much about it as I can.
Posted: 17th Nov 2022 18:30
There are a couple of reasons, off the top of my head, why constants can be better than variables:

+ Most programming languages will replace where your constant appears with the actual value during compiling for optimization. This way it doesn't need to reserve that space in memory (the stack) like a variable does.
+ It is also a great way to set a value in one place and prevent it accidentally being changed somewhere else in your code. It is sometimes too easy to change a variable value by mistake and difficult to track down the problem in thousands of lines of code.
Posted: 17th Nov 2022 18:43
Zappo

This makes better sense to me now as the explination in the help files well, does not explain the why.

Now i understand the why use them, But why would someone use them with values that makes sense to change? mabey it was just for a fast written example I am guessing.

Well In this case i need to look into how to use them where and why to have better performance.
Posted: 17th Nov 2022 19:30
since this thread, i've come to love #CONSTANT and the help file doesn't reveal its full potential.

Constants are not STATIC values (when you don't want them to be). Each time you reference them, they will return their current values.
here are some simple Constants that i tend to use a lot to demonstrate:
+ Code Snippet
#CONSTANT LeftRight (GetRawKeyState(39)-GetRawKeyState(37)) //Left/Right Keys
#CONSTANT ESC (GetRawKeyPressed(27)) //Escape Key Pressed
#CONSTANT LMState = GetRawMouseLeftState()
#CONSTANT RMState = GetRawMouseRightState()
#CONSTANT LMPressed = GetRawMouseLeftPressed()
#CONSTANT RMPressed = GetRawMouseRightPressed()
#CONSTANT LMReleased = GetRawMouseLeftReleased()
#CONSTANT RMReleased = GetRawMouseRightReleased()
#CONSTANT MWDelta = GetRawMouseWheelDelta()
#CONSTANT MX = GetPointerX()
#CONSTANT MY = GetPointerY()
#CONSTANT SHIFT = GetRawKeyState(16)

i consider them shortcuts or small functions themselves which are quite handy.

so, to check if the ESC key has been pressed during a given loop, i can simply:
+ Code Snippet
If ESC then End

same goes for retrieving current values for all the rest of the above, and i'm sure there are more elaborate ways to use them in similar fashion.

hope that helps.
Posted: 17th Nov 2022 21:46
Virtual Nomad

This makes compleate good sense now and thank you for better explination on it.

if I knew about this before I would have had better controlls in game.
Posted: 17th Nov 2022 21:47
double post?
Posted: 17th Nov 2022 23:27
another reason to use constants is the removal of static values in your code, say one of your vars or some system uses the value "12" and this is repeated 250 times in your code and you decide "15" is a better value, you have to go edit 250 places in your code, use a constant in the first place and you only ever need to adjust a single value, whenever there is a static value in my code if I cant make it dynamic I make it a const

and as VN points out, AppGameKit constants also moonlight as macros which is very very handy

also, as constants are usually used in C/C++ they can help add additional functionally to function calls, make switch statements and and flag input, a say a function needs 11 arguments, creating a window for example and 7 of those inputs are for window appearance ... put them in flags and use bit opps to filter them

and things like finite state machines become a mess unless you use constants, state's always have a static value that never changes so constants are perfect

so as you can see there are many reasons why constants are used its just knowing when and where you should use them
Posted: 17th Nov 2022 23:47
its just knowing when and where you should use them


Yes, and it is learning how and where to use them.
Posted: 20th Nov 2022 21:12
constants in agk are just literals. They make code cleaner. They also avoid entering different immediate values and make those values readable.

different examples;
+ Code Snippet
#Constant MakeVirusColor 0xFF000000 || ((C = 1) * 0x00204080) || ((C = 2) * 0x0000FF00) || ((C = 3) * 0x000000FF) || ((C = 4) * 0x0000FFFF) || ((C = 5) * 0x00FF00FF) || ((C = 6) * 0x00FFFF00) || ((C = 7) * 0x00302820)
#Constant PaintSprite (C && 255), (C >> 8) && 255, (C >> 16) && 255, 255
#Constant MaxEntities 2048

C = VirusMap[x, y].ColorIndex
C = MakeVirusColor
SetSpriteColor(VirusMap[x, y].Sprites[0], PaintSprite)
If Map[x, y].VirusIndex > MaxEntities Then ......
Posted: 20th Nov 2022 22:08
Constants make perfect sense and any pro programmer will understand that , novices won't necessarily if your using WIN API or DIRECT X for e.g. you need specific variables to be constant that don't change as they are exclusive and have exclusive meaning. that's it in a nutshell.
Posted: 20th Nov 2022 22:08
Constants make perfect sense and any pro programmer will understand that , novices won't necessarily. if your using WIN API or DIRECT X for e.g. you need specific variables to be constant that don't change as they are exclusive and have exclusive meaning. that's it in a nutshell.
Posted: 20th Nov 2022 22:12
any pro programmer will understand that


Well I am a programmer and did not understand that until it was explained to me what they are good for. I wrote many programs with out them till now becouse I know there use now lol.