Section: Variadic Functions
Displaying 1-2 of 2 total.
1
Please enter a numerical value for the importance of this sticky.
Enter 0 to unsticky.
Overkill

I present DictConstruct, which can create and populate a dictionary in the same call.
// DictConstruct
// Constructs a dictionary and populates it using the string arguments given to it.
// Pass: args - strings of the form "key:value"
// Returns: A dictionary handle populated with the key-to-value mappings you supply.
int DictConstruct(... args)
{
int i;
int dict = DictNew();
string k, v;
for(i = 0; i < args.length; i++)
{
if(args.is_string[i])
{
k = GetToken(args.string[i], ":", 0);
v = TokenRight(args.string[i], ":", 1);
DictSetString(dict, k, v);
}
else
{
Exit("DictConstruct: Argument " + str(i) + " must be a string value.");
}
}
return dict;
}
Here is an example usage:
int dict = DictConstruct("name:Bob", "job:Fighter", "attack:54");

Posted on 2008-12-27 23:56:29 (last edited on 2008-12-28 00:58:17)

Overkill

Here's a fairly simple (but possibly handy) varargs function for logging text.
// Print
// Logs a message from a list of mixed string/int arguments and prints it with tabs
// Pass: args - The arguments to print
void Print(... args)
{
	string s;
	int i;
	for(i = 0; i < args.length; i++)
	{
		if(args.is_int[i])
		{
			if(i) s += "	";
			s += str(args.int[i]);
		}
		if(args.is_string[i])
		{
			if(i) s += "	";
			s += args.string[i];
		}
	}
	Log(s);
}
Example usage:
Print("Hey look it's like Log() except for ", 1, " thing, it takes varargs");

Posted on 2008-12-28 00:07:39


Displaying 1-2 of 2 total.
1
 
Newest messages

Ben McGraw's lovingly crafted this website from scratch for years.
It's a lot prettier this go around because of Jon Wofford.
Verge-rpg.com is a member of the lunarnet irc network, and would like to take this opportunity to remind you that regardless how babies taste, it is wrong to eat them.