Page 1 of 1

Defaulting values in functions

Posted: Mon Oct 27, 2014 7:28 am
by cromcrom
I all,
I am trying to create a "catchall" function to get stats (either average them all, get the best stats without mods, or with mods. I would kile to default the booleans in the function. Any idea about this ?

Code: Select all

function getPartyStat(stat,baseStat=true,average=false)
	local finalStat = 0
	local a = {}
	local p = party.party
	local finalAverage = 0
	local c = p:getChampion(i)

	if baseStat then
		local statToRecover = c:getBaseStat(stat)
	elseif baseStat == false then
		local statToRecover = c:getCurrentStat(stat)
	end
	end	
	if average == false then
		for i = 1,4 do
			c = p:getChampion(i)
			if c:getEnabled() and c:isAlive() and statToRecover > finalStat		
				then finalStat = c:getCurrentStat(stat) 
			end
		end	
	elseif
		for i=1, 4 do
			if c:getEnabled() and c:isAlive() then 
			finalAverage = finalAverage+statToRecover 
			table.insert(a,i)
	   end
   finalStat = (finalAverage / #a)   
   end
	end
	return finalStat
end

Re: Defaulting values in functions

Posted: Mon Oct 27, 2014 7:57 am
by minmay
Lua doesn't have default parameters like that. What you can do is:

Code: Select all

function getPartyStat(stat,baseStat=true,average=false)
baseStat = baseStat or true
average = average or false
...

Re: Defaulting values in functions

Posted: Mon Oct 27, 2014 11:09 am
by cromcrom
well, tx for the tip, but I created 4 functions, its easier to understand.