[Include] GVars - Just like PVars, except global.
#1

Information

I've been a bit light on my shoulders lately and decided to release this GVar include (Global Variables). Similar to SA:MP's Player variable functions (Ex: SetPVarInt), these are used globally.

Functions

SetGVarString(varName[], value[])

varName - Variable storage name
value - A string value.

returns
- The variable ID.

Код:
Example - SetGVarString("Cookies", "Taste good");
GetGVarString(varName[])

varName - Variable storage name

returns
- The stored string.

Код:
Example - printf("Cookies %s", GetGVarString("Cookies"));
SetGVarInt(varName[], value)

varName - Variable storage name
value - The value of the variable.

returns
- The variable ID.

Код:
Example - SetGVarInt("L33t", 1337);
GetGVarInt(varName[])

varName - Variable storage name

returns
- The value stored.

Код:
Example - printf("He's %i", GetGVarInt("L33t"));
SetGVarFloat(varName[], Float:value)

varName - Variable storage name
value - The float value you wan't store.

returns
- The variable ID

Код:
Example - 
new Float:pPos[3];
GetPlayerPos(playerid, pPos[0], pPos[1], pPos[2]);
SetGVarFloat("X", pPos[0]);
SetGVarFloat("Y", pPos[1]);
SetGVarFloat("Z", pPos[2]);
Float:GetGVarFloat(varName[])

varName - Variable storage name

returns
- The value stored.

Код:
Example - printf("You're at %f|%f|%f", GetGVarFloat("X"), GetGVarFloat("Y"), GetGVarFloat("Z"));
CountGVars()


returns
- The amount of variables used.

Код:
Example - printf("We created %i GVars", CountGVars());
Notes

\

Download

Click the banner (Pastebin)

Reply
#2

Nice job Carlton, I'll be testing it soon.
Reply
#3

Nice. I will be using this for sure
Reply
#4

Nice one but in our Russian section MX_Master already created this include https://sampforum.blast.hk/showthread.php?tid=137501
Reply
#5

Awesome include Carlton =) Will be useful for me soon :P
Reply
#6

Quote:
Originally Posted by Romanius
Посмотреть сообщение
Nice one but in our Russian section MX_Master already created this include https://sampforum.blast.hk/showthread.php?tid=137501
I like how MX_Master used properties to save the data, very nice, however I heard properties can become slow after multiple are set.

(I find it funny that I thought set/get/existsproperty functions actually created ownable properties in-game when I first started scripting :P)
Reply
#7

Thank you all
Reply
#8

Quote:

I heard properties can become slow after multiple are set.

Better not to believe people guesswork without facts. Why don't you just test it yourself?

I was testing its speed and the system seems to be slow.
It loops through the whole array and compares the string with all the strings in the array. It is especially slow if the needed gvar is near the end of the gvar array. Using a binary tree may help for faster search.

Using setproperty & getproperty is more efficient, not only because it is native. Its algorithm is much better.
Even though, if you want to get a nonstring (money for example), you have to unpack it and convert it to a value first, it still faster.


Here are the results (I also edited the .inc defines):

Код:
Setting + Getting + saving in a variable.

10000 Iterations:
Property: 0230
GVar: 20264


100 Iterations:
Property: 0000
GVar: 0019

Setting only (property method needs unpacking and strval() everytime. So the property method is even faster here)


10000 Iterations:
Property: 0219
GVar: 20256


100 Iterations:
Property: 0000
GVar: 0019


Property indexing by names instead of unique values:

Setting + Getting + saving in a variable

I also tested Icognitosґ GVar and it is even faster than the property functions! Use the plugin.


10000 Iterations:
Property: 2113
GVar: 20139


100 Iterations:
Property: 0000
GVar: 0019

The testing script part:
http://pastebin.com/i8cy8JWF
(credits to ******. Used his testing script.)
Why not just use:

#define SetGVar(%1, %2) setproperty(0, "", makehash(%1), %2)
or
#define SetGVar(%1, %2) setproperty(0, %1, 0, %2)
for example

