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 / #Constant (misuse?)

Author
Message
Virtual Nomad
Moderator
18
Years of Service
User Offline
Joined: 14th Dec 2005
Location: SF Bay Area, USA
Posted: 9th Aug 2021 05:53 Edited at: 9th Aug 2021 06:01
i understand that #CONSTANTs are supposed to return fixed values but i've seen others using it in what i'd considered a clever way, so:

Note the 3rd and 4th Print{}s should be identical:


IE, 1 key returns "properly" while its "opposite" does not. the same occurred in my original code adding:

so, it might be a "keyboard" thing and it reminds me of THIS but, if you're sharing a project that includes the above and might work fine for you, know that it may not for others.

meanwhile, i thought #CONSTANT "compiled" a fixed value; why doesn't this misuse return an error?
[My Itch.io Home] [Community Apps on Itch.io]
[AGK Resource Directory] [TGC @ GitHub]
[CODE lang=agk] YOUR CODE HERE [/CODE]
[VIDEO=youtube] VIDEO ID [/VIDEO]
[AGK Showcase][Google Forum Search]

Attachments

Login to view attachments
blink0k
Moderator
11
Years of Service
User Offline
Joined: 22nd Feb 2013
Location: the land of oz
Posted: 9th Aug 2021 07:18 Edited at: 9th Aug 2021 07:20
Constants don't return fixed values. The just substitute the left hand side for the right hand side in the code
Virtual Nomad
Moderator
18
Years of Service
User Offline
Joined: 14th Dec 2005
Location: SF Bay Area, USA
Posted: 9th Aug 2021 08:06 Edited at: 9th Aug 2021 08:20
Quote: "Description
Constants are special tokens that can be defined to hold a fixed value or string. You can use the constants anywhere in your program to specify items of data that are not going to change throughout your programs execution."

i understand "can" doesn't mean "must" but isn't "items of data that are not going to change" why constants exist? if not, i need to adjust my understanding of the concept.

meanwhile, any explanation/theory for the unexpected(?) results above? i know it's not "rollback" because it occurs whether or not either 1 or 2 keys are pressed in conjunction with #constant (but it's somehow related?).

btw:
Quote: "Constants don't return fixed values. The just substitute the left hand side for the right hand side in the code"
...is what i was hoping to achieve. basically a function() that is 2 lines shorter than a "strict" function

