Difference between revisions of "DataPacks (AMX Mod X Scripting)"

From AlliedModders Wiki
Jump to: navigation, search
(Adjusted few things)
m
Line 12: Line 12:
 
=== Creating & Disposing ===
 
=== Creating & Disposing ===
  
:{{margin|20px 0 15px 0|{{tt|{{color|green|DataPack}}:CreateDataPack()|opt=large}}}}
+
{{margin|20px 0 15px 0|{{tt|{{color|green|DataPack}}:CreateDataPack()|opt=large}}}}
:: Creates a new datapack.
+
: Creates a new datapack.
  
::{{alert|info|Return:|New datapack handle, which must be freed via {{tt|DestroyDataPack|opt=nobg}}.|opt=full-border}}
+
:{{alert|info|Return:|New datapack handle, which must be freed via {{tt|DestroyDataPack|opt=nobg}}.|opt=full-border}}
::{{alert|danger|Important:|Plugins are responsible for freeing all datapack handles they acquire. Failing to free handles will result in the plugin and AMXX leaking memory.|opt=full-border}}
+
:{{alert|danger|Important:|Plugins are responsible for freeing all datapack handles they acquire. Failing to free handles will result in the plugin and AMXX leaking memory.|opt=full-border}}
::{{alert|success|Example:|{{tt|new const {{color|green|DataPack}}:handle <nowiki>=</nowiki> CreateDataPack();|opt=nobg}}|opt=full-border}}
+
:{{alert|success|Example:|{{tt|new const {{color|green|DataPack}}:handle <nowiki>=</nowiki> CreateDataPack();|opt=nobg}}|opt=full-border}}
  
:{{margin|35px 0 15px 0|{{tt|DestroyDataPack(&{{color|green|DataPack}}:{{h:title||pack}})|opt=large}}}}
+
{{margin|35px 0 15px 0|{{tt|DestroyDataPack(&{{color|green|DataPack}}:{{h:title||pack}})|opt=large}}}}
::Destroys the datapack and frees its memory.
+
:Destroys the datapack and frees its memory.
  
::{|
+
:{|
 
|-
 
|-
 
| {{tt|pack}} || {{ns}} Datapack handle to free.
 
| {{tt|pack}} || {{ns}} Datapack handle to free.
Line 28: Line 28:
 
|}
 
|}
  
::{{alert|info|Note:|{{tt|pack|opt=nobg}} will be reset to 0.|opt=full-border}}
+
:{{alert|info|Note:|{{tt|pack|opt=nobg}} will be reset to 0.|opt=full-border}}
::{{alert|success|Example:|{{tt|DestroyDataPack(pack);|opt=nobg}}|opt=full-border}}
+
:{{alert|success|Example:|{{tt|DestroyDataPack(pack);|opt=nobg}}|opt=full-border}}
  
 
=== Resetting ===
 
=== Resetting ===
  
:{{margin|20px 0 15px 0|{{tt|ResetPack({{color|green|DataPack}}:{{h:title||pack}}, {{color|green|bool}}:{{h:title||clear}} <nowiki>=</nowiki> false)|opt=large}}}}
+
{{margin|20px 0 15px 0|{{tt|ResetPack({{color|green|DataPack}}:{{h:title||pack}}, {{color|green|bool}}:{{h:title||clear}} <nowiki>=</nowiki> false)|opt=large}}}}
::Resets the datapack read/write position to the start.
+
:Resets the datapack read/write position to the start.
  
::{|
+
:{|
 
|-
 
|-
 
| {{tt|clear}} || {{ns}} If true, clears the contained data.
 
| {{tt|clear}} || {{ns}} If true, clears the contained data.
Line 42: Line 42:
 
|}
 
|}
  
::{{alert|warning|Attention:|You need to reset the datapack before reading data.|opt=full-border}}
+
:{{alert|warning|Attention:|You need to reset the datapack before reading data.|opt=full-border}}
::{{alert|success|Example:|{{tt|ResetPack(pack);|opt=nobg}}|opt=full-border}}
+
:{{alert|success|Example:|{{tt|ResetPack(pack);|opt=nobg}}|opt=full-border}}
  
 
=== Writing data ===
 
=== Writing data ===
  
:{{margin|20px 0 15px 0|{{tt|WritePackCell({{color|green|DataPack}}:{{h:title||pack}}, {{color|green|any}}:{{h:title||cell}})|opt=large}}}}
+
{{margin|20px 0 15px 0|{{tt|WritePackCell({{color|green|DataPack}}:{{h:title||pack}}, {{color|green|any}}:{{h:title||cell}})|opt=large}}}}
:: Packs a cell value into a datapack.
+
: Packs a cell value into a datapack.
  
::{{alert|success|Example:|{{tt|WritePackString(pack, 42);|opt=nobg}}|opt=full-border}}
+
:{{alert|success|Example:|{{tt|WritePackString(pack, 42);|opt=nobg}}|opt=full-border}}
  
:{{margin|35px 0 15px 0|{{tt|WritePackFloat({{color|green|DataPack}}:{{h:title||pack}}, {{color|green|Float}}:{{h:title||val}})|opt=large}}}}
+
{{margin|35px 0 15px 0|{{tt|WritePackFloat({{color|green|DataPack}}:{{h:title||pack}}, {{color|green|Float}}:{{h:title||val}})|opt=large}}}}
:: Packs a float value into a datapack.
+
: Packs a float value into a datapack.
  
::{{alert|success|Example:|{{tt|WritePackString(pack, 13.36);|opt=nobg}}|opt=full-border}}
+
:{{alert|success|Example:|{{tt|WritePackString(pack, 13.36);|opt=nobg}}|opt=full-border}}
  
:{{margin|35px 0 15px 0|{{tt|WritePackString({{color|green|DataPack}}:{{h:title||pack}}, const {{h:title||str}}[])|opt=large}}}}
+
{{margin|35px 0 15px 0|{{tt|WritePackString({{color|green|DataPack}}:{{h:title||pack}}, const {{h:title||str}}[])|opt=large}}}}
:: Packs a string value into a datapack.
+
: Packs a string value into a datapack.
  
::{{alert|info|Return:|Length of copied string.|opt=full-border}}
+
:{{alert|info|Return:|Length of copied string.|opt=full-border}}
::{{alert|success|Example:|{{tt|new const length <nowiki>=</nowiki> WritePackString(pack, "Hello world!");|opt=nobg}}|opt=full-border}}
+
:{{alert|success|Example:|{{tt|new const length <nowiki>=</nowiki> WritePackString(pack, "Hello world!");|opt=nobg}}|opt=full-border}}
  
 
=== Reading data ===
 
=== Reading data ===
  
:{{margin|20px 0 15px 0|{{tt|{{color|green|bool}}:IsPackEnded({{color|green|DataPack}}:{{h:title||pack}})|opt=large}}}}
+
{{margin|20px 0 15px 0|{{tt|{{color|green|bool}}:IsPackEnded({{color|green|DataPack}}:{{h:title||pack}})|opt=large}}}}
:: Returns if the datapack has reached its end and no more data can be read.
+
: Returns if the datapack has reached its end and no more data can be read.
  
::{{alert|info|Return:|True if datapack has reached the end, false otherwise.|opt=full-border}}
+
:{{alert|info|Return:|True if datapack has reached the end, false otherwise.|opt=full-border}}
::{{alert|success|Example:|{{tt|if (IsPackEnded(pack)) {}|opt=nobg}}|opt=full-border}}
+
:{{alert|success|Example:|{{tt|if (IsPackEnded(pack)) {}|opt=nobg}}|opt=full-border}}
  
:{{margin|20px 0 15px 0|{{tt|{{color|green|any}}:ReadPackCell({{color|green|DataPack}}:{{h:title||pack}})|opt=large}}}}
+
{{margin|20px 0 15px 0|{{tt|{{color|green|any}}:ReadPackCell({{color|green|DataPack}}:{{h:title||pack}})|opt=large}}}}
:: Reads a cell from a Datapack.
+
: Reads a cell from a Datapack.
  
::{{alert|warning|Attention:|You need to reset the datapack before reading data.|opt=full-border}}
+
:{{alert|warning|Attention:|You need to reset the datapack before reading data.|opt=full-border}}
::{{alert|info|Return:|Cell value.|opt=full-border}}
+
:{{alert|info|Return:|Cell value.|opt=full-border}}
::{{alert|success|Example:|{{tt|new value <nowiki>=</nowiki> ReadPackString(pack);|opt=nobg}}|opt=full-border}}
+
:{{alert|success|Example:|{{tt|new value <nowiki>=</nowiki> ReadPackString(pack);|opt=nobg}}|opt=full-border}}
  
