Questions about Constants

New to FreeBASIC? Post your questions here.
Chained to Fate
Posts: 52
Joined: Apr 15, 2008 16:12
Location: Oregon

Questions about Constants

Postby Chained to Fate » Sep 23, 2009 19:46

I am building a program framework to put all the peices of my masterpeice in, and I have a question. I want to load a txt file that holds my users preferences, then define the keys for the game based on those preferences. I currently have the keys defined as CONSTants, but if the player changes the keys, I am not able to re-define the key CONSTants.

I have never used #Define before, does it work like CONST? If I set #DEFINE UP_KEY = 75, will it stay as that accross functions and includes, and throw an error if something tries to over-write it? (that is, until I #UNDef?)

Does anyone have any ideas how to do this better?
Merick
Posts: 1038
Joined: May 28, 2007 1:52

Postby Merick » Sep 23, 2009 20:19

#define is a pre-processor statement - meaning that it works only when compiling, I.E., if you have

#define seven 7

when you compile, fbc will replace all instances of "seven" with the number 7, which will then be hard-coded into the exe.
sys49152
Posts: 43
Joined: Jun 11, 2005 21:36

Re: Questions about Constants

Postby sys49152 » Sep 23, 2009 20:33

Chained to Fate wrote:I am building a program framework to put all the peices of my masterpeice in, and I have a question. I want to load a txt file that holds my users preferences, then define the keys for the game based on those preferences. I currently have the keys defined as CONSTants, but if the player changes the keys, I am not able to re-define the key CONSTants.

I have never used #Define before, does it work like CONST? If I set #DEFINE UP_KEY = 75, will it stay as that accross functions and includes, and throw an error if something tries to over-write it? (that is, until I #UNDef?)

Does anyone have any ideas how to do this better?


What you want is VARiables.

cange the line:

CONST UP_KEY=75

to:

VAR UP_KEY=75
Chained to Fate
Posts: 52
Joined: Apr 15, 2008 16:12
Location: Oregon

Postby Chained to Fate » Sep 23, 2009 20:38

Ok, so VAR Declares a variable whose type is implied from the initializer expression.

but how is it different to do

Code: Select all

VAR UP_KEY = 75

rather than do

Code: Select all

DIM SHARED UP_KEY AS INTEGER
UP_KEY = 75

?
sys49152
Posts: 43
Joined: Jun 11, 2005 21:36

Postby sys49152 » Sep 23, 2009 21:05

Chained to Fate wrote:Ok, so VAR Declares a variable whose type is implied from the initializer expression.

but how is it different to do

Code: Select all

VAR UP_KEY = 75

rather than do

Code: Select all

DIM SHARED UP_KEY AS INTEGER
UP_KEY = 75

?


Its not different. Its just shorter to write.
I think its there to make freebasic more compatible with other
basic dialects.
MichaelW
Posts: 3500
Joined: May 16, 2006 22:34
Location: USA

Postby MichaelW » Sep 23, 2009 21:39

Its not different.

Variables declared with VAR are not automatically shared.
cha0s
Site Admin
Posts: 5317
Joined: May 27, 2005 6:42
Location: Illinois
Contact:

Postby cha0s » Sep 23, 2009 22:10

Well, to round it out... it's possible to do

Code: Select all

var shared up_key = 75
jevans4949
Posts: 1154
Joined: May 08, 2006 21:58
Location: Crewe, England

Postby jevans4949 » Sep 23, 2009 22:46

@Chained to Fate: If I were you, I would put all the user-configurable items in a UDT, with default values to initialise them. When the user changes them, save the UDT by PUTting to a BINARY file. At the start of each game, if the file is present, GET the UDT (otherwise the defaults apply). Just declare the variables as standard types - integers or zstrings - in the UDT, to which you can compare the results of INKEY or MULTIKEY, or whatever you are using.

Somewhere on these boards there are also routines for handling a .INI file, which you could consider as an alternative.
sys49152
Posts: 43
Joined: Jun 11, 2005 21:36

Postby sys49152 » Sep 23, 2009 22:47

MichaelW wrote:
Its not different.

Variables declared with VAR are not automatically shared.

Whoops! That's right.
notthecheatr
Posts: 1759
Joined: May 23, 2007 21:52
Location: Cut Bank, MT
Contact:

Postby notthecheatr » Sep 24, 2009 19:52

If you want a preferences settings, you need to store them as variables, not constants. I'd recommend using a UDT, as jevans suggested. This also allows you to pass them about to various parts of the program, instead of using global variables, which are usually considered poor practice.


For storing the preferences in a file, I highly recommend using libINI by sir_mud. It's quite old but I still use it, and it seems to work just fine.

There are other libraries you can use too, such as XML libraries, but for your purposes a simple INI file will probably do. They're very easy to edit, and the library is very easy to use.

Return to “Beginners”

Who is online

Users browsing this forum: No registered users and 5 guests