and, i do remember reading somewhere in the past ~15 years of tinkering with BASIC that constants are (literally) replaced with the value assigned to it when compiled (or is that something DBPro does vs AppGameKit since AppGameKit doesn't compile the code in the same way?).

regardless of it all, thanks for the response. i still hope to figure it all out "some day"
[My Itch.io Home] [Community Apps on Itch.io]
[AGK Resource Directory] [TGC @ GitHub]
[CODE lang=agk] YOUR CODE HERE [/CODE]
[VIDEO=youtube] VIDEO ID [/VIDEO]
[AGK Showcase][Google Forum Search]
blink0k
Moderator
11
Years of Service
User Offline
Joined: 22nd Feb 2013
Location: the land of oz
Posted: 9th Aug 2021 09:49 Edited at: 9th Aug 2021 09:49
i think if you change

to

You will get the result you want
Virtual Nomad
Moderator
18
Years of Service
User Offline
Joined: 14th Dec 2005
Location: SF Bay Area, USA
Posted: 9th Aug 2021 17:01
Quote: "You will get the result you want"

thanks!
[My Itch.io Home] [Community Apps on Itch.io]
[AGK Resource Directory] [TGC @ GitHub]
[CODE lang=agk] YOUR CODE HERE [/CODE]
[VIDEO=youtube] VIDEO ID [/VIDEO]
[AGK Showcase][Google Forum Search]
Raven
19
Years of Service
User Offline
Joined: 23rd Mar 2005
Location: Hertfordshire, England
Posted: 9th Aug 2021 22:14
#Constants are essentially identical to #define rather than const in C/C++
This is to say that they're a Pre-Processor Features (something that occurs at compile time, rather than runtime)

Although #Constant is frustratingly less feature complete as #define.
What I mean is., #define allows for operators, thus you can create macro programs

So let's say we want to write a Min/Max Function

Well said function would be:
Function Min( A, B )
If A < B Then ExitFunction A Else ExitFunction B
EndFunction

Now as a #define we could do:

#define Min(a,b) ( (a) < (b) ? (a) : (b) )

Of course we don't actually have the Ternary Operator... which is a shame, as it's a great little operator.
It's a Binary Operator, so it's quick in C/C++
Essentially it's a True/False Check...

A ? B : C ... literally translates to If B = True Then A = B Else A = C
This makes it great for our Min / Max Macro Definition above.

Now a reason why this works in C/C++ is because the Compiler is smart enough to replace the Inputs on the Left with the Right., where-as AGK/DBP this doesn't happen.
It instead would literally look for Min( A, B ) as opposed to Min( [ParameterA], [ParameterB] )

This said it also doesn't work like 'const' because what const does is creates static memory... this means the Memory Address for a given Variable NEVER changes., and in most cases also can't be written to; thus the value remains constant from when it's initially defined until the application ends.
I think this is the behaviour that TGC is trying to provide; but being a Pre-Processor somewhat diminishes it's usefulness... that is to say you don't necessarily need Static / Constant values that aren't reliant upon other inputs.

So let's say we're creating an Audio Subsystem... well "Constants" you will want are going to be the Audio Device, Audio Format and Speaker Format., but you'll want these as Constants ONLY while said Device is Active; you might still want to then redefine that later when you change to a new Device with different Formats... BUT you don't want these values to change until you destroy the last one (thus all of the associated Constants)
It's the same with Audio Buffers associated with such; you likely want their Memory Addresses to remain Static rather than Defining and Redefining that requires Allocation and Deallocation to constantly happen (which is a performance hit).

Thus Constants come in useful for doing such actions.
For perhaps a less Low-Level Example., let's say we have a Level where we want a Constant Seed Value., and Various Defines that are going to remain constant (and need to) while the Level is Active but you'd want to re-define once you're all done... well yeah a Pre-Processor approach isn't very useful for that.

I think the kicker to all of this., is for C/C++ you'd be using these for Application Stability and/or Performance; where-as for AppGameKit Script., there's not really much difference and you're more limited in the scope.
Be it from a Macro Definition standpoint (or lack there-of) or that it's Pre-Processor and MUST be defined at initial Runtime rather than during Runtime.
Virtual Nomad
Moderator
18
Years of Service
User Offline
Joined: 14th Dec 2005
Location: SF Bay Area, USA
Posted: 9th Aug 2021 23:05 Edited at: 9th Aug 2021 23:07
so... is:

...proper usage in AppGameKit context? does it help/hurt anything?
[My Itch.io Home] [Community Apps on Itch.io]
[AGK Resource Directory] [TGC @ GitHub]
[CODE lang=agk] YOUR CODE HERE [/CODE]
[VIDEO=youtube] VIDEO ID [/VIDEO]
[AGK Showcase][Google Forum Search]
Raven
19
Years of Service
User Offline
Joined: 23rd Mar 2005
Location: Hertfordshire, England
Posted: 10th Aug 2021 10:29
Quote: "...proper usage in AppGameKit context? does it help/hurt anything?"


It isn't how TGC intended it to be used., but really the only useful usage for it is as a "Find-and-Replace" ... so you don't have to keep typing common formula.
Now a clever way to use it, is to create some Global Variables that you're using in said #Constant replacements.

Remember these are quicker than Functions., because for some reason Functions come with some noticeable overhead...
So let's say we want a Min/Max (for arguments sake here)

Global A As Float
Global B As Float

#Constant Min ( ( (A < B) * A ) + ( (B < A) * B ) )
#Constant Max ( ( (A > B) * A) + ( (B > A) * B ) )

Now provided we popular A and B before we call these., we can use Min / Max without ever needing to call a function.
And if you're curious as to how/why this works... basically the < > are binary operators; as in their results are either 1 (True) or 0 (False)., thus multiplying by the value = either 0 or the value; and by then adding the other what is returned is the value.
Be that the Lowest or Highest., without ever using an IF Statement (which again is slow).

It's a creative usage of something that isn't really overly useful outside of a mnemonic for common things.
Say., like the Keystate Codes or adding things like True / False.

I mean commonly I have
#Constant True -1
#Constant False 0
#Constant KEY_ESC 27

at the top of every program
Kevin Picone
21
Years of Service
User Offline
Joined: 27th Aug 2002
Location: Australia
Posted: 17th Aug 2021 15:39

@VM

The issue occurs due to precedence when the expressions being evaluated. As has already been mentioned, #Constant act more like a macro, so when the parser see's the keyword in an expression it inserts those tokens (the ones after the define) in place of the keyword into the output expression..

So this, (LeftRight * (Tick#*Rate#) is seen as GetRawKeyState(39) - GetRawKeyState(37) * (Tick#*Rate#).. Once the brackets are evaluated the mult is applied to the result of the GetRawKeyState(37) function call and then subtracted from GetRawKeyState(39). Long story short, slapping the definition inside brackets will make it behave like an inlined function.


On a side note, one to be aware of, is that historically DB/DBPro doesn't actually have pre evaluation of constant literals, original AGk didn't but it may have been added in recent years. What i mean operations on literal terms can be done at compile time rather than being exported as code.

eg.
print( "Hello" + "World")





PlayBASIC To HTML5/WEB - Convert PlayBASIC To Machine Code
Virtual Nomad
Moderator
18
Years of Service
User Offline
Joined: 14th Dec 2005
Location: SF Bay Area, USA
Posted: 17th Aug 2021 18:32
Got it. Thank you!
[My Itch.io Home] [Community Apps on Itch.io]
[AGK Resource Directory] [TGC @ GitHub]
[CODE lang=agk] YOUR CODE HERE [/CODE]
[VIDEO=youtube] VIDEO ID [/VIDEO]
[AGK Showcase][Google Forum Search]

Login to post a reply

Server time is: 2024-04-25 15:39:51
Your offset time is: 2024-04-25 15:39:51