View Single Post
  #5  
Unread 06-10-2012, 11:03 AM
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
function Disease()
if (EffectList:Contains(charge)) then -- This is line 30
Disease = 0.5
else
Disease = 0
end
end
In addition to what the authors above noted, you have a number of problems in the Disease function. First, you are using the same name for a function and a variable within that function (I suspect you have a VB background and are trying to assign the return value). Unfortunately in Lua what will happen with your code is the first time the function gets called, it gets reassigned to a numeric value and will become invalid as a function the next time you try to call it.

Consider the code:
Code:
import "Turbine"
function Disease()
        Disease = 0.5
end
Turbine.Shell.WriteLine("Type before call:"..type(Disease));
local tmpVal=Disease();
Turbine.Shell.WriteLine("tmpVal:"..tostring(tmpVal));
Turbine.Shell.WriteLine("Type after call:"..type(Disease));
The above code demonstrates the two problems with your function, first the output:
Code:
Type before call:function
tmpVal:nil
Type after call:number
shows that "Disease" is indeed changed from a function to a number after it is called. Second, the value returned by Disease() is "nil". This is because you must explicitly use the "return" statement to return a value.

The below code demonstrates using a local value for the return value, initializing it to the default and explicitly returning it. Note that you should always create variables with the narrowest possible scope, so the return value is scoped as local to the function to avoid conflict with any other variable with the same name.
Code:
-- assumes that "EffectList" is properly assigned an instance of an EffectList and that "charge" is properly defined
function Disease()
    local retVal=0;
    if (EffectList:Contains(charge)) then -- This is line 30
        retVal = 0.5;
    end
    return retVal;
end
Reply With Quote