AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting Help (https://forums.alliedmods.net/forumdisplay.php?f=11)
-   -   Array Help ? (https://forums.alliedmods.net/showthread.php?t=93364)

Drak 05-28-2009 00:49

Array Help ?
 
Sorry if I explain this bad.

I have this array
Code:
 new g_UserCustomMessage[33][MAX_CMSGS][128]

And I use it like so:
Code:
RemoveMessage(id,const Message[]) {     for(new Count;Count < MAX_CMSGS;Count++)         if(containi(g_UserCustomMessage[id][Count],Message) != -1)             copy(g_UserCustomMessage[id][Count],127,""); } AddMessage(id,const Message[]) {     formatex(g_UserCustomMessage[id][MAX_CMSGS - 1],"%s",Message);         RefreshMessages(id); } RefreshMessages(id)     for(new Count;Count < MAX_CMSGS;Count++)         copy(g_UserCustomMessage[id][Count],127,Count == MAX_CMSGS - 1 ? "" : g_UserCustomMessage[id][Count + 1]);

All that works just fine. And I have a task to remove messages in the array every so often. (RefreshMessages) But I want a better way to remove a message. This is my problem:

Code:
public func() {     AddMessage(id,"Hello World 1");     AddMessage(id,"Hello World 2");     AddMessage(id,"Hello World 3");         RemoveMessage(id,"Hello World"); }

Obviously, since i use "contain" it would remove all those messages. Is there any way i can maybe make "AddMessage" return something, to remove my exactly message I added? I can return the position where the string is, but "RefreshMessages" bumps it around. Any ideas?

This is what I was hoping for:
Code:
public Function() {     new Msg = AddMessage(id,"Some Message");     RemoveMessage(id,Msg); }

SchlumPF* 05-28-2009 01:28

Re: Array Help ?
 
RemoveMessage(id,Msg[id]); ?!

Drak 05-28-2009 15:15

Re: Array Help ?
 
Quote:

Originally Posted by SchlumPF* (Post 836298)
RemoveMessage(id,Msg[id]); ?!

What? No.

SnoW 05-29-2009 06:55

Re: Array Help ?
 
You could get where the message goes by returning it from the refreshmessages to addmessage and from there to the variable and use it with removemessage or make them to same function etc... Still you'r saying that it removes all because you are using contain. How about using equal, or is there some reason why you wouldn't? You could actually make two funcs, one with containi and one with equal check.

Exolent[jNr] 05-29-2009 13:23

Re: Array Help ?
 
You could do this:

Code:
new Array:g_aMessageArrays; new g_iMaxPlayers; public plugin_init() {     g_aMessageArrays = ArrayCreate(1);         g_iMaxPlayers = get_maxplayers();     for( new i = 0; i <= g_iMaxPlayers; i++ )     {         ArrayPushCell(g_aMessageArrays, ArrayCreate(128));     } } public plugin_end() {     new Array:aMessageArray;     for( new i = 0; i <= g_iMaxPlayers; i++ )     {         aMessageArray = ArrayGetCell(g_aMessageArrays, i);         ArrayDestroy(aMessageArray);     }         ArrayDestroy(g_aMessageArrays); } AddMessage(client, const szMessage[]) {     new Array:aMessageArray = ArrayGetCell(g_aMessageArrays, client);         ArrayPushString(aMessageArray, szMessage);         return ArraySize(aMessageArray) - 1; } RemoveMessage(client, iMessage) {     new Array:aMessageArray = ArrayGetCell(g_aMessageArrays, client);         return ArraySetString(aMessageArray, iMessage, ""); } ClearMessages(client) {     new Array:aMessageArray = ArrayGetCell(g_aMessageArrays, client);         return ArrayClear(aMessageArray); } MessageCount(client) {     new Array:aMessageArray = ArrayGetCell(g_aMessageArrays, client);         return ArraySize(aMessageArray); } GetMessage(client, iMessage, szMessage[], len) {     new Array:aMessageArray = ArrayGetCell(g_aMessageArrays, client);         return ArrayGetString(aMessageArray, iMessage, szMessage, len); } IsValidMessage(client, iMessage) {     new Array:aMessageArray = ArrayGetCell(g_aMessageArrays, client);         new szMessage[2];     ArrayGetString(aMessageArray, iMessage, szMessage, 1);         return szMessage[0]; }

Code:
public myFunction(client) {     ClearMessages(client);         AddMessage(client, "Message #1");     new iMessage = AddMessage(client, "Message #2");     AddMessage(client, "Message #3");         RemoveMessage(client, iMessage);         new iTotal = MessageCount(client);     new szMessage[128];     for( new i = 0; i < iTotal; i++ )     {         if( !IsValidMessage(client, i) ) continue;                 GetMessage(client, i, szMessage, sizeof(szMessage) - 1);         console_print(client, "%s", szMessage);     }         // Should print:     // Message #1     // Message #3 }

Drak 05-29-2009 14:35

Re: Array Help ?
 
That. Is. Perfect.
Thank you!

Exolent[jNr] 05-29-2009 15:03

Re: Array Help ?
 
This would be better for AddMessage() so it replaces the slots that have deleted messages.

Code:
AddMessage(client, const szMessage[]) {     new Array:aMessageArray = ArrayGetCell(g_aMessageArrays, client);         new iTotal = MessageCount(client);     for( new i = 0; i < iTotal; i++ )     {         if( !IsValidMessage(client, i) )         {             ArraySetString(aMessageArray, i, szMessage);             return i;         }     }         ArrayPushString(aMessageArray, szMessage);         return ArraySize(aMessageArray) - 1; }

ConnorMcLeod 05-29-2009 15:44

Re: Array Help ?
 
To retrieve i efficiently, use ArraySize instead of loop + if( !IsValidMessage(client, i) )
Of course this assumes you delete array item if you delete a message.

Exolent[jNr] 05-29-2009 16:02

Re: Array Help ?
 
Quote:

Originally Posted by ConnorMcLeod (Post 837532)
To retrieve i efficiently, use ArraySize instead of loop + if( !IsValidMessage(client, i) )
Of course this assumes you delete array item if you delete a message.

The reason I didn't do that is because when DeleteItem is used, all the others shift down.
Therefore, the index returned from AddMessage() will be wrong if a message before it was deleted.

ConnorMcLeod 05-29-2009 17:37

Re: Array Help ?
 
Quote:

Originally Posted by Exolent[jNr] (Post 837539)
The reason I didn't do that is because when DeleteItem is used, all the others shift down.
Therefore, the index returned from AddMessage() will be wrong if a message before it was deleted.

You could also handle this ;)


All times are GMT -4. The time now is 01:32.

Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.