:{{margin|35px 0 15px 0|{{tt|{{color|green|Float}}:ReadPackFloat({{color|green|DataPack}}:{{h:title||pack}})|opt=large}}}}
+
{{margin|35px 0 15px 0|{{tt|{{color|green|Float}}:ReadPackFloat({{color|green|DataPack}}:{{h:title||pack}})|opt=large}}}}
:: Reads a float from a Datapack.
+
: Reads a float from a Datapack.
  
::{{alert|warning|Attention:|You need to reset the datapack before reading data.|opt=full-border}}
+
:{{alert|warning|Attention:|You need to reset the datapack before reading data.|opt=full-border}}
::{{alert|info|Return:|Float value.|opt=full-border}}
+
:{{alert|info|Return:|Float value.|opt=full-border}}
::{{alert|success|Example:|{{tt|new {{color|green|Float}}:value <nowiki>=</nowiki> ReadPackString(pack);|opt=nobg}}|opt=full-border}}
+
:{{alert|success|Example:|{{tt|new {{color|green|Float}}:value <nowiki>=</nowiki> ReadPackString(pack);|opt=nobg}}|opt=full-border}}
  
 
:{{margin|35px 0 15px 0|{{tt|ReadPackString({{color|green|DataPack}}:{{h:title||pack}}, {{h:title||buffer}}[], {{h:title||maxlen}})|opt=large}}}}
 
:{{margin|35px 0 15px 0|{{tt|ReadPackString({{color|green|DataPack}}:{{h:title||pack}}, {{h:title||buffer}}[], {{h:title||maxlen}})|opt=large}}}}
:: Reads a string from a Datapack.
+
: Reads a string from a Datapack.
  
::{{alert|warning|Attention:|You need to reset the datapack before reading data.|opt=full-border}}
+
:{{alert|warning|Attention:|You need to reset the datapack before reading data.|opt=full-border}}
::{{alert|info|Return:|Number of cells written to buffer.|opt=full-border}}
+
:{{alert|info|Return:|Number of cells written to buffer.|opt=full-border}}
::{{alert|success|Example:|{{tt|new buffer[32];<br/>new const length <nowiki>=</nowiki> ReadPackString(pack, buffer, charsmax(buffer));|opt=nobg}}|opt=full-border}}
+
:{{alert|success|Example:|{{tt|new buffer[32];<br/>new const length <nowiki>=</nowiki> ReadPackString(pack, buffer, charsmax(buffer));|opt=nobg}}|opt=full-border}}
  
 
=== Managing position ===
 
=== Managing position ===
  
:{{margin|20px 0 15px 0|{{tt|{{color|green|DataPackPos}}:GetPackPosition({{color|green|DataPack}}:{{h:title||pack}})|opt=large}}}}
+
{{margin|20px 0 15px 0|{{tt|{{color|green|DataPackPos}}:GetPackPosition({{color|green|DataPack}}:{{h:title||pack}})|opt=large}}}}
:: Returns the datapack read/write position.
+
: Returns the datapack read/write position.
  
::{{alert|info|Return:|Position in the datapack, only usable with calls to {{tt|SetPackPosition|opt=nobg}}.|opt=full-border}}
+
:{{alert|info|Return:|Position in the datapack, only usable with calls to {{tt|SetPackPosition|opt=nobg}}.|opt=full-border}}
::{{alert|success|Example:|{{tt|new const {{color|green|DataPackPos}}:position <nowiki>=</nowiki> GetPackPosition(pack);|opt=nobg}}|opt=full-border}}
+
:{{alert|success|Example:|{{tt|new const {{color|green|DataPackPos}}:position <nowiki>=</nowiki> GetPackPosition(pack);|opt=nobg}}|opt=full-border}}
  
:{{margin|20px 0 15px 0|{{tt|SetPackPosition({{color|green|DataPack}}:{{h:title||pack}}, {{color|green|DataPackPos}}:{{h:title||position}})|opt=large}}}}
+
{{margin|20px 0 15px 0|{{tt|SetPackPosition({{color|green|DataPack}}:{{h:title||pack}}, {{color|green|DataPackPos}}:{{h:title||position}})|opt=large}}}}
:: Sets the datapack read/write position.
+
: Sets the datapack read/write position.
  
