CVAR SYSTEM
This idea came from ID-Software.
I did some peaking in the Quake 4 SDK and bumped into
the CVAR system source. I thought, This is damn handy
and made a DBP version of it x3
Here's a summary about CVARS:
Quote: "
Console Variables (CVars) are used to hold scalar or string variables
that can be changed or displayed at the console as well as accessed
directly in code.
CVars are mostly used to hold settings that can be changed from the
console or saved to and loaded from configuration files. CVars are also
occasionally used to communicate information between different modules
of the program.
CVars may be declared in the global namespace, in classes and in functions.
However declarations in classes and functions should always be static to
save space and time. Making CVars static does not change their
functionality due to their global nature.
CVars should be contructed only through one of the constructors with name,
value, flags and description. The name, value and description parameters
to the constructor have to be static strings.
CVars may be declared multiple times using the same name string. However,
they will all reference the same value and changing the value of one CVar
changes the value of all CVars with the same name.
"
I made a small demo which can be found here.
You'll have to INLCUDE the function source file yourself.
DEMO.DBA
`=================== `
`==== CVAR DEMO ==== `
`=================== `
` ----------------------------------- `
` This source shows you what the CVAR-`
` System can do. `
` ----------------------------------- `
_PragmaHeader.CVARS() `Include the CVAR Headers.
Set Window On
Set Display mode 800,600,32,0
Sync Off
Print "This demo shows you what CVARs can do."
print "They are handy for dynamicly add or maintain"
Print "global variables which can be instantly"
Print "accessed by your application and used."
Print ""
Print "Ok, lets create our first CVAR called 'MyCVAR'"
Print "Press Any key to continue..."
setCVAR("MyCVAR","My Variable",_CVAR_ARCHIVE,"MyCVAR's Description")
Suspend For Key
Print "Ok, The CVAR has been created. Now Press a Key to display"
Print "it's variable and description."
Print " "
Suspend For Key
Print CVAR_Str("MyCVAR") + " :: " + CVAR_Description("MyCVAR")
Print ""
Print "Pretty cool huh?"
Print ""
Print "Ok, lets modify our variable now."
Print "Just use the same command: setCVAR(). But since the CVAR"
Print "already exists, it will now modify the existing CVAR"
Print "IF it has access rights to. If the CVAR is set"
Print "to _CVAR_ROM or _CVAR_SYSTEM, you won't be able to change"
Print "the CVAR variables."
Print "Press Any key to continue..."
Suspend For key
Print ""
Print "Ok, This is the original CVAR's Variable:" + CVAR_Str("MyCVAR")
Print "Lets Modify it. Press a key..."
Print ""
Suspend For Key
SetCVAR("MyCVAR", "A new Variable",0,"")
`NOTE: You can leave the cType and Desc values empty if you
` are changing a value.
Print "The modified value is: " + CVAR_Str("MyCVAR")
Print "Ok, that's all for today. Have fun with it."
Print ""
Print "Post comments and feedback please x3"
Suspend For Key
End
And here's the CVARSystem.DBA file.
Include this one to the DEMO.DBA file to make it work
` ========================= `
` ====== CVAR SYSTEM ====== `
` ===-------------------=== `
` === Author: H.Iedema === `
` === Version: v1.2 === `
` ========================= `
#CONSTANT _CVAR_FALSE 0x00
#CONSTANT _CVAR_TRUE 0x01
#CONSTANT _CVAR_UNSIGNED 0x11
#cONSTANT _cVAR_ARCHIVE 0x00
#CONSTANT _CVAR_ROM 0x01
#CONSTANT _CVAR_CHEAT 0x02
#CONSTANT _CVAR_SYSTEM 0x03
#CONSTANT _CVAR_NORMAL 0x12
#CONSTANT _CVAR_MODIFIED 0x14
// _HEADER_ //
__CVAR_HEADER:
TYPE CVAR_ENUM
Name As String
Variable As String
cType As Byte
Flags As Byte
Desc As String
ENDTYPE
DIM __CVAR(0) As CVAR_ENUM
__CVAR(0).Name = "DefaultCVAR"
__CVAR(0).Variable = ""
__CVAR(0).cType = _CVAR_SYSTEM
__CVAR(0).Flags = _CVAR_NORMAL
__CVAR(0).Desc = "DO NOT REMOVE!"
RETURN
// !_HEADER_! //
// ========= //
// FUNCTIONS //
// ========= //
Function setCVAR(Name As String, Var As String, cType As Byte, Desc As String)
If _unsigned_cvar(Name)
//If _unsigned_str(Name) Then ExitFunction
//If _unsigned_str(Var) Then ExitFunction
_cvar_insert()
index = _max_CVARs()
__CVAR(index).Name = Name
__CVAR(index).Variable = Var
__CVAR(index).cType = cType
__CVAR(index).Flags = _CVAR_NORMAL
__CVAR(index).Desc = Desc
Else
if _cvar_flags(Name) <> _CVAR_ROM Or _cvar_flags(Name) <> _CVAR_SYSTEM
index = _getCVAR_index(Name)
__CVAR(index).Variable = Var
__CVAR(index).Flags = _CVAR_MODIFIED
EndIf
EndIf
EndFunction
Function CVAR_Description(Name As String)
Local r as String
If not _unsigned_cvar(Name)
n = _GetCVAR_index(Name)
r = __CVAR(n).Desc
EndIf
EndFunction r
Function CVAR_Str(Name As String)
Local r as String
If not _unsigned_cvar(Name)
n = _GetCVAR_index(Name)
r = __CVAR(n).Variable
EndIf
EndFunction r
Function CVAR_Int(Name As String)
Local r as Integer
If not _unsigned_cvar(Name)
n = _GetCVAR_index(Name)
r = val(__CVAR(n).Variable)
EndIf
EndFunction r
Function CVAR_Float(Name As String)
Local r as Float
If not _unsigned_cvar(Name)
n = _GetCVAR_index(Name)
r = val(__CVAR(n).Variable)
EndIf
EndFunction r
// ================== //
// INTERNAL FUNCTIONS //
// ================== //
Function _cvar_flags(n as String)
Local f As Byte
If Not _max_CVARs() Then ExitFunction _CVAR_FALSE
For i = 1 To _max_CVARs()
If __CVAR(i).Name = n
f = __CVAR(i).cType
ExitFunction _CVAR_FALSE
EndIf
Next i
EndFunction _CVAR_FALSE
Function _cvar_insert()
Array Insert At Bottom __CVAR()
EndFunction
Function _unsigned_int(n as Integer)
If Not n Then ExitFunction _CVAR_TRUE
EndFunction _CVAR_FALSE
Function _unsigned_str(n as String)
If n = "" Then ExitFunction _CVAR_TRUE
EndFunction _CVAR_FALSE
Function _max_CVARs()
Local n as Integer
n = Array Count(__CVAR())
EndFunction n
Function _unsigned_cvar(Name as String)
If Not _max_CVARs() Then ExitFunction _CVAR_TRUE
For i = 1 To _max_CVARs()
If __CVAR(i).Name = Name Then ExitFunction _CVAR_FALSE
Next i
EndFunction _CVAR_TRUE
Function _PragmaHeader.CVARS()
Gosub __CVAR_HEADER
EndFunction
Function _GetCVAR_index(Name As String)
If Not _max_CVARs() Then ExitFunction _CVAR_FALSE
For i = 1 To _max_CVARs()
If __CVAR(i).Name = Name Then ExitFunction i
Next i
EndFunction _CVAR_FALSE
My own (private) version also supports import/export
of CVAR library files. But the smart people around
here can figure how to do that for them selfs.
Anyways, Have fun x3
Any improvements, suggestions are welcome.
- : aXeon Essentials : -