Need help initializing an array of records

Take_Pills

New member
I'm trying to do something with the new item_drops_array() and I want something that has the same function as:

int[item]loc_mon_it;
loc_mon_it = item_drops(loc_mon[mon]);

except instead of an array of ints i want an array of records generated by item_drops_array(). right now i have:

record[item]loc_mon_it;
loc_mon_it = item_drops_array(loc_mon[mon]);

but this is giving me some error like [ is not a proper name for a record. am i not initializing this correctly? also, will item_drops_array be able to handle the input of an array of monsters?
 

Take_Pills

New member
erm, never mind. from the wiki "Since this is the first function to return a record, it cannot be stored to a variable, and thus the only way to access the data is by iterating over the elements in the array."

that's sort of unhelpful :(. oh well i'll figure it out
 

slyz

Developer
If you really need to store the data, you can create a similar array of records and save the results in it:
PHP:
record dropdata {
   item drop;
   int rate;
   string type;
};
monster mon = $monster[giant sandworm];
dropdata[int]loc_mon_it;
foreach i, rec in item_drops_array(mon) {
	loc_mon_it[i].drop = rec.drop;
	loc_mon_it[i].rate = rec.rate;
	loc_mon_it[i].type = rec.type;
}

foreach i ,rec in loc_mon_it
  print("index: "+i+", item: "+rec.drop+", drop rate: "+rec.rate+", type: "+rec.type);

EDIT:
This code will return:
Code:
index: 0, item: spices, drop rate: 10, type:
index: 1, item: spices, drop rate: 10, type:
index: 2, item: spices, drop rate: 10, type:
index: 3, item: spice melange, drop rate: 0, type: n

If you really want an array of items, you can do this:
PHP:
record dropdata {
   int rate;
   string type;
};
monster mon = $monster[blooper];
dropdata[item]loc_mon_it;
foreach i, rec in item_drops_array(mon) {
	loc_mon_it[rec.drop].rate = rec.rate;
	loc_mon_it[rec.drop].type = rec.type;
}

foreach itm ,rec in loc_mon_it
  print("item: "+itm+", drop rate: "+rec.rate+", type: "+rec.type);
but for monsters that drop multiple times the same item, some info will be lost. This snippet will print:
Code:
item: white pixel, drop rate: 60, type:
instead of
Code:
index: 0, item: white pixel, drop rate: 80, type:
index: 1, item: white pixel, drop rate: 70, type:
index: 2, item: white pixel, drop rate: 60, type:
 
Last edited:

heeheehee

Developer
Staff member
Oh man, and I thought that code sample showed how to use it (i.e. how exactly to iterate over the elements of an array). :(

The idea of these samples is generally for the user to try them out him/herself and see what happens. Just to give an example of proper syntax/usage.

(Seriously, though, what can we do to make that page more helpful?)
 

Bale

Minion
Oh man, and I thought that code sample showed how to use it (i.e. how exactly to iterate over the elements of an array). :(

The idea of these samples is generally for the user to try them out him/herself and see what happens. Just to give an example of proper syntax/usage.

(Seriously, though, what can we do to make that page more helpful?)
Page is now more helpful.
 
Top