Questions about Constants
-
- Posts: 52
- Joined: Apr 15, 2008 16:12
- Location: Oregon
Questions about Constants
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?
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?
Re: Questions about Constants
What you want is VARiables.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?
cange the line:
CONST UP_KEY=75
to:
VAR UP_KEY=75
-
- Posts: 52
- Joined: Apr 15, 2008 16:12
- Location: Oregon
Ok, so VAR Declares a variable whose type is implied from the initializer expression.
but how is it different to do
rather than do
?
but how is it different to do
Code: Select all
VAR UP_KEY = 75
Code: Select all
DIM SHARED UP_KEY AS INTEGER
UP_KEY = 75
Its not different. Its just shorter to write.Chained to Fate wrote:Ok, so VAR Declares a variable whose type is implied from the initializer expression.
but how is it different to dorather than doCode: Select all
VAR UP_KEY = 75
?Code: Select all
DIM SHARED UP_KEY AS INTEGER UP_KEY = 75
I think its there to make freebasic more compatible with other
basic dialects.
Well, to round it out... it's possible to do
Code: Select all
var shared up_key = 75
-
- Posts: 1186
- Joined: May 08, 2006 21:58
- Location: Crewe, England
@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.
Somewhere on these boards there are also routines for handling a .INI file, which you could consider as an alternative.
-
- Posts: 1759
- Joined: May 23, 2007 21:52
- Location: Cut Bank, MT
- Contact:
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.
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.