View Single Post
Krystek.
Member
Join Date: May 2022
Old 07-23-2022 , 02:42   Re: Is this execution correct?
Reply With Quote #7

Quote:
Originally Posted by fysiks View Post
The thing that stands out to me about this is that you have code execution for all every cell in perks_id[] (because the if statements are opposite so one or the other code blocks will get executed) but you're being inefficient about it by using two loops.

You can do this in a single loop by just looping through all cells of the array and just using an if-then-else to determine which code you execute on each iteration.

P.S. While I understand that it's only part of your debugging, it's probably a good thing to point out so that you get better at seeing your coding mistakes in general: your log_amx() functions in your second loop should be throwing errors because you pass two arguments but the format string only has one.
PHP Code:
    new damage_better_items random_num(312);
    new 
damage_items         random_num(16);

    new 
perks_id[] = { 1234713 };

    for( new 
isizeof(perks_id); i++ ) {
        if( 
cod_get_user_perk(vid) == perks_id[i] ) {
            
log_amx("Better Durability: %i | PerkID %i"durability_perks[vid], perks_id[i]);
            
durability_perks[vid] -= (durability_perks[vid] > damage_better_items)? damage_better_itemsdurability_perks[vid];
            
log_amx("Better Durability: %i | PerkID %i"durability_perks[vid], perks_id[i]);

            return 
PLUGIN_CONTINUE;
        } else {
            
log_amx("Durability: %i"durability_perks[vid], perks_id[i]);
            
durability_perks[vid] -= (durability_perks[vid] > damage_items)? damage_itemsdurability_perks[vid];
            
log_amx("Durability: %i"durability_perks[vid], perks_id[i]);

            return 
PLUGIN_CONTINUE;
        }
    } 
By doing it this way, only the content after the else is executed.

Last edited by Krystek.; 07-23-2022 at 02:43.
Krystek. is offline