Difference between revisions of "DataPacks (AMX Mod X Scripting)"
(Added DataPack documentation) |
(Adjusted few things) |
||
Line 19: | Line 19: | ||
::{{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}}: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. | ||
Line 33: | Line 33: | ||
=== Resetting === | === Resetting === | ||
− | :{{margin|20px 0 15px 0|{{tt|ResetPack({{color|green|DataPack}}:pack, {{color|green|bool}}: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. | ||
Line 47: | Line 47: | ||
=== Writing data === | === Writing data === | ||
− | :{{margin|20px 0 15px 0|{{tt|WritePackCell({{color|green|DataPack}}:pack, {{color|green|any}}: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}}:pack, {{color|green|Float}}: | + | :{{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}}:pack, const 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. | ||
Line 65: | Line 65: | ||
=== Reading data === | === Reading data === | ||
− | :{{margin|20px 0 15px 0|{{tt|{{color|green|bool}}:IsPackEnded({{color|green|DataPack}}: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. | ||
Line 71: | Line 71: | ||
::{{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}}: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. | ||
Line 78: | Line 78: | ||
::{{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}}: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. | ||
Line 85: | Line 85: | ||
::{{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}}:pack, buffer[], 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. | ||
Line 94: | Line 94: | ||
=== Managing position === | === Managing position === | ||
− | :{{margin|20px 0 15px 0|{{tt|{{color|green|DataPackPos}}:GetPackPosition({{color|green|DataPack}}: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. | ||
Line 100: | Line 100: | ||
::{{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}}:pack, {{color|green|DataPackPos}}: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. | ||
Revision as of 14:11, 17 June 2017
Language: | English |
---|
Contents
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); }