PHP код:
#define MESSAGE_QUEUE_SIZE (10) // Number of last messages that will be stored and listed in dialog
#define MESSAGE_MAX_LENGTH (128) // Max. length of one messsage
new messages[ MESSAGE_QUEUE_SIZE ][ MESSAGE_MAX_LENGTH ];
new messagePointer = 0; // Current pointer in "messages" where new message will be stored
CMD:addtext( playerid, args ) {
// TODO message validation (var "args")
format( messages[ messagePointer ], MESSAGE_MAX_LENGTH, args ); //Store the message
messagePointer++; // Set the pointer to the next index of the array so the next message will be written there
if ( messagePointer == MESSAGE_QUEUE_SIZE ) { // If pointer reached the end of the array
messagePointer = 0; // Go to start of the array
}
}
CMD:seetext( playerid, args ) {
new buffer[ MESSAGE_QUEUE_SIZE * MESSAGE_MAX_LENGTH ]; // We'll store our dialog text here
// Add messages before pointer in descending order
for ( new i=messagePointer; i>-1; i-- ) {
if ( strlen( messages[ i ] ) != 0 ) {
strcat( buffer, messages[ i ] );
strcat( buffer, "\n" );
}
}
// Add messages after pointer in descending order
if ( messagePointer != MESSAGE_QUEUE_SIZE - 1 ) {
for ( new i=MESSAGE_QUEUE_SIZE - 1; i>messagePointer; i-- ) {
if ( strlen( messages[ i ] ) != 0 ) {
strcat( buffer, messages[ i ] );
strcat( buffer, "\n" );
}
}
}
// Show the dialog
ShowPlayerDialog( playerid, DIALOG_SOMETHING, DIALOG_STYLE_..., "Messages", buffer, "Close", "" );
}
It works like that (example):
- Player calls /addtext and his message is stored into messages[ 0 ], pointer is 1 now
- Another player calls /addtext and his message is stored into messages[ 1 ], pointer is 2 now
- ...
- Another player calls /addtext and his message is stored into messages[ 9 ], pointer is 0 now
- Another player calls /addtext and his message is stored into messages[ 0 ], pointer is 1 now // Rewrite the oldest message
- Another player calls /addtext and his message is stored into messages[ 1 ], pointer is 2 now
- Someone call /seetext, the script concatenates these messages (2, 1, 0, 9, 8, 7, 6, 5, 4, 3) and list them in dialog
That's the idea