• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Pawn] How to optimize this code
#1
I have this enum that stores the information of organizations stored with mysql.

PHP Code:
enum oInfo
{
    
oID,
    
oName[50],
    
oMoney,
    
oMaterials,
    
oMaconha,
    
oCrack,
    
oCocaina,
    
oOrgVip,
    
oOrgVipTime,
    
Float:oPosX,
    
Float:oPosY,
    
Float:oPosZ,
    
Float:oPosA,
    
oInterior,
    
oWorld,
    
oSkinLider,
    
oSkinLiderF,
    
oSkinMember,
    
oSkinMember2,
    
oSkinMember3,
    
oSkinMember4,
    
oSkinMember5,
    
oSkinMemberF,
    
oSalaryLider,
    
oSalaryMember,
    
oSalaryMember2,
    
oSalaryMember3,
    
oSalaryMember4,
    
oSalaryMember5,
    
oLider[MAX_PLAYER_NAME],
    
oSubLider[MAX_PLAYER_NAME],
    
oMember1[MAX_PLAYER_NAME],
    
oMember2[MAX_PLAYER_NAME],
    
oMember3[MAX_PLAYER_NAME],
    
oMember4[MAX_PLAYER_NAME],
    
oMember5[MAX_PLAYER_NAME],
    
oMember6[MAX_PLAYER_NAME],
    
oMember7[MAX_PLAYER_NAME],
    
oMember8[MAX_PLAYER_NAME],
    
oMember9[MAX_PLAYER_NAME],
    
oMember10[MAX_PLAYER_NAME],
    
oMember11[MAX_PLAYER_NAME],
    
oMember12[MAX_PLAYER_NAME],
    
oMember13[MAX_PLAYER_NAME],
    
oMember14[MAX_PLAYER_NAME],
    
oMember15[MAX_PLAYER_NAME],
    
oMember16[MAX_PLAYER_NAME],
    
oMember17[MAX_PLAYER_NAME],
    
oMember18[MAX_PLAYER_NAME],
    
oMember19[MAX_PLAYER_NAME],
    
oMember20[MAX_PLAYER_NAME],
    
oMember21[MAX_PLAYER_NAME],
    
oMember22[MAX_PLAYER_NAME],
    
oMember23[MAX_PLAYER_NAME],
    
oMember24[MAX_PLAYER_NAME],
    
oMember25[MAX_PLAYER_NAME],
    
oMember26[MAX_PLAYER_NAME],
    
oMember27[MAX_PLAYER_NAME],
    
oMember28[MAX_PLAYER_NAME],
    
oMember29[MAX_PLAYER_NAME],
    
oMember30[MAX_PLAYER_NAME]

};
new 
OrgInfo[MAX_ORGS][oInfo]; 

There is a situation, in which I will display all the members of the organization, however as we can see there are more than 30 members and the current way in which I do this is to write the code for each member.

PHP Code:
format(stringsizeof(string), "Member 1: %s\n"OrgInfo[org][oMember1]);
strcat(membersListstring);

format(stringsizeof(string), "Member 2: %s\n"OrgInfo[org][oMember2]);
strcat(membersListstring);

.... 
30 Code Very Long

ShowPlayerDialog
(playeridDIALOG_ORG_BACKDIALOG_STYLE_TABLIST_HEADERS"Membros da Organiza??o"membersList"Voltar""Fechar"); 

I would like to know if there is any way, to access the variable of each member without having to be one by one. Perhaps with the use of some repetition loop? It's possible?
  Reply
#2
You can loop through the enum (I don't remember how do you loop through enum items in pawn but it's super easy) but I'd recommend using y_foreach a.k.a y_iterate
Using Pawn.CMD?

If you're doing so, this is the very first sign that you absolutely shouldn't utilize your all powerful P-Code knowledge in any of the scripting discussion topics.
  Reply
#3
Is there any place where I can find some explanation about the loop in the enum? Because I would only loop the members' positions
  Reply
#4
Nah, the (main) forum is dead, wait for someone else ig :/



But I really do recommend usage of y_foreach of non-loop functions too, like Iter_Contains n stuff like that
Using Pawn.CMD?

If you're doing so, this is the very first sign that you absolutely shouldn't utilize your all powerful P-Code knowledge in any of the scripting discussion topics.
  Reply
#5
(2021-01-21, 11:54 PM)RhaegarX Wrote: Is there any place where I can find some explanation about the loop in the enum?? Because I would only loop the members' positions

It's simply to loop through every item in the enum, and you do this by using it's size as the upper limit.

The way to traditionally do this is:
Code:
for (new i = 0; i < sizeof(enumName); i)

However, because in this case the enum contains a lot of irrelevant data, you want to start on the position of the enum where the members are. In your enum, it looks like the members start at the 32th position, or 31 counting from 0.

I think this should work:

Code:
for (new i = 31; i < sizeof(oInfo); i) {
    format(string, sizeof(string), "Member %i: %s\n", i-30; OrgInfo[org][oInfo:i]);
    strcat(membersList, string);
}

ShowPlayerDialog(playerid, DIALOG_ORG_BACK, DIALOG_STYLE_TABLIST_HEADERS, "Membros da Organiza??o", membersList, "Voltar", "Fechar");

However, I cannot test it right now.

Note that using 31 as a "magic number" to start the loop looks dirty, and that's because it is. A better solution would be to store the members in another way. A slightly more elegant way to handle it would be perhaps to have a Members[MAX_GROUP_MEMBERS][MAX_PLAYER_NAME] 3d array defined in the enum so you can cleanly loop from 0 to MAX_GROUP_MEMBERS instead. Realistically, loading them right off the database might be the "cleanest" way of them all.
  Reply
#6
(2021-01-22, 08:44 PM)Markski Wrote:
(2021-01-21, 11:54 PM)RhaegarX Wrote: Is there any place where I can find some explanation about the loop in the enum?? Because I would only loop the members' positions



It's simply to loop through every item in the enum, and you do this by using it's size as the upper limit.



The way to traditionally do this is:

Code:
for (new i = 0; i < sizeof(enumName); i)



However, because in this case the enum contains a lot of irrelevant data, you want to start on the position of the enum where the members are. In your enum, it looks like the members start at the 32th position, or 31 counting from 0.



I think this should work:



Code:
for (new i = 31; i < sizeof(oInfo); i) {

format(string, sizeof(string), "Member %i: %s\n", i-30; OrgInfo[org][oInfo:i]);

strcat(membersList, string);

}



ShowPlayerDialog(playerid, DIALOG_ORG_BACK, DIALOG_STYLE_TABLIST_HEADERS, "Membros da Organiza??o", membersList, "Voltar", "Fechar");



However, I cannot test it right now.



Note that using 31 as a "magic number" to start the loop looks dirty, and that's because it is. A better solution would be to store the members in another way. A slightly more elegant way to handle it would be perhaps to have a Members[MAX_GROUP_MEMBERS][MAX_PLAYER_NAME] 3d array defined in the enum so you can cleanly loop from 0 to MAX_GROUP_MEMBERS instead. Realistically, loading them right off the database might be the "cleanest" way of them all.





I tried it that way and returned the following error:

PHP Code:
error039constant symbol has no size 
  Reply


Forum Jump: