Defaulting values in functions

Ask for help about creating mods and scripts for Grimrock 2 or share your tips, scripts, tools and assets with other modders here. Warning: forum contains spoilers!
Post Reply
User avatar
cromcrom
Posts: 549
Joined: Tue Sep 11, 2012 7:16 am
Location: Chateauroux in a socialist s#!$*&% formerly known as "France"

Defaulting values in functions

Post by cromcrom » Mon Oct 27, 2014 7:28 am

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
A trip of a thousand leagues starts with a step.

minmay
Posts: 2657
Joined: Mon Sep 23, 2013 2:24 am

Re: Defaulting values in functions

Post by minmay » Mon Oct 27, 2014 7:57 am

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
...
Grimrock 1 dungeon
Grimrock 2 resources
I no longer answer scripting questions in private messages. Please ask in a forum topic or this Discord server.

User avatar
cromcrom
Posts: 549
Joined: Tue Sep 11, 2012 7:16 am
Location: Chateauroux in a socialist s#!$*&% formerly known as "France"

Re: Defaulting values in functions

Post by cromcrom » Mon Oct 27, 2014 11:09 am

well, tx for the tip, but I created 4 functions, its easier to understand.
A trip of a thousand leagues starts with a step.

Post Reply