Raised This Month: $32 Target: $400
 8% 

[EXTENSION] SHVector with example usage


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
API
Veteran Member
Join Date: May 2006
Old 05-13-2007 , 22:51   [EXTENSION] SHVector with example usage
Reply With Quote #1

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.
Attached Files
File Type: zip shvector_source.zip (10.9 KB, 3364 views)
File Type: zip shvector_1_0_0.zip (56.9 KB, 5651 views)
__________________
API is offline
Send a message via AIM to API
sskillz
Member
Join Date: Apr 2005
Old 05-15-2007 , 13:31   Re: [EXTENSION] SHVector with example usage
Reply With Quote #2

What it could be used for? Can you give some more practical examples?
thanks.
sskillz is offline
API
Veteran Member
Join Date: May 2006
Old 05-15-2007 , 17:05   Re: [EXTENSION] SHVector with example usage
Reply With Quote #3

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.
__________________
API is offline
Send a message via AIM to API
sskillz
Member
Join Date: Apr 2005
Old 05-17-2007 , 17:26   Re: [EXTENSION] SHVector with example usage
Reply With Quote #4

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
}
sskillz is offline
BAILOPAN
Join Date: Jan 2004
Old 05-17-2007 , 20:00   Re: [EXTENSION] SHVector with example usage
Reply With Quote #5

That would defeat the purpose and greatly cut down the speed. You're looking for associative arrays.
__________________
egg
BAILOPAN is offline
sslice
Senior Member
Join Date: Feb 2005
Location: Texas, USA
Old 05-19-2007 , 00:26   Re: [EXTENSION] SHVector with example usage
Reply With Quote #6

Quote:
Originally Posted by sskillz View Post
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
__________________
sslice is offline
sskillz
Member
Join Date: Apr 2005
Old 05-20-2007 , 03:20   Re: [EXTENSION] SHVector with example usage
Reply With Quote #7

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!
sskillz is offline
daniel_syrian
New Member
Join Date: Aug 2008
Old 08-04-2008 , 13:50   Re: [EXTENSION] SHVector with example usage
Reply With Quote #8

r u going to put all in extansions or ?
daniel_syrian is offline
TheNoob
Junior Member
Join Date: Jul 2009
Old 09-10-2009 , 08:08   Re: [EXTENSION] SHVector with example usage
Reply With Quote #9

where to extract the shvector_source.zip?
TheNoob is offline
DJ Tsunami
DJ Post Spammer
Join Date: Feb 2008
Location: The Netherlands
Old 09-10-2009 , 08:25   Re: [EXTENSION] SHVector with example usage
Reply With Quote #10

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.
__________________
Advertisements | REST in Pawn - HTTP client for JSON REST APIs
Please do not PM me with questions. Post in the plugin thread.
DJ Tsunami is offline
Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -4. The time now is 17:23.


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