Difference between revisions of "DataPacks"

From AlliedModders Wiki
Jump to: navigation, search
m (Update highlighting)
 
(7 intermediate revisions by 3 users not shown)
Line 3: Line 3:
 
=Example of using a DataPack=
 
=Example of using a DataPack=
 
Syntax:
 
Syntax:
<pawn>
+
<sourcepawn>
 
//writing
 
//writing
new Handle:pack = CreateDataPack()
+
DataPack pack = new DataPack();
WritePackCell(pack, 23)
+
pack.WriteCell(23);
WritePackString(pack, "I'm a little teapot.")
+
pack.WriteString("I'm a little teapot.");
ResetPack(pack) //resets the index to the beginning
 
  
 
//reading
 
//reading
new cellValue = ReadPackCell(pack)
+
pack.Reset(); //resets the index to the beginning, necessary for read.
new buffer[1024]
+
int cellValue = pack.ReadCell();
ReadPackString(pack, buffer, 1024)
+
char buffer[1024];
</pawn>
+
pack.ReadString(buffer, 1024);
 +
</sourcepawn>
  
 
=Creating a DataPack=
 
=Creating a DataPack=
 
Creating a DataPack is very simple; all you need is a Handle to write to.
 
Creating a DataPack is very simple; all you need is a Handle to write to.
<pawn>new Handle:dataPackHandle = CreateDataPack();</pawn>
+
<sourcepawn>Datapack dataPackHandle = new DataPack();</sourcepawn>
  
 
For more information on using Handles, see [[Handle API (SourceMod)]].
 
For more information on using Handles, see [[Handle API (SourceMod)]].
Line 27: Line 27:
 
==WritePackCell==
 
==WritePackCell==
 
Syntax:
 
Syntax:
<pawn>native WritePackCell(Handle:pack, cell);
+
<sourcepawn>native void WritePackCell(Handle pack, any cell);
</pawn>
+
</sourcepawn>
  
 
==WritePackFloat==
 
==WritePackFloat==
Line 34: Line 34:
  
 
Syntax:
 
Syntax:
<pawn>native WritePackFloat(Handle:pack, Float:val);
+
<sourcepawn>native void WritePackFloat(Handle pack, float val);
</pawn>
+
</sourcepawn>
  
 
==WritePackString==
 
==WritePackString==
Line 41: Line 41:
  
 
Syntax:
 
Syntax:
<pawn>native WritePackString(Handle:pack, const String:str[]);
+
<sourcepawn>native void WritePackString(Handle pack, const char[] str);
</pawn>
+
</sourcepawn>
  
 
==ReadPackCell==
 
==ReadPackCell==
 
Syntax:
 
Syntax:
<pawn>native ReadPackCell(Handle:pack);
+
<sourcepawn>native any ReadPackCell(Handle pack);
</pawn>
+
</sourcepawn>
  
 
==ReadPackFloat==
 
==ReadPackFloat==
Line 53: Line 53:
  
 
Syntax:
 
Syntax:
<pawn>native Float:ReadPackFloat(Handle:pack);
+
<sourcepawn>native float ReadPackFloat(Handle pack);
</pawn>
+
</sourcepawn>
  
 
==ReadPackString==
 
==ReadPackString==
Line 60: Line 60:
  
 
Syntax:
 
Syntax:
<pawn>native ReadPackString(Handle:pack, String:buffer[], maxlen);
+
<sourcepawn>native void ReadPackString(Handle pack, char[] buffer, maxlen);
</pawn>
+
</sourcepawn>
  
 
==ResetPack==
 
==ResetPack==
Line 67: Line 67:
  
 
Syntax:
 
Syntax:
<pawn>native ResetPack(Handle:pack, bool:clear=false);
+
<sourcepawn>native void ResetPack(Handle pack, bool clear=false);
</pawn>
+
</sourcepawn>
  
 
==GetPackPosition==
 
==GetPackPosition==
Line 74: Line 74:
  
 
Syntax:
 
Syntax:
<pawn>native GetPackPosition(Handle:pack);
+
<sourcepawn>native int GetPackPosition(Handle pack);
</pawn>
+
</sourcepawn>
  
 
==SetPackPosition==
 
==SetPackPosition==
Line 81: Line 81:
  
 
Syntax:
 
Syntax:
<pawn>native SetPackPosition(Handle:pack, position);
+
<sourcepawn>native void SetPackPosition(Handle pack, int position);
</pawn>
+
</sourcepawn>
  
 
=Disposing of a DataPack=
 
=Disposing of a DataPack=
Line 88: Line 88:
  
 
Example:
 
Example:
<pawn>CloseHandle(dataPackHandle);
+
<sourcepawn>CloseHandle(dataPackHandle);
</pawn>
+
</sourcepawn>
  
 
[[Category:SourceMod Scripting]]
 
[[Category:SourceMod Scripting]]

Latest revision as of 18:58, 29 March 2020

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
DataPack pack = new DataPack();
pack.WriteCell(23);
pack.WriteString("I'm a little teapot.");
 
//reading
pack.Reset(); //resets the index to the beginning, necessary for read.
int cellValue = pack.ReadCell();
char buffer[1024];
pack.ReadString(buffer, 1024);

Creating a DataPack

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

Datapack dataPackHandle = new DataPack();

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 void WritePackCell(Handle pack, any cell);

WritePackFloat

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

Syntax:

native void WritePackFloat(Handle pack, float val);

WritePackString

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

Syntax:

native void WritePackString(Handle pack, const char[] str);

ReadPackCell

Syntax:

native any 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 void ReadPackString(Handle pack, char[] buffer, maxlen);

ResetPack

This function resets your position in the DataPack.

Syntax:

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

GetPackPosition

This function gets your current position in the DataPack.

Syntax:

native int GetPackPosition(Handle pack);

SetPackPosition

This function sets your current position in the DataPack.

Syntax:

native void SetPackPosition(Handle pack, int position);

Disposing of a DataPack

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

Example:

CloseHandle(dataPackHandle);