How to add optional parameters to functions?

HasteBro

Member
I'm currently going over my personal function library, making it smaller/smarter. I have a function for adventuring with specific settings (fam, fam equip, location and amount of adventures), and I thought it would be nice to add another parameter for adding a goal, but I want that parameter to be optional (like the boolean buy function being able to handle coinmasters optionally). How do I go about doing that?


Here's the function:
Code:
boolean famadventure ( familiar fam, item fe, location l, int adv )
	{
		if(fam.have_familiar())
		{
			use_familiar(fam);
			equip($slot[familiar], fe);
			adventure(adv, l);
			if(!have_equipped(fe))
			{
				equip($slot[familiar], $item[none]);
			}
		}
	
		if(!fam.have_familiar())
		{
			print("You don have a "+ fam +". Unable to adventure.");
		}
		return(true);
	}
 

slyz

Developer
You have to override your original function, like this:
PHP:
boolean famadventure( familiar fam, item fe, location l, int adv  )
{
	if(fam.have_familiar())
	{
		use_familiar(fam);
		equip($slot[familiar], fe);
		adventure(adv, l);
		if(!have_equipped(fe))
		{
			equip($slot[familiar], $item[none]);
		}
	}

	if(!fam.have_familiar())
	{
		print("You don have a "+ fam +". Unable to adventure.");
	}
	return(true);
}
boolean famadventure ( familiar fam, item fe, location l )
{
	return famadventure( fam, fe, l, 0 );
}
boolean famadventure ( familiar fam, item fe )
{
	return famadventure( fam, fe, $location[ none ], 0 );
}
boolean famadventure ( familiar fam )
{
	return famadventure( fam, $item[ none ], $loation[ none ], 0 );
}

EDIT: Right... you "overload" your function, you don't "override it".
 
Last edited:

lostcalpolydude

Developer
Staff member
The simplest way is to make another function with 5 parameters. The 4-parameter function would just call the 5-parameter function with a default value for the fifth parameter.
 

Winterbay

Active member
You need to create an overloaded function (same name, different amount or types of parameters). Say you want to have the option to add a combat filter to your function above then you could do:
Code:
boolean famadventure ( familiar fam, item fe, location l, int adv, string combat ) { 	if(fam.have_familiar()) 	{ 		use_familiar(fam); 		equip($slot[familiar], fe); 		if(combat == "")
		{
			adventure(adv, l);
		}
		else
		{
			adventure(adv, l, combat);
		} 		if(!have_equipped(fe)) 		{ 			equip($slot[familiar], $item[none]); 		} 	}  	if(!fam.have_familiar()) 	{ 		print("You don have a "+ fam +". Unable to adventure."); 	} 	return(true); }


boolean famadventure ( familiar fam, item fe, location l, int adv )
{
	return famadventure ( familiar fam, item fe, location l, int adv , "");
}
 
Top