Questions about Constants

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

Questions about Constants

Post by Chained to Fate »

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

Post by Merick »

#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

Post by sys49152 »

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

Post by Chained to Fate »

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

Post by sys49152 »

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

Post by MichaelW »

Its not different.
Variables declared with VAR are not automatically shared.
cha0s
Site Admin
Posts: 5319
Joined: May 27, 2005 6:42
Location: USA
Contact:

Post by cha0s »

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

Code: Select all

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

Post by jevans4949 »

@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

Post by sys49152 »

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:

Post by notthecheatr »

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.
Post Reply