18.01.2012, 08:26
(
Последний раз редактировалось Slice; 13.06.2012 в 14:09.
)
Hey,
Here's a neat implementation of pointers for PAWN! This include makes it a lot easier to deal with variadic functions (functions that can take a varying number of arguments), but also allows you to, for example, have pointers from large enum structures into other arrays, parts of strings, etc.
One example would be to have an array for player gangs, then use a pointer from a player array into the player's gang (think C structures, sort of).
A quick example:
Here's an example of a function that appends all string arguments to a given string.
Syntax:
Download: pointers.inc
Here's a neat implementation of pointers for PAWN! This include makes it a lot easier to deal with variadic functions (functions that can take a varying number of arguments), but also allows you to, for example, have pointers from large enum structures into other arrays, parts of strings, etc.
One example would be to have an array for player gangs, then use a pointer from a player array into the player's gang (think C structures, sort of).
A quick example:
pawn Код:
new
g_Test[] = {123, 456, 789}
;
public OnGameModeInit() {
new address = GetVariableAddress(g_Test);
// Change the 2nd value
@ptr[address][1] = 444;
// Print out the values
printf("%d, %d, %d", @ptr[address][0], @ptr[address][1], @ptr[address][2]);
}
pawn Код:
public OnGameModeInit() {
new buf[128] = "Lorem";
concat(buf, sizeof(buf), "ipsum", "dolor", "sit", "amet,", "consectetur", "adipiscing.");
// Prints out: Lorem ipsum dolor sit amet, consectetur adipiscing.
print(buf);
}
// Append all strings to szOutput
stock concat(output[], maxsize = sizeof(output), ...) {
new
arg_count = numargs()
;
for (new i = 2; i < arg_count; i++) {
// First add a space
strcat(output, " ", maxsize);
// Then add the string
strcat(output, @arg[i], maxsize);
}
}
pawn Код:
@ptr[address] // This is now a pointer to "address"
@arg[2] // This is now a pointer to the 3rd argument (0 is the first)