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 / type as constant?

Author
Message
Mugen Wizardry
User Banned
Posted: 9th Aug 2010 00:38 Edited at: 9th Aug 2010 01:19
how do u use a type as a constant?



CHECK OUT SOME MUSIC FROM MY NEW TECHNO CD! TECHNOKINESIS
http://www.youtube.com/watch?v=4a8KedfgVv0
ALSO, CHECK OUT MY NEW TECHNO CD! http://www.imageposeidon.com/
Mugen Wizardry
User Banned
Posted: 9th Aug 2010 02:39
can someone help please?

CHECK OUT SOME MUSIC FROM MY NEW TECHNO CD! TECHNOKINESIS
http://www.youtube.com/watch?v=4a8KedfgVv0
ALSO, CHECK OUT MY NEW TECHNO CD! http://www.imageposeidon.com/
Diggsey
20
Years of Service
User Offline
Joined: 24th Apr 2006
Location: On this web page.
Posted: 9th Aug 2010 02:47
If it's constant then it shouldn't be part of a type. If you want to a constant belonging to enemy then use enemy_somename as the name of the constant to make it more readable, but what you are trying to do at the moment is completely pointless.

[b]
Mugen Wizardry
User Banned
Posted: 9th Aug 2010 02:48
i need to be able to access it through a type.

my entire game is based on types.

CHECK OUT SOME MUSIC FROM MY NEW TECHNO CD! TECHNOKINESIS
http://www.youtube.com/watch?v=4a8KedfgVv0
ALSO, CHECK OUT MY NEW TECHNO CD! http://www.imageposeidon.com/
Jeff032
18
Years of Service
User Offline
Joined: 13th Aug 2007
Location:
Posted: 9th Aug 2010 02:58 Edited at: 9th Aug 2010 02:58
So I'm guessing the problem is this line:



What's wrong with doing this?



Mugen Wizardry
User Banned
Posted: 9th Aug 2010 03:08
because ALL my objects are based on types.

CHECK OUT SOME MUSIC FROM MY NEW TECHNO CD! TECHNOKINESIS
http://www.youtube.com/watch?v=4a8KedfgVv0
ALSO, CHECK OUT MY NEW TECHNO CD! http://www.imageposeidon.com/
Jeff032
18
Years of Service
User Offline
Joined: 13th Aug 2007
Location:
Posted: 9th Aug 2010 03:36
Perhaps you could explain in more detail what the problem is/what you need to do?

Also, what is wrong with "p.id = 1" instead of "#constant p.id 1"? They both set p.id to 1, right? (Well, that is if the second one actually worked.)

-Jeff

=PRoF=
23
Years of Service
User Offline
Joined: 17th Mar 2003
Location: Milton Keynes, UK
Posted: 9th Aug 2010 04:09
The same thing I showed you earlier for images will work with objects as well.

Why not try someth like this...



Typed arrays, surprisingly useful

Rich Dersheimer
AGK Developer
17
Years of Service
User Offline
Joined: 1st Jul 2009
Location: Inside the box
Posted: 9th Aug 2010 05:11 Edited at: 9th Aug 2010 05:15
The variables in a type cannot be constants. Variables change, that's why the are called variables.

Constants don't change, that's why they are called constants.

You can assign the value from a constant to a variable in a UDT however.

Like for instance,


If there is a variable in your User Defined Type that never changes, you can safely omit it from the type, and just use its constant value.

As =PRoF= says above, an array of UDTs is what you want. Then you can access them in a loop.

Green Gandalf
VIP Member
21
Years of Service
User Offline
Joined: 3rd Jan 2005
Playing: Malevolence:Sword of Ahkranox, Skyrim, Civ6.
Posted: 9th Aug 2010 12:35
If I recall correctly the compiler replaces all occurrences of constants with the text given in their definitions. Hence all the compiler would do, if it worked, in your example is replace p.id with 1. Whether or not that would work depends on the rest of your code. However it won't even compile because the constant declaration is expecting a variable name and p.id is not a valid name since it contains the full stop (.).

But as others have said why do you need it? If it is a constant and doesn't change - then don't change it.

In my opinion the best use of #constant in DBPro is to replace a moderately cumbersome bit of code with something shorter and more meaningful - mainly to make your code more readable. Another use is to make it hard for you to accidentally change a variable's value when it should never change - but that shouldn't be a problem if you know what you're doing when you're coding.
Sasuke
20
Years of Service
User Offline
Joined: 2nd Dec 2005
Location: Milton Keynes UK
Posted: 9th Aug 2010 12:40
Quote: "but that shouldn't be a problem if you know what you're doing when you're coding."


