Difference between revisions of "DataPacks"

From AlliedModders Wiki
Jump to: navigation, search
(New page: DataPacks are a way to store and move around various types of data in SourceMod Scripting. Since some things are not possible in SourcePawn, such as a fun...)
 
Line 1: Line 1:
 
DataPacks are a way to store and move around various types of data in [[:Category:SourceMod Scripting|SourceMod Scripting]]. Since some things are not possible in SourcePawn, such as a function consuming a String, DataPacks help us get these Strings and other items where they need to go.
 
DataPacks are a way to store and move around various types of data in [[:Category:SourceMod Scripting|SourceMod Scripting]]. Since some things are not possible in SourcePawn, such as a function consuming a String, DataPacks help us get these Strings and other items where they need to go.
 +
 +
=Example of using a DataPack=
 +
Syntax:
 +
<pawn>
 +
//writing
 +
new Handle:pack = CreateDataPack()
 +
WritePackCell(pack, 23)
 +
WritePackString(pack, "I'm a little teapot.")
 +
ResetPack(pack) //resets the index to the beginning
 +
 +
//reading
 +
new cellValue = ReadPackCell(pack)
 +
new buffer[1024]
 +
ReadPackString(pack, buffer, 1024)
 +
</pawn>
  
 
=Creating a DataPack=
 
=Creating a DataPack=

Revision as of 11:48, 15 September 2010

DataPacks are a way to store and move around various types of data in SourceMod Scripting. Since some things are not possible in SourcePawn, such as a function consuming a String, DataPacks help us get these Strings and other items where they need to go.

Example of using a DataPack

Syntax:

//writing
new Handle:pack = CreateDataPack()
WritePackCell(pack, 23)
WritePackString(pack, "I'm a little teapot.")
ResetPack(pack) //resets the index to the beginning
 
//reading
new cellValue = ReadPackCell(pack)
new buffer[1024]
ReadPackString(pack, buffer, 1024)

Creating a DataPack

Creating a DataPack is very simple; all you need is a Handle to write to.

new Handle:dataPackHandle = CreateDataPack();

For more information on using Handles, see Handle API (SourceMod).

DataPack Functions

On you have created your DataPack, you can use a variety of functions to manage the DataPack.

WritePackCell

Syntax:

native WritePackCell(Handle:pack, cell);

WritePackFloat

This function can be used to write a Float to a DataPack.

Syntax:

native WritePackFloat(Handle:pack, Float:val);

WritePackString

This function can be used to write a String to a DataPack.

Syntax:

native WritePackString(Handle:pack, const String:str[]);

ReadPackCell

Syntax:

native ReadPackCell(Handle:pack);

ReadPackFloat

This function can be used to read a Float from a DataPack.

Syntax:

native Float:ReadPackFloat(Handle:pack);

ReadPackString

This function can be used to read a String from a DataPack.

Syntax:

native ReadPackString(Handle:pack, String:buffer[], maxlen);

ResetPack

This function resets your position in the DataPack.

Syntax:

native ResetPack(Handle:pack, bool:clear=false);

GetPackPosition

This function gets your current position in the DataPack.

Syntax:

native GetPackPosition(Handle:pack);

SetPackPosition

This function sets your current position in the DataPack.

Syntax:

native SetPackPosition(Handle:pack, position);

Disposing of a DataPack

To dispose of a DataPack, all you have to do is close its Handle.

Example:

CloseHandle(dataPackHandle);