::{{alert|warning|Attention:|This should only ever be used with (known to be valid) positions returned by {{tt|GetPackPosition|opt=nobg}}. It is not possible for plugins to safely compute datapack positions.|opt=full-border}}
+
:{{alert|warning|Attention:|This should only ever be used with (known to be valid) positions returned by {{tt|GetPackPosition|opt=nobg}}. It is not possible for plugins to safely compute datapack positions.|opt=full-border}}
::{{alert|success|Example:|{{tt|SetPackPosition(pack, position);|opt=nobg}}|opt=full-border}}
+
:{{alert|success|Example:|{{tt|SetPackPosition(pack, position);|opt=nobg}}|opt=full-border}}
  
 
== Example ==
 
== Example ==

Revision as of 16:55, 17 June 2017

Language: English


About

Datapacks provide a way to store and move around arbitrary amounts (and types) of data in AMX Mox X, available from datapack.inc.
Data is packed into a single cell value - the DataPack handle. This handle can be passed around more easily, can be returned by functions and can simulate advanced concepts like string consummation.

Usage

Creating & Disposing

DataPack:CreateDataPack()
Creates a new datapack.
Return:New datapack handle, which must be freed via DestroyDataPack.
Important:Plugins are responsible for freeing all datapack handles they acquire. Failing to free handles will result in the plugin and AMXX leaking memory.
Example:new const DataPack:handle = CreateDataPack();
DestroyDataPack(&DataPack:pack)
Destroys the datapack and frees its memory.
pack     Datapack handle to free.
Note:pack will be reset to 0.
Example:DestroyDataPack(pack);

Resetting

ResetPack(DataPack:pack, bool:clear = false)
Resets the datapack read/write position to the start.
clear     If true, clears the contained data.
Attention:You need to reset the datapack before reading data.
Example:ResetPack(pack);

Writing data

WritePackCell(DataPack:pack, any:cell)
Packs a cell value into a datapack.
Example:WritePackString(pack, 42);
WritePackFloat(DataPack:pack, Float:val)
Packs a float value into a datapack.
Example:WritePackString(pack, 13.36);
WritePackString(DataPack:pack, const str[])
Packs a string value into a datapack.
Return:Length of copied string.
Example:new const length = WritePackString(pack, "Hello world!");

Reading data

bool:IsPackEnded(DataPack:pack)
Returns if the datapack has reached its end and no more data can be read.
Return:True if datapack has reached the end, false otherwise.
Example:if (IsPackEnded(pack)) {}
any:ReadPackCell(DataPack:pack)
Reads a cell from a Datapack.
Attention:You need to reset the datapack before reading data.
Return:Cell value.
Example:new value = ReadPackString(pack);
Float:ReadPackFloat(DataPack:pack)
Reads a float from a Datapack.
Attention:You need to reset the datapack before reading data.
Return:Float value.
Example:new Float:value = ReadPackString(pack);
ReadPackString(DataPack:pack, buffer[], maxlen)
Reads a string from a Datapack.
Attention:You need to reset the datapack before reading data.
Return:Number of cells written to buffer.
Example:new buffer[32];
new const length = ReadPackString(pack, buffer, charsmax(buffer));

Managing position

DataPackPos:GetPackPosition(DataPack:pack)
Returns the datapack read/write position.
Return:Position in the datapack, only usable with calls to SetPackPosition.
Example:new const DataPackPos:position = GetPackPosition(pack);
SetPackPosition(DataPack:pack, DataPackPos:position)
Sets the datapack read/write position.
Attention:This should only ever be used with (known to be valid) positions returned by GetPackPosition. It is not possible for plugins to safely compute datapack positions.
Example:SetPackPosition(pack, position);

Example

foo()
{
    // Creating
    new const DataPack:pack = CreateDataPack();
 
    // Writing
    WritePackCell(pack, refCell);
    WritePackFloat(pack, refFloat);
    WritePackString(pack, refString);
 
    // Reset before reading
    ResetPack(pack);
    server_print("Datapack is readable: %s", !IsPackEnded(pack) ? "yes" : "no");
 
    // Reading
    new cellValue = ReadPackCell(pack);
    new Float:floatValue = ReadPackFloat(pack);
    new buffer[32];
    ReadPackString(pack, buffer, charsmax(buffer));
    server_print("cellValue = %d, floatValue = %f, buffer = %s", cellValue, floatValue, buffer)
    server_print("Datapack is no more readable: %s", IsPackEnded(pack) ? "yes" : "no");
 
    // Clear all data
    ResetPack(pack, .clear = true);
    server_print("Datapack is empty: %s", IsPackEnded(pack) ? "yes" : "no");
 
    // Disposing
    DestroyDataPack(pack);
}