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.

Author
Message
TommoHand
18
Years of Service
User Offline
Joined: 17th Nov 2005
Location:
Posted: 17th Nov 2005 15:24
What does the command #constant actually do?

I really need help on this one.
The Nerd
20
Years of Service
User Offline
Joined: 5th Jun 2004
Location: Denmark
Posted: 17th Nov 2005 21:29 Edited at: 17th Nov 2005 21:30
Well, it makes a variable un-changeable during your program. Lets say you created this source code:


Now, you will get an error when compiling this program, why? Because you set the variable hello to a constant, wich means that you are not allowed to change this variable at all. But if you make this source:

You can compile it, simply because the variable hello is not a constant anymore. You can use constants to make sure that the program will not change a variable anytime when the program runs.

Hope it helped

-The Nerd

PanzerGameshere
Free Particle Engine For dbpro :
here
Freddy 007
19
Years of Service
User Offline
Joined: 30th Nov 2004
Location: Denmark
Posted: 18th Nov 2005 11:38 Edited at: 18th Nov 2005 11:39
The #constant make a certain variable, well, constant. As The Nerd says, unchangeable. Although it also makes the variable GLOBAL. If you don't know the GLOBAL command, then it makes a variable valid in all scopes. Umm... Here's an example:



That code will print '0' to the screen. All variables you declare outside a certain scope, will not be available inside the scope(function). But if you make the variable #constant or GLOBAL, the variable becomes available in all scopes:



The #constant can also help your code look clearer. If fx. you have an alien spaceship in your game, then you want the spaceship to damage the player with the same value each time it hits. You could write it like this(pseudocode, not real code):



Then the player will loose 15 healthpoints each time he's hit. But later on, you might get back to this piece of code and think: ,, Why does it say fifteen?".
You can simply write the code like this, to make it more readable and easier to understand:



Now you can see that the players health is decreased, with the amount of the aliens damage.

I hope this helps you


I can't be fired... Slaves are SOLD!
spooky
22
Years of Service
User Offline
Joined: 30th Aug 2002
Location: United Kingdom
Posted: 18th Nov 2005 12:38
Think of #constant more as a pre-complier search and replace.

When you compile your program, the first thing that happens is all constants are replaced in code with their #constant values, then this new source code is compiled into your exe. So you can use them for values, commands, strings, whatever you like!

This should explain what I mean;



It's great for shortening those longwinded commands that make code look untidy.

Boo!
Freddy 007
19
Years of Service
User Offline
Joined: 30th Nov 2004
Location: Denmark
Posted: 18th Nov 2005 12:41
I didn't know that

Thanks


I can't be fired... Slaves are SOLD!
Lukas W
21
Years of Service
User Offline
Joined: 5th Sep 2003
Location: Sweden
Posted: 18th Nov 2005 14:12
what i really like about constants is the fact that i can use cool text effects like these:



Zombie Hunter DBPro Open Source Project:
editor: 14% | survive mode: 30% | multiplayer: 0% | story mode: 0% | DEMO: 18%
Peter H
20
Years of Service
User Offline
Joined: 20th Feb 2004
Location: Witness Protection Program
Posted: 18th Nov 2005 18:04
@Spooky- Crikey! i need to visit this board more often... i had no idea you could use constants to replace commands!

"We make the worst games in the universe..."
Me!
19
Years of Service
User Offline
Joined: 26th Jul 2005
Location:
Posted: 18th Nov 2005 21:52
nor did I, time to replace that verbose rubbish with something shorter and sweeter , nice one Spooky, you just cured the one big gripe I have with DB, have a cup of cocoa with 50% whiskey in it on me <sings> hi ho! hi ho! a header writing I will go, to crop the verbose and spell colour right, hi ho!



if there is one thing I can NOT tolerate, it`s intolerant people.
TommoHand
18
Years of Service
User Offline
Joined: 17th Nov 2005
Location:
Posted: 19th Nov 2005 16:20
Thanks alot guys that all really helped.

Can move on to doing some more coding now .

Thanks again!
PeaKnuckle
19
Years of Service
User Offline
Joined: 10th Nov 2004
Location:
Posted: 20th Nov 2005 01:33
Quote: "Think of #constant more as a pre-complier search and replace.

When you compile your program, the first thing that happens is all constants are replaced in code with their #constant values, then this new source code is compiled into your exe. So you can use them for values, commands, strings, whatever you like!"



I have to say that was probably the tip of the day for me. very neat indeed. Thank you for posting that.

Login to post a reply

Server time is: 2024-09-24 09:24:10
Your offset time is: 2024-09-24 09:24:10