View Single Post
  #9  
Unread 06-10-2012, 02:18 PM
Garan's Avatar
Garan Garan is offline
The Undying
Interface Author - Click to view interfaces
 
Join Date: Oct 2010
Posts: 341
Quote:
Originally Posted by Kunter
Code:
function Disease()
    var = 0;
    for i = 1, EffectList:GetCount(), 1 do
        Turbine.Shell.WriteLine(EffectList:Get(i):GetName( ));
    if (EffectList:Get(i):GetName( ) == "Charge") then
        var = 0.5;
    end
    end
    return var;
end
YEEEAAAAHHHH, it works^^

Many Thanks!

Now I have to find the debuffs names...
Two things, first, you are still failing to assign a local scope to your variables and while that won't necessarily cause an issue here, it WILL eventually trip you up somewhere. Especially with commonly used variables like "i". Consider a loop using "i" which calls a function which internally also has a loop using a variable "i" - both "i" variables are one and the same (same scope) so the function will destroy the value in the outer loop causing lots and lots of havoc. By declaring them as local you will get different instances of the variable and eliminate any potential conflict.

Second is more a matter of efficiency. As moebius showed in his example, when using a loop and looking for a specific condition, it is beneficial to use a break statement to exit the loop once the condition has been met if such a condition should only be true for one iteration or if only the first occurence is relevant (if more than one match can occur and all matches must be processed then a break statement is not useful). This can significantly increase the efficiency of your code, since on average you can cut the iterations in half or if you can reasonably expect your match to be at the end of the iteration, perform the iteration in reverse and get even better efficiency.
Reply With Quote