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.

DarkBASIC Professional Discussion / What Happened to the GLOBAL Command in DBPro?

Author
Message
WickedVixen
19
Years of Service
User Offline
Joined: 28th Mar 2007
Location: New Brisbane, Utopia Prime, VGC GHQ
Posted: 12th Dec 2012 21:03
I'm trying to code some global variables like I was with Think Pascal back in the day, and the GLOBAL command is missing from the latest version of DBPro!

I used to be able to throw the GLOBAL command at the beginning of my code and not have to worry about altering the variables as I marked my global variables as gVAR, as shown.

I would code TRUE as gT and FALSE as gF for faster usage in my programs. This allowed better readability than using a 1 or 0.

Please help me with this!!! Thanks.

Green Gandalf
VIP Member
21
Years of Service
User Offline
Joined: 3rd Jan 2005
Playing: Malevolence:Sword of Ahkranox, Skyrim, Civ6.
Posted: 12th Dec 2012 21:37
Just declare your variables like this:



Arrays are global by default.

You'll have to use DBPro syntax rules if you want to use DBPro I'm afraid.
sladeiw
17
Years of Service
User Offline
Joined: 16th May 2009
Location: UK
Posted: 13th Dec 2012 16:21 Edited at: 13th Dec 2012 16:22
Quote: "I would code TRUE as gT and FALSE as gF for faster usage in my programs. This allowed better readability than using a 1 or 0."


For true/false flags I use constants rather than globals:

#constant FALSE 0
#constant TRUE 1
The Weeping Corpse
14
Years of Service
User Offline
Joined: 19th Sep 2011
Location: United Kingdom
Posted: 14th Dec 2012 01:07
yeah, constants are the way to go, they are faster at runtime.

Dar13
18
Years of Service
User Offline
Joined: 12th May 2008
Location: Microsoft VisualStudio 2010 Professional
Posted: 14th Dec 2012 02:45
Quote: " yeah, constants are the way to go, they are faster at runtime."

They're faster at compile time too.

UncleRemus
14
Years of Service
User Offline
Joined: 19th Mar 2012
Location:
Posted: 14th Dec 2012 05:56
Quote: "They're faster at compile time too."


They're faster when trying to find in your project manager tree opposed to looking through a long list of variables (in the chance you take a break from coding for awhile and forget), too!

LOL, bottom line....constant is the way to go. Three strikes, and global is out!
Phaelax
DBPro Master
23
Years of Service
User Offline
Joined: 16th Apr 2003
Location: Metropia
Posted: 14th Dec 2012 16:13
Quote: "I would code TRUE as gT and FALSE as gF for faster usage in my programs. This allowed better readability than using a 1 or 0."


I don't think gT and gF make for better readability, since those names aren't descriptive I wouldn't have a clue what they meant. True and false, though a few extra characters longer, and infinitely more readable as everyone would instantly know what it meant.

"You're not going crazy. You're going sane in a crazy world!" ~Tick
Kevin Picone
23
Years of Service
User Offline
Joined: 27th Aug 2002
Location: Australia
Posted: 14th Dec 2012 16:41
Since #Constant is implemented as replacement macro, can't see how this would improve compile times.

Qqite
16
Years of Service
User Offline
Joined: 28th Feb 2010
Location: Florida, U.S.
Posted: 14th Dec 2012 17:08 Edited at: 14th Dec 2012 17:08
A variable is a reference to a saved memory location, where a constant isn't.

It's like saying "Go read my essay which is in the 53rd folder over there" compared to "here's my essay."

Macros are just changed into numbers behind the scenes before it gets compiled, and straight numbers are always better than variables, global or not.

Ventures of the worlds around us are limited only by our imagination.
Chris Tate
DBPro Master
17
Years of Service
User Offline
Joined: 29th Aug 2008
Location: London, England
Posted: 14th Dec 2012 17:38
Quote: "Macros are just changed into numbers"
... and strings, chunks and function calls.



Dar13
18
Years of Service
User Offline
Joined: 12th May 2008
Location: Microsoft VisualStudio 2010 Professional
Posted: 14th Dec 2012 21:28
Quote: "Since #Constant is implemented as replacement macro, can't see how this would improve compile times."

Preprocessors are very very fast most of the time, as it removes work from the compiler(replacing each reference to the constant with a literal is easier than referencing a variable).

Green Gandalf
VIP Member
21
Years of Service
User Offline
Joined: 3rd Jan 2005
Playing: Malevolence:Sword of Ahkranox, Skyrim, Civ6.
Posted: 15th Dec 2012 00:22
Quote: "as it removes work from the compiler"


How? All #constant does is tell the pre-processor which code to use to replace the constant name before it gets compiled. The compiler still has to compile the resulting code. For example the following code



is exactly the same as



when compiled.

Since all you've achieved is an extra pre-processor step it's hard to see where the saving lies.
Ortu
DBPro Master
18
Years of Service
User Offline
Joined: 21st Nov 2007
Location: Austin, TX
Posted: 15th Dec 2012 01:51
Sure when the constant is a command it's not really saving anything, savings come when it's a value






Green Gandalf
VIP Member
21
Years of Service
User Offline
Joined: 3rd Jan 2005
Playing: Malevolence:Sword of Ahkranox, Skyrim, Civ6.
Posted: 15th Dec 2012 12:06
Er, not quite. Those two things are not equivalent code. The first snippet is equivalent to



The advantage of your first snippet over this one is primarily a coding advantage. Instead of having to change every occurrence of the string 123 in your code when you change the value, you only have to change the value of the constant once - and you can't accidentally change it elsewhere in your code unlike variables.

Literals may or may not be processed faster than variables but that is not the main reason for using #constants.
Dar13
18
Years of Service
User Offline
Joined: 12th May 2008
Location: Microsoft VisualStudio 2010 Professional
Posted: 15th Dec 2012 16:34
Quote: "Er, not quite. Those two things are not equivalent code."

They're not supposed to be. One uses a constant, the other uses a variable.

Quote: "Literals may or may not be processed faster than variables"

If they don't, then DBP needs a serious compiler upgrade. But yes I do agree that the primary advantage is a coding one.

WickedVixen
19
Years of Service
User Offline
Joined: 28th Mar 2007
Location: New Brisbane, Utopia Prime, VGC GHQ
Posted: 17th Dec 2012 02:26
I couldn't figure out which has advantages over the other, but it seems that #CONSTANT is faster, as a pre-processor component, than GLOBAL which allows for the use of a variable initialization that precludes scope variables.

This is what I knew, but here's a code snippet that I have used in other applications. 'gT' and 'gF' or 'T' and 'F' I never have elsewhere in my program defined as other things.

It seems that with this amount of assistance, and usually on a regular basis, I know where to go for any help for my DBPro dilemmas.

Thanks to all of you for your awesome help. No matter what, there's a use for GLOBAL variables and for #CONSTANT variables. I think it was just a matter of syntax, and the little overlooked craziness on my part; panicking and all. Again, to all of you, a "Thank You"!





pictionaryjr
17
Years of Service
User Offline
Joined: 12th Mar 2009
Location:
Posted: 17th Dec 2012 22:11 Edited at: 17th Dec 2012 22:15
Gandalf, that's what dar was saying. the constant would be quicker because it doesn't have to call to another memory location and just goes directly to it making it quicker although that may not be the case in all situations, if used wisely it can be.

Edit:
http://www.amazon.com/Game-Programming-For-Serious-Creation/dp/1435455568
Check this book out for example, on the bottom of page 6 and the top of page 7. It talks about the differences between references in c# which is the dbp equivalent of variables and values in c# which is dbp equivalent of constants.
Green Gandalf
VIP Member
21
Years of Service
User Offline
Joined: 3rd Jan 2005
Playing: Malevolence:Sword of Ahkranox, Skyrim, Civ6.
Posted: 18th Dec 2012 01:51
Quote: "Gandalf, that's what dar was saying."


I know - but that was my point. In DBPro, constants do not always compile to simple literals since they are nothing more than simple text string replacements and are replaced by that string whatever it is. My concern is that many people here don't seem to appreciate that simple fact.

Quote: "It talks about the differences between references in c# which is the dbp equivalent of variables and values in c# which is dbp equivalent of constants."


That is simply not true. In DBPro, constants are not simple values except in simple cases. I can't speak for C++ or C# though.
Dar13
18
Years of Service
User Offline
Joined: 12th May 2008
Location: Microsoft VisualStudio 2010 Professional
Posted: 18th Dec 2012 04:26
Quote: "My concern is that many people here don't seem to appreciate that simple fact."

Ah, so that's what you meant. I've rarely used constants for anything else but a simple literal value so I forgot to account for that.

I agree, in any other case where you do something more advanced than "#constant TRUE 1" or "#constant OFFSET_Y .5" then it moves out of the realm of comparison with global variables.

Ortu
DBPro Master
18
Years of Service
User Offline
Joined: 21st Nov 2007
Location: Austin, TX
Posted: 18th Dec 2012 04:56
Quote: "I've rarely used constants for anything else but a simple literal value "


Yeah same here,I tend to forget you can stick code etc in there unless something reminds me of it

pictionaryjr
17
Years of Service
User Offline
Joined: 12th Mar 2009
Location:
Posted: 18th Dec 2012 18:15
Quote: "That is simply not true. In DBPro, constants are not simple values except in simple cases. I can't speak for C++ or C# though."


That's interesting. I was always under the impression that they were. you learn something new everyday
BMacZero
20
Years of Service
User Offline
Joined: 30th Dec 2005
Location: E:/ NA / USA
Posted: 20th Dec 2012 03:31 Edited at: 20th Dec 2012 03:32
In C# and C++ you have #define, which functions like DBP's #constant, and you have the const modifier for "variables", which actually makes a variable that isn't allowed to be changed.

More on-topic, did you actually figure out why GLOBAL wasn't working?

http://brianmacintosh.com
"'Who's that primitive entity accessing sliding collision data from my bridge?!', the troll roared." - TheComet

Login to post a reply

Server time is: 2026-07-09 02:34:33
Your offset time is: 2026-07-09 02:34:33