AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Extensions (https://forums.alliedmods.net/forumdisplay.php?f=134)
-   -   [EXTENSION] SHVector with example usage (https://forums.alliedmods.net/showthread.php?t=55110)

API 05-13-2007 22:51

[EXTENSION] SHVector with example usage
 
2 Attachment(s)
Hey guys,

SHVector is an extension I made to expose the CVector class to SourceMod. Basically, a vector is like an array of data, without a set size and without many datatype limitations. You can have handles, floats, ints, and bools if you use the TYPE_CELL vector type, and there is TYPE_STRING for a string vector.

Credit
PimpinJuice, BAILOPAN, and everyone in IRC that kept me entertained.

Installation
1. Extract 'shvector.ext.dll' and 'shvector.ext.so' to the 'addons/sourcemod/extensions' folder.
2. Extract 'shvector.inc' to the 'addons/sourcemod/scripting/include' folder.

Functions
There are 19 different functions that you can use in your plugins.
They are documented and called as follows.
Code:

/**
 * Creates a SHVector
 * @param type      The type of vector to create (TYPE_CELL or TYPE_STRING)
 * @return          The SHVector handle of the result
 */
native Handle:SHVectorCreate(type=TYPE_CELL);

/**
 * Frees the vector from the memory
 * @param vector    The vector to free from the memory
 * @noreturn
 */
native SHVectorFree_Cell(Handle:vector);
native SHVectorFree_String(Handle:vector);

/**
 * Gets the vector value at the provided position
 * @param vector    The vector to use
 * @param pos      The position in the vector to get the value of
 * @return          The value
 */
native any:SHVectorAt_Cell(Handle:vector,pos);
native SHVectorAt_String(Handle:vector,pos,String:storeto[],maxlen);

/**
 * Sets the vector value at the provided position
 * @param vector    The vector to use
 * @param pos      The position in the vector to set the value of
 * @param value    The value to set as
 * @return          Returns 0 if there was a failure
 */
native SHVectorSetAt_Cell(Handle:vector,pos,any:value);
native SHVectorSetAt_String(Handle:vector,pos,String:value[]);

/**
 * Clear the whole vector provided
 * @param vector    The vector to use
 * @noreturn
 */
native SHVectorClear_Cell(Handle:vector);
native SHVectorClear_String(Handle:vector);

/**
 * Is the vector empty?
 * @param vector    The vector to use
 * @return          Returns true if the vector is empty
 */
native bool:SHVectorIsEmpty_Cell(Handle:vector);
native bool:SHVectorIsEmpty_String(Handle:vector);

/**
 * Swap the contents of two pos's in a vector
 * @param vector  The Vector to use
 * @param pos1    One of the pos's
 * @param pos2    The other pos 
 * @return        Returns 0 if there was a failure
 */
native SHVectorSwap_Cell(Handle:vector,pos1,pos2);
native SHVectorSwap_String(Handle:vector,pos1,pos2);

/**
 * Insert a new item into a vector
 * @param vector    The vector to use
 * @param where    Where to put the item (ITER_FRONT or ITER_BACK)
 * @param value    The value to insert
 * @return          Returns 0 if there was a failure
 */
native SHVectorInsert_Cell(Handle:vector,where,any:value);
native SHVectorInsert_String(Handle:vector,where,String:value[]);

/**
 * Erase an item from the vector
 * @param vector    The vector to use
 * @param pos      The position to delete from the vector
 * @return          Returns 0 if there was a failure
 */
native SHVectorErase_Cell(Handle:vector,pos);
native SHVectorErase_String(Handle:vector,pos);

/**
 * The amount of items in the vector
 * @param vector    The vector to use
 * @return          Returns the amount of items in the vector
 */
native SHVectorSize_Cell(Handle:vector);
native SHVectorSize_String(Handle:vector);

Here is a small example on usage of the functions, although it is not showing everything.

Code:

#include <sourcemod>
#include <shvector>

public Plugin:myinfo =
{
        name = "SHVector Test",
        author = "PimpinJuice",
        description = "Testing out the SHVector extension",
        version = "1.0.0.0",
        url = "http://pimpinjuice.net/"
};

public OnPluginStart()
{
    // Create a vector
    new Handle:shVec=SHVectorCreate(TYPE_CELL); // supports int,float,bool, and handle
    SHVectorInsert_Cell(shVec,ITER_BACK,2.5); // Put 2.5 at the back of the vector, size should now be 1.
    PrintToServer("The size of the vector is now %d",SHVectorSize_Cell(shVec));
    // Lets add an int and a bool
    SHVectorInsert_Cell(shVec,ITER_BACK,5);
    SHVectorInsert_Cell(shVec,ITER_BACK,true);
    // Right now the vector reads 2.5,5,true, in that order, but I want it to say true,5,2.5
    SHVectorSwap_Cell(shVec,0,2);
    // Lets see our full vector and our size
    PrintToServer("size: %d\n0 == %d\n1 == %d\n2 == %f",SHVectorSize_Cell(shVec),SHVectorAt_Cell(shVec,0),SHVectorAt_Cell(shVec,1),SHVectorAt_Cell(shVec,2));
    // Lets give our system the memory back
    SHVectorFree_Cell(shVec);
}

The output, as expected, will follow as:
Quote:

The size of the vector is now 1
size: 3
0 == 1
1 == 5
2 == 2.500000
Note: I have included the source code, feel free to modify and take code from it, but credit me.

Have fun. :)

sskillz 05-15-2007 13:31

Re: [EXTENSION] SHVector with example usage
 
What it could be used for? Can you give some more practical examples?
thanks.

API 05-15-2007 17:05

Re: [EXTENSION] SHVector with example usage
 
Well, also here: http://forums.alliedmods.net/showthread.php?t=55114
It can be used for any type of data storage, my favorite part is the fact that you can mix multiple datatypes into one handle, and get the information with one call. Also, for strings you don't need to define string size and whatnot, in my War3Source plugin I can pull some code from it when I get home and show you.

sskillz 05-17-2007 17:26

Re: [EXTENSION] SHVector with example usage
 
Its very nice, Maybe you should make it so you can specify a alias for a cell like this:
if(client>0)
{
new Handle:newPlayer=SHVectorCreate(TYPE_CELL);
SHVectorInsert_Cell(newPlayer,ITER_BACK,"Inde x",client); // The first thing is client index
SHVectorInsert_Cell(newPlayer,ITER_BACK,"XP", 0); // Place-holder for XP points
SHVectorInsert_Cell(newPlayer,ITER_BACK,"Leve l",0); // Place-holder for current level
SHVectorInsert_Cell(vecPlayers,ITER_BACK,newP layer); // Put our new player at the end of the vecPlayers vector
}

BAILOPAN 05-17-2007 20:00

Re: [EXTENSION] SHVector with example usage
 
That would defeat the purpose and greatly cut down the speed. You're looking for associative arrays.

sslice 05-19-2007 00:26

Re: [EXTENSION] SHVector with example usage
 
Quote:

Originally Posted by sskillz (Post 477760)
Its very nice, Maybe you should make it so you can specify a alias for a cell like this:
if(client>0)
{
new Handle:newPlayer=SHVectorCreate(TYPE_CELL);
SHVectorInsert_Cell(newPlayer,ITER_BACK,"Inde x",client); // The first thing is client index
SHVectorInsert_Cell(newPlayer,ITER_BACK,"XP", 0); // Place-holder for XP points
SHVectorInsert_Cell(newPlayer,ITER_BACK,"Leve l",0); // Place-holder for current level
SHVectorInsert_Cell(vecPlayers,ITER_BACK,newP layer); // Put our new player at the end of the vecPlayers vector
}

You can just use KeyValues for this. http://wiki.alliedmods.net/KeyValues...d_Scripting%29

sskillz 05-20-2007 03:20

Re: [EXTENSION] SHVector with example usage
 
Quote:

You can just use KeyValues for this. http://wiki.alliedmods.net/KeyValues...d_Scripting%29
Yes just he isnt using a file which is much slower :|
Great extension anyway!

daniel_syrian 08-04-2008 13:50

Re: [EXTENSION] SHVector with example usage
 
r u going to put all in extansions or ?

TheNoob 09-10-2009 08:08

Re: [EXTENSION] SHVector with example usage
 
where to extract the shvector_source.zip?

DJ Tsunami 09-10-2009 08:25

Re: [EXTENSION] SHVector with example usage
 
You don't, that's for if you want to edit the extension. shvector_1_0_0.zip is for when you want to run it.


All times are GMT -4. The time now is 22:19.

Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.