04.01.2018, 17:06
(
Последний раз редактировалось kristo; 04.01.2018 в 17:48.
)
Variadic functions in PAWN
Introduction
Variadic functions are a feature in most scripting languages, where a scripter can specify an indefinite amount of arguments towards a function. However, in PAWN, this isn't possible without playing around with the assembly (later versions of PAWN have dealt with this), but this include introduces a brand new way of handling them!
The include was originally created by Emmet_ and is now maintained by me. Most of the credits for the include still go to Emmet_, I have only added support for mysql_format and done a few other changes.
More information
Have you ever wrote a function like this before?
And then you figured out that you can't do it this way! The reason being is because variadic parameters don't have a referable location in ordinary PAWN code, so assembly is required, but that can be very complicated and one mistake could corrupt the entire script.
However, this include allows scripters to dynamically push arguments and format them. The pushed arguments are stored within an array by address, so they can be accessed later with the corresponding functions.
Example
An equivalent of the function in the introduction is possible using this include:
You can also use @variadic[] with CallLocalFunction and such to call local functions with the corresponding pushed arguments.
Alternatively, you can also use another keyword (or none) instead of var, but make sure you place ... after the parameters.
String arguments
You can retrieve string arguments easily.
Getting:
Setting:
Functions
Installation
Simply install to your project:
Include in your code and begin using the library:
Credits
Download
https://github.com/kristoisberg/variadic
Introduction
Variadic functions are a feature in most scripting languages, where a scripter can specify an indefinite amount of arguments towards a function. However, in PAWN, this isn't possible without playing around with the assembly (later versions of PAWN have dealt with this), but this include introduces a brand new way of handling them!
The include was originally created by Emmet_ and is now maintained by me. Most of the credits for the include still go to Emmet_, I have only added support for mysql_format and done a few other changes.
More information
Have you ever wrote a function like this before?
PHP код:
printex(str[], ...)
{
new output[128];
format(output, sizeof(output), str, ...);
return print(output);
}
However, this include allows scripters to dynamically push arguments and format them. The pushed arguments are stored within an array by address, so they can be accessed later with the corresponding functions.
Example
An equivalent of the function in the introduction is possible using this include:
PHP код:
#include <a_samp>
#include <variadic>
var printex(str[])
{
new output[128];
format(output, sizeof(output), str, @variadic[1]);
return print(output);
}
Alternatively, you can also use another keyword (or none) instead of var, but make sure you place ... after the parameters.
String arguments
You can retrieve string arguments easily.
Getting:
PHP код:
MyFunction(a[], b[])
{
new addr = GetVariadicAddress(0); // Address of "a[]".
print(GetVariadicString(addr)); // Print the result.
}
PHP код:
MyFunction(a[], b[])
{
new addr = GetVariadicAddress(1); // Address of "b[]".
SetVariadicString(addr, "Hi there.");
print(GetVariadicString(addr)); // Print the result.
}
PHP код:
// Get the address of a function argument.
native GetVariadicAddress(argument);
// Get the string from the specified argument address. You can return it or store it inside "dest".
native GetVariadicString(address, dest[] = "", size = sizeof(dest));
// Set the string from the specified argument address to the specified string.
native SetVariadicString(address, str[]);
// Checks if an argument is packed.
native IsArgumentPacked(address);
// Gets the length of a variadic string.
native GetVariadicLength(address);
// Delete an argument from the specified argument address.
native DeleteArgument(address);
// Popped the pushed arguments.
native PopArguments();
Simply install to your project:
Код:
sampctl package install kristoisberg/variadic
PHP код:
#include <variadic>
- Emmet_ - The original creator of the include, an absolute legend.
- kvann - Current maintainer.
- Southclaws - Added sampctl support, some minor changes.
Download
https://github.com/kristoisberg/variadic