makehash(adler32 / bernstein /.. but it has to return a value, not a string(MD5)) converts a string into an unique ID value (int).
Reply
#9

Quote:
Originally Posted by Remis
Посмотреть сообщение
Better not to believe people guesswork without facts. Why don't you just test it yourself?

I was testing its speed and the system seems to be slow.
It loops through the whole array and compares the string with all the strings in the array. It is especially slow if the needed gvar is near the end of the gvar array. Using a binary tree may help for faster search.

Using setproperty & getproperty is more efficient, not only because it is native. Its algorithm is much better.
Even though, if you want to get a nonstring (money for example), you have to unpack it and convert it to a value first, it still faster.


Here are the results (I also edited the .inc defines):

Код:
Setting + Getting + saving in a variable.

10000 Iterations:
Property: 0230
GVar: 20264


100 Iterations:
Property: 0000
GVar: 0019

Setting only (property method needs unpacking and strval() everytime. So the property method is even faster here)


10000 Iterations:
Property: 0219
GVar: 20256


100 Iterations:
Property: 0000
GVar: 0019


Property indexing by names instead of unique values:

Setting + Getting + saving in a variable

I also tested Icognitosґ GVar and it is even faster than the property functions! Use the plugin.


10000 Iterations:
Property: 2113
GVar: 20139


100 Iterations:
Property: 0000
GVar: 0019

The testing script part:
http://pastebin.com/i8cy8JWF
(credits to ******. Used his testing script.)
Why not just use:

#define SetGVar(%1, %2) setproperty(0, "", makehash(%1), %2)
or
#define SetGVar(%1, %2) setproperty(0, %1, 0, %2)
for example

makehash(adler32 / bernstein /.. but it has to return a value, not a string(MD5)) converts a string into an unique ID value (int).
I haven't really touched setproperty & getproperty and so on, i'll take a look into it more. Thanks for the speedtest whats-o-ever.
Reply
#10

Quote:
Originally Posted by Remis
Посмотреть сообщение
Better not to believe people guesswork without facts. Why don't you just test it yourself?

I was testing its speed and the system seems to be slow.
It loops through the whole array and compares the string with all the strings in the array. It is especially slow if the needed gvar is near the end of the gvar array. Using a binary tree may help for faster search.

Using setproperty & getproperty is more efficient, not only because it is native. Its algorithm is much better.
Even though, if you want to get a nonstring (money for example), you have to unpack it and convert it to a value first, it still faster.


Here are the results (I also edited the .inc defines):

Код:
Setting + Getting + saving in a variable.

10000 Iterations:
Property: 0230
GVar: 20264


100 Iterations:
Property: 0000
GVar: 0019

Setting only (property method needs unpacking and strval() everytime. So the property method is even faster here)


10000 Iterations:
Property: 0219
GVar: 20256


100 Iterations:
Property: 0000
GVar: 0019


Property indexing by names instead of unique values:

Setting + Getting + saving in a variable

I also tested Icognitosґ GVar and it is even faster than the property functions! Use the plugin.


10000 Iterations:
Property: 2113
GVar: 20139


100 Iterations:
Property: 0000
GVar: 0019

The testing script part:
http://pastebin.com/i8cy8JWF
(credits to ******. Used his testing script.)
Why not just use:

#define SetGVar(%1, %2) setproperty(0, "", makehash(%1), %2)
or
#define SetGVar(%1, %2) setproperty(0, %1, 0, %2)
for example

makehash(adler32 / bernstein /.. but it has to return a value, not a string(MD5)) converts a string into an unique ID value (int).
Get/setproperty is far slower than the GVar plugin made by Incognito. Since you provided no code behind your apparently forged numbers, here is real proof in the pudding with code to back it up: http://forum.sa-mp.com/showpost.php?...1&postcount=34

Back then it was me whom Incog was proving wrong. The beauty of GVars (and get/set property) is you have the ability to do loopless lookups of certain array data.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)