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.

Code Snippets / [DBP] CVAR System

Author
Message
aXeon
17
Years of Service
User Offline
Joined: 28th Jun 2007
Location:
Posted: 24th Aug 2007 00:54
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


And here's the CVARSystem.DBA file.
Include this one to the DEMO.DBA file to make it work



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 : -
Mr Kohlenstoff
18
Years of Service
User Offline
Joined: 7th Jun 2006
Location: Germany
Posted: 24th Aug 2007 14:50
The system looks interesting.. but might be a bit slow for games. (At least if you use "hundreds" of variables and call them multiple times each loop.. your _GetCVAR_index-Function looks through the whole array always. It would be faster if you sorted new created variables (by their name) and use a binary search then. Another method would be hashing to avoid string-comparisons (so you create a dword depending on the string, and if the compared dwords are equal you compare the string.. I tested this method some weeks ago, and the hash-method brings a speed-boost of ~30% compared to string-comparison. Binary search on the other hand can give you more then 90% speed-boost by average when you have several 100 variables.))
Another aspect is the variable-creation. You could insert 10 elements to the array instead of one and use them one after each other. So you start with an array of 10 elements, as soon as the 11th variable would be created you add another 10 elements etc. At least on my computer this is much faster then creating one element each time (seems like the "insert at bottom" operation needs nearly the same time, not depending on the amount of elements you add, so 10x1 element need around 9 times more time (u know^^) then 1x10 new elements.)
However.. for smaller projects it might be very usefull, but speeding it up might be a good idea, imho .

[I hope everything I said was understandable ]

aXeon
17
Years of Service
User Offline
Joined: 28th Jun 2007
Location:
Posted: 24th Aug 2007 18:29
Hmm.. I havent tested it with speed optimization much
since I have a high-end pc myself. The speed what I get
is the same as when using normal variables.

My current project contains over 1k vars where I dont
really notice any decreament in runtime speed.

I think I understand what you mean, well..A part
is understandable lol.. Recode the 'return index'
function if you want and post it here.

I'm quite curious if it really speeds up that much.

- : aXeon Essentials : -
Mr Kohlenstoff
18
Years of Service
User Offline
Joined: 7th Jun 2006
Location: Germany
Posted: 24th Aug 2007 18:41
Wow, I didn't think that it runs with the same speed on your computer. In this case there can't be much to speed up lol.. maybe I'll try it here later.
Oh, btw thanks for sharing.

Mr Kohlenstoff
18
Years of Service
User Offline
Joined: 7th Jun 2006
Location: Germany
Posted: 24th Aug 2007 19:25 Edited at: 24th Aug 2007 20:08
On my computer the following code runs with 1500 FPS when using DBP-Variables and with 400 FPS with yours. So for 500 variable-changes it needs 1-2 ms.. not much of course, but DBP is around 20 times faster. I tried speeding it up with hashing, but didn't get much improvement... if I find a solution I'll inform you.



Edit: Rmpf, I used binary search now, but that halved the speed, when I used 100 variables. Very strange.. maybe it's just faster with even more variables.

Ian T
22
Years of Service
User Offline
Joined: 12th Sep 2002
Location: Around
Posted: 5th Oct 2007 02:56
Slow with tons of variables, but I can't imagine a game needing more than a few hundred and most of those would only be called occassionaly, not at runtime. There's no reason to have to check them more than once, really - get the CVAR and store it in a standard variable if you need to use it every loop.
aXeon
17
Years of Service
User Offline
Joined: 28th Jun 2007
Location:
Posted: 7th Oct 2007 13:58
Well, that's an idea Ian, BUT...

What if you'd use the same method for Device-Input?
Here's what I mean:

Imagine, you have a program with an ingame console that can be activated by pressing a key that the user defined previously.

Ex.: 'Event.Register("ToggleConsole",DEVICE_KEYBOARD,"~")'

Now In the Loop you'd be using this:

If gEvent("ToggleConsole") Then Console.Active = True

The gEvent() function scans the array for defined keys which contain the keycode of (as in the example '~'.
If you'd be doing this for every key, Id MIGHT slow down on some computers.

I'm using these methods because I'm making a Framework. So everything has to be completly modifyable outside the source.
And also, They keys can be defined by using the ingame console
or some 'settings' menu.

- : aXeon Essentials : -

Login to post a reply

Server time is: 2024-11-22 13:40:00
Your offset time is: 2024-11-22 13:40:00