Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

AppGameKit Classic Chat / [SOLVED] constants make no sense

Author
Message
Game_Code_here
4
Years of Service
User Offline
Joined: 2nd Jun 2020
Location:
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.

The author of this post has marked a post as an answer.

Go to answer

Zappo
Valued Member
20
Years of Service
User Offline
Joined: 27th Oct 2004
Location: In the post
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.
Game_Code_here
4
Years of Service
User Offline
Joined: 2nd Jun 2020
Location:
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.
Virtual Nomad
Moderator
18
Years of Service
User Offline
Joined: 14th Dec 2005
Location: SF Bay Area, USA
Posted: 17th Nov 2022 19:30 Edited at: 17th Nov 2022 19:40
This post has been marked by the post author as the answer.
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:

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:

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.
Game_Code_here
4
Years of Service
User Offline
Joined: 2nd Jun 2020
Location:
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.

Game_Code_here
4
Years of Service
User Offline
Joined: 2nd Jun 2020
Location:
Posted: 17th Nov 2022 21:47 Edited at: 17th Nov 2022 21:48
double post?
PartTimeCoder
AGK Tool Maker
9
Years of Service
User Offline
Joined: 9th Mar 2015
Location: London UK
Posted: 17th Nov 2022 23:27 Edited at: 17th Nov 2022 23:39
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
Open Source plugins
Cl - DnD Plugin
Buy Me A Coffee
Game_Code_here
4
Years of Service
User Offline
Joined: 2nd Jun 2020
Location:
Posted: 17th Nov 2022 23:47
Quote: " its just knowing when and where you should use them"


Yes, and it is learning how and where to use them.
Arch-Ok
AGK Developer
5
Years of Service
User Offline
Joined: 11th Jul 2019
Location: Bursa/TÜRKIYE
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;
Bored of the Rings
User Banned
Posted: 20th Nov 2022 22:08 Edited at: 20th Nov 2022 22:09
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.
Pro Programmer / Data Scientist, languages: SAS, C++, SQL, PL-SQL, DBPro, Purebasic, JavaScript, others
Bored of the Rings
User Banned
Posted: 20th Nov 2022 22:08 Edited at: 20th Nov 2022 22:09
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.
Pro Programmer / Data Scientist, languages: SAS, C++, SQL, PL-SQL, DBPro, Purebasic, JavaScript, others
Game_Code_here
4
Years of Service
User Offline
Joined: 2nd Jun 2020
Location:
Posted: 20th Nov 2022 22:12
Quote: "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.

Login to post a reply

Server time is: 2024-11-21 13:15:33
Your offset time is: 2024-11-21 13:15:33