How do you make a function return a map value?

Fluxxdog

Active member
I have no idea where to look for this and I don't seem to find any examples on the wiki. What I'd like to know is how could I construct a function like this:
int[item]retriveData(input)

How would I go about making a function that could potentially create a map? I see a function like "monster [int] get_monsters( location )" and wonder how I could do something like that. Do these functions have to be hard coded in to mafia?
 

Grotfang

Developer
They don't have to be hardcoded. Try returning an aggregate.

Code:
int [int] aggFunc()
{
	int [int] i;
	i[0] = 1;
	i[1] = 2;
	return i;
}
void main()
{
	int [int] i = aggFunc();
	foreach j in i
		print( i[j] );
}
 

Veracity

Developer
Staff member
For clarity, you might want to use typedefs.

Code:
typedef int [int] myMap;

myMap aggFunc()
{
    myMap i;
    ...
    return i;
}
...and so on...
 

Fluxxdog

Active member
OK, if Bale didn't know about them, I sure as heck didn't ^^ The wiki does have that listed as a reserved word for Data Types. Alright, here's something I whipped up as a real use example:
PHP:
typedef int[int,item]mana_gear;

mana_gear buildList()
{
mana_gear i;
foreach gear in $items[]{
	if(numeric_modifier(gear,"Mana Cost")<0){
		cli_execute("whatif equip "+gear+"; quiet");
		i[to_int(numeric_modifier(gear,"Mana Cost")),gear]=numeric_modifier("_spec", "Buffed MP Maximum");
		}
	}
return i;
}

void main()
{
	mana_gear mp_value = buildList();
	foreach mp_reduction,mana_saver in mp_value{
		print( mana_saver+" would reduce costs by "+mp_reduction+" and would change your max MP to "+mp_value[mp_reduction,mana_saver]);
	}
}
This outputs:
Code:
Brimstone Bracelet would reduce casts by -3 and would change your max MP to 207
plexiglass pocketwatch would reduce casts by -3 and would change your max MP to 188
stainless steel solitaire would reduce casts by -2 and would change your max MP to 174
Emblem of Ak'gyxoth would reduce casts by -1 and would change your max MP to 160
Idol of Ak'gyxoth would reduce casts by -1 and would change your max MP to 190
baconstone bracelet would reduce casts by -1 and would change your max MP to 162
baconstone earring would reduce casts by -1 and would change your max MP to 162
jewel-eyed wizard hat would reduce casts by -1 and would change your max MP to 180
navel ring of navel gazing would reduce casts by -1 and would change your max MP to 184
solid baconstone earring would reduce casts by -1 and would change your max MP to 183
woven baling wire bracelets would reduce casts by -1 and would change your max MP to 160
Your MP results may vary ^^ typedef seems to be like record, except without name[key].field.

From the way I'm looking at things, doing this would have more limited uses, though I'm sure I can find a couple. Processing a map another function would return before using it in your main script might be a good example. If you have more than 1 key, it looks like record would be preferable for clarity. I'm not exactly sure where something like this would go in the wiki, so i leave the editing to better minds.
 

Veracity

Developer
Staff member
A typedef is not "like a record". It is an alias for another type. You can already use the name of a record to refer to it. For maps, not so. Choosing a record vs. a map is a design/clarity decision itself and is independent of whether you use typedefs to make maps more clear. Using typedefs for maps seems like an obvious design/clarity decision.
 
Last edited:

Fluxxdog

Active member
I meant like record in that you don't have type out every single part of the definition over and over again if you're going to use it repeatedly. But yeah, clarity is the major call. I can read MY scripts rather well, but I think I need work on the flow of reading when I want to update or recycle code or post it here. I have 26 scripts that I've designed and use most of them on a bBS basis. One of the thing I've been lacking has been commenting what the heck I was thinking. Made for a bit of a twitch when I went to rework as script that autosold items at the end of the day.
 
Top