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 / syntax question : pros and cons of using "="

Author
Message
Loktofeit
AGK Developer
Gold Codemaster
11
Years of Service
User Offline
Joined: 21st Jan 2009
Location: Sarasota, FL
Posted: 25th Aug 2020 12:21 Edited at: 26th Aug 2020 02:55
AGK accepts both.
Most coders here seem to use the =.
The Help pages don't use the =.

Is there any advantage or disadvantage to doing it one way or the other?


jd_zoo
2
Years of Service
User Offline
Joined: 12th May 2018
Location: Nova Scotia
Posted: 25th Aug 2020 14:26
Portability of code is probably the biggest reason to stick with "=" signs. I myself call them out luggagecode as integer = 123456 and don't use the # syntax but that is just personal preference so whatever floats your boat!
Conjured Entertainment
AGK Developer
15
Years of Service
User Offline
Joined: 12th Sep 2005
Location: Nirvana
Posted: 25th Aug 2020 15:11 Edited at: 25th Aug 2020 15:21
Quote: "Portability of code is probably the biggest reason to stick with "=" signs. "


Agreed.

I even use them for IF statements when they are not required.

My logic goes back to the old saying... "it's better to have it and not need it, than to need it and not have it."

I doubt the use of it is going to break anything, and one extra character here and there is not going to blow the file size out of proportion.

Quote: "but that is just personal preference so whatever floats your boat!"

That is what it all boils down to in the end, and using them floats my boat.

Just like using the ENDIF instead of THEN for the one liners, just in case I want to add more actions to the condition later, and it gives my formatting better consistency. ( I am a sloppy coder by habit, so I am trying to change that every way possible these days )

Coding things my way since 1981 -- Currently using AppGameKit V2 Tier 1
gosukiwi
AGK Tool Maker
User Offline
Joined: 24th May 2020
Location: Argentina
Posted: 26th Aug 2020 01:02 Edited at: 26th Aug 2020 01:03
`#constant` is not the same as defining a variable. Constants cannot change, hence the name

They are great for removing "magic values" all over code. For example, instead of:



You can do



It makes the code easier to understand, and maintain/change. Normally you'd define all constants at the very top of the file, or in it's own file.

As whether I use `=` or not, I personally don't. But AFAIK there's no difference besides adding a few more bits to the file
jd_zoo
2
Years of Service
User Offline
Joined: 12th May 2018
Location: Nova Scotia
Posted: 26th Aug 2020 03:34 Edited at: 26th Aug 2020 03:39
Quote: "They are great for removing "magic values" all over code. "


Ah ok I did not realize that, I am a bit single minded that I use a variable for my constants - your reference to "magic values" I refer to as "hard coded" and is one of my personal no-nos (for the most part), I can see the #constant being rather key for multiple people working on a project. I was thinking of the string$ syntax when I replied - thank you for that!

Edit - just looked this up and they are global? I missed the boat on that one. Noted for next time.
gosukiwi
AGK Tool Maker
User Offline
Joined: 24th May 2020
Location: Argentina
Posted: 26th Aug 2020 05:37
Funny thing, they actually have a Wikipedia page

Quote: "I can see the #constant being rather key for multiple people working on a project. "


And your future you also counts as other people! You won't remember anything if you look at something after a few months. It's good to be explicit about as many things as you can. Leave comments if something is not clear, use constants, make small functions, etc.
blink0k
Moderator
7
Years of Service
User Offline
Joined: 22nd Feb 2013
Location: the land of oz
Posted: 26th Aug 2020 06:50 Edited at: 26th Aug 2020 10:22
"You've changed!"
- Said the constant to the variable
PartTimeCoder
AGK Tool Maker
5
Years of Service
User Offline
Joined: 9th Mar 2015
Location: Suffolk UK
Posted: 26th Aug 2020 11:35
Constants are not constant in AppGameKit, another quirky TGC way of doing things but this one I actually like, you can in fact assign the return value of a function to a #Const in AppGameKit as long as any input params are constant

