[Pawn] How to optimize this code - Printable Version + open.mp forum (https://forum.open.mp) -- Forum: SA-MP (https://forum.open.mp/forumdisplay.php?fid=3) --- Forum: Pawn Scripting (https://forum.open.mp/forumdisplay.php?fid=10) --- Thread: [Pawn] How to optimize this code (/showthread.php?tid=1573) |
How to optimize this code - RhaegarX - 2021-01-21 I have this enum that stores the information of organizations stored with mysql. PHP Code: enum 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(string, sizeof(string), "Member 1: %s\n", OrgInfo[org][oMember1]); 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? RE: How to optimize this code - Pinch - 2021-01-21 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 RE: How to optimize this code - RhaegarX - 2021-01-21 Is there any place where I can find some explanation about the loop in the enum? Because I would only loop the members' positions RE: How to optimize this code - Pinch - 2021-01-21 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 RE: How to optimize this code - Markski - 2021-01-22 (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) { 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. RE: How to optimize this code - RhaegarX - 2021-01-23 (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 I tried it that way and returned the following error: PHP Code: error: 039: constant symbol has no size |