Hehe

A dream is a fantasy, if you achieve that fantasy it was never a dream to begin with.
Mugen Wizardry
User Banned
Posted: 9th Aug 2010 14:18
thanks guys. -sasuke

CHECK OUT SOME MUSIC FROM MY NEW TECHNO CD! TECHNOKINESIS
http://www.youtube.com/watch?v=4a8KedfgVv0
ALSO, CHECK OUT MY NEW TECHNO CD! http://www.imageposeidon.com/
Rich Dersheimer
AGK Developer
17
Years of Service
User Offline
Joined: 1st Jul 2009
Location: Inside the box
Posted: 9th Aug 2010 15:27 Edited at: 9th Aug 2010 15:34
Quote: "n my opinion the best use of #constant in DBPro is to replace a moderately cumbersome bit of code with something shorter and more meaningful - mainly to make your code more readable"


Very much agreed! Although sometimes "shorter" and "more meaningful" don't work together. When I'm coding, PLAY SOUND 1 is shorter, but if I use PLAY SOUND sfxFootsteps, I don't have to remember that the footstep sound is number 1, etc. Here are some of the constants from Quickhack:



Green Gandalf
VIP Member
21
Years of Service
User Offline
Joined: 3rd Jan 2005
Playing: Malevolence:Sword of Ahkranox, Skyrim, Civ6.
Posted: 10th Aug 2010 02:53
Some of those "constants" are actually function calls. Wouldn't it be better to pre-evaluate the function and use that instead. For example,

#constant cBlack = rgb(0,0,0)

All that is required is a specific dword value - but your solution is asking DBPro to re-evaluate it from the rgb() function every time you use it. Whether that actually matters depends on where you use this function of course.
Rich Dersheimer
AGK Developer
17
Years of Service
User Offline
Joined: 1st Jul 2009
Location: Inside the box
Posted: 10th Aug 2010 11:16
Quote: "Some of those "constants" are actually function calls"


So, every time I use a command like

INK cWhite,cBlack

DBPro has to re-calculate the constant? That hardly seems efficient! I did not know that. The only reason I'm using the rgb() in those color constants is so that I can tweak the colors as I test. Once I'm happy with the colors, I'll be sure to replace those rgb() calls with the DWORD values.

Thanks for the info!

Green Gandalf
VIP Member
21
Years of Service
User Offline
Joined: 3rd Jan 2005
Playing: Malevolence:Sword of Ahkranox, Skyrim, Civ6.
Posted: 10th Aug 2010 11:37
Quote: "DBPro has to re-calculate the constant?"


Yes, I'm fairly sure that's how it works. The compiler simply replaces all occurrences of the constant with the text given in it's definition before doing anything else.


IanM can give the definitive answer.
Zotoaster
21
Years of Service
User Offline
Joined: 20th Dec 2004
Location: Scotland
Posted: 10th Aug 2010 11:47
That's exactly what constants are. They are essentially search & replaces. Constants are best for.. well.. constants! Because you essentially get the advantages of variables from the programming side, while getting the speed of using constants (not taking the extra step to load variables). For functions, use variables, because then you don't have to recalculate it every time it's used.

"everyone forgets a semi-colon sometimes." - Phaelax
=PRoF=
23
Years of Service
User Offline
Joined: 17th Mar 2003
Location: Milton Keynes, UK
Posted: 10th Aug 2010 12:07
Hmm, this is interesting.

I do the whole #constant colBlack rgb(0,0,0) thing a lot;

I just assumed the value was stored, not that it called the function every time you used it.

Seems you Learn someth new every day

Rich Dersheimer
AGK Developer
17
Years of Service
User Offline
Joined: 1st Jul 2009
Location: Inside the box
Posted: 10th Aug 2010 17:51 Edited at: 10th Aug 2010 17:52
Quote: "They are essentially search & replaces."


So, when the compiler makes the pass to search and replace, it's not using the value of rgb(0,0,0), but the actual code to call rgb(0,0,0)? Yah, def need to put the DWORD value in there instead. Or just use a variable.

Login to post a reply

Server time is: 2026-07-25 23:12:00
Your offset time is: 2026-07-25 23:12:00