IE: "#Constant Key_Escape GetRawKeyPressed(27)" is valid syntax "Key_Escape" will return 1 if the key is down, 0 if not.

I was a little bemused but pleasantly supersized by this behaviour.
jd_zoo
2
Years of Service
User Offline
Joined: 12th May 2018
Location: Nova Scotia
Posted: 26th Aug 2020 16:41 Edited at: 26th Aug 2020 16:42
Quote: "And your future you also counts as other people!"


There's my philosophical quote for the day! Yes blink I've def wondered why one of my "constant variables" was getting updated somewhere only for it to be something I did...

I've had the right idea, just didn't look outside my comfort zone on this one I guess!

Attachments

Login to view attachments
blink0k
Moderator
7
Years of Service
User Offline
Joined: 22nd Feb 2013
Location: the land of oz
Posted: 26th Aug 2020 20:56
Quote: "#Constant Key_Escape GetRawKeyPressed(27)""

That's a good example because it shows that it is just a simple substitution. Like a search and replace
gosukiwi
AGK Tool Maker
User Offline
Joined: 24th May 2020
Location: Argentina
Posted: 27th Aug 2020 21:50
It seems to be done at compile-time, so it's faster than a variable assignment Not like that's gonna be a bottleneck but it's good to know.
Ortu
DBPro Master
12
Years of Service
User Offline
Joined: 21st Nov 2007
Location: Austin, TX
Posted: 29th Aug 2020 03:51
Right, #CONSTANT is basically a preprocessor macro that does a match and replace at compile time.

You can really stick any one line of code you want in there and anywhere the constant is used will be converted to and compiled with the literal text assigned to it.

They don't exist at runtime and have no inherent local/ global scope as they were simply replaced with the assigned text inline at compile.

They are super handy
http://games.joshkirklin.com/sulium

A single player RPG featuring a branching, player driven storyline of meaningful choices and multiple endings alongside challenging active combat and intelligent AI.
blink0k
Moderator
7
Years of Service
User Offline
Joined: 22nd Feb 2013
Location: the land of oz
Posted: 29th Aug 2020 04:29
Knowing that you can execute the compiler from the command line it would be cool to have a pre-processor that implemented the cpp pre-processor commands like #ifdef #else etc
Lupo4mica37
User Offline
Joined: 1st Jun 2020
Location:
Posted: 29th Aug 2020 21:07 Edited at: 29th Aug 2020 21:08
Quote: "My logic goes back to the old saying... "it's better to have it and not need it, than to need it and not have it." "


So true and very practical, it's like going to climb a mountain with a 20 kg backpack fully equiped for long term survival and discovering you are stuck on the mountain peak for a week, but then again you could have climbed it much lighter with a 10kg bag only equipped for a mere hike without any tent or sleeping bag and the nights on mountains are always cold.

Rik Vanner in his Riktris used 2 constants, one for grid x size, the other for grid y size, so I guess if he wanted the grid size to be optional by the player he would probably have defined them as variables. Also, in his Database tutorial code he used a constant for the total number of database entries.

Quote: "Funny thing, they actually have a Wikipedia page"


This is hiklarious! This place is sometimes like a comedy show.

Quote: ""You've changed!"
- Said the constant to the variable"


Quote: "IE: "#Constant Key_Escape GetRawKeyPressed(27)" is valid syntax "Key_Escape" will return 1 if the key is down, 0 if not."


Didn't quiet understand the mechanics of this part, until this explained it:

Quote: "Right, #CONSTANT is basically a preprocessor macro that does a match and replace at compile time.

You can really stick any one line of code you want in there and anywhere the constant is used will be converted to and compiled with the literal text assigned to it.

They don't exist at runtime and have no inherent local/ global scope as they were simply replaced with the assigned text inline at compile.

They are super handy"


Really good to know, that's it's not just about values, but actual code that can be used, despite I haven't tested this myself to understand it better in practice.

Quote: "Knowing that you can execute the compiler from the command line it would be cool to have a pre-processor that implemented the cpp pre-processor commands like #ifdef #else etc"


One time as I was coding something, I was trying the code to do something impossible at least for AppGameKit Basic, meaning to create a variable whilst the code is running, so from a string, convert the string into an actual variable that can store data. Something like create code whilst the code is running. Sounds crazy, but I was trying at some point for the program to be able to do that. I didn't find the function command for it to make it possible.
Ortu
DBPro Master
12
Years of Service
User Offline
Joined: 21st Nov 2007
Location: Austin, TX
Posted: 30th Aug 2020 01:00 Edited at: 30th Aug 2020 01:01
Quote: "Knowing that you can execute the compiler from the command line it would be cool to have a pre-processor that implemented the cpp pre-processor commands like #ifdef #else etc"


So funny enough, i built a custom preprocessor and build system for dbpro to do much this type of thing as well as some other convenience features. I haven't tried it but there is really no reason you couldn't point it at the agk compiler instead of the dbpro compiler and still get all the preprocessor features. It works in the same principal of calling the compiler from command line and just injects a pass through the preprocessor first.

For instance you can define environments and environment tags such as a debug build which provides the tag <? Code to include only when building against debug ?>

Great for including detailed logging, visualizations, or dev/testing controls, but have it automatically stripped out when compiling for production.
http://games.joshkirklin.com/sulium

A single player RPG featuring a branching, player driven storyline of meaningful choices and multiple endings alongside challenging active combat and intelligent AI.
blink0k
Moderator
7
Years of Service
User Offline
Joined: 22nd Feb 2013
Location: the land of oz
Posted: 30th Aug 2020 01:15
I would love to see that in AppGameKit!
Virtual Nomad
Moderator
14
Years of Service
Recently Online
Joined: 14th Dec 2005
Location: SF Bay Area, USA
Posted: 30th Aug 2020 05:04 Edited at: 30th Aug 2020 05:22
funny this topic came up where i recently found myself skipping "=", sometimes. i know i picked it up from seeing others omit it (and my tendency for laziness).

related: when i do some basic keyboard stuff like:

i might later use:

which works fine while i used to believe it wouldn't (thinking that i needed to account for all 3 potential results).

in other words, i learned that it means "if LR is anything other than 0 then...", for the most part, and until i tried:

which produces: error: IF conditional must be an integer data type

so, i can only be SO lazy, which is a good thing
[My Itch.io Home] | [#LowRezJamAGK2020]
[AGK Resource Directory] | [TGC @ GitHub]
[CODE lang=AGK] Your Code Here [/CODE] | [VIDEO=youtube] VideoID [/VIDEO]
Conjured Entertainment
AGK Developer
15
Years of Service
User Offline
Joined: 12th Sep 2005
Location: Nirvana
Posted: 30th Aug 2020 14:51 Edited at: 30th Aug 2020 14:52
Quote: "
LR = GetRawKeyState(39) - GetRawKeyState(37)"


LOL ... this is really cool ... it shows how my use of the '=' when not needed has made me completely ignore these possibilities.

I love these discussions when people are sharing ideas, because it sends me off on a new direction that I had not considered.

Coding things my way since 1981 -- Currently using AppGameKit V2 Tier 1
jd_zoo
2
Years of Service
User Offline
Joined: 12th May 2018
Location: Nova Scotia
Posted: 25th Sep 2020 01:33
I'm late on everything including this late post on this, but after discovering the use for Code Properties in Studio (I always had it off before) the #export feature will let you update data through the code properties panel in real-time. The documentation in the user guide is quite good.

After finding this post from Janbo https://forum.thegamecreators.com/thread/226122 I have not implemented on anything critical but potentially a great tool for creating simple proof-of-concepts and get things running.
Loktofeit
AGK Developer
Gold Codemaster
11
Years of Service
User Offline
Joined: 21st Jan 2009
Location: Sarasota, FL
Posted: 27th Sep 2020 20:55
So much great info from all the replies.

Login to post a reply

Server time is: 2020-10-25 17:31:12
Your offset time is: 2020-10-25 17:31:12