I just went through and added a cursory script. I however, did not test it. So let me know how it goes and if any errors occur. You should now be able to put multiple resources in the ammo field, separated by a comma. For example ammo=Arrows, other resource name. The only thing that I changed between this and the code in the companion script is in bold. var handleammo = function (msg,character,player) {
if(msg.content.indexOf("ammo=") === -1) {
// UNABLE TO FIND AMMO
return;
}
var ammostr;
if(msg.content.indexOf("{{charname=") > -1) {
ammostr = (msg.content.split("ammo=")[1]||'').split(" {{charname=")[0];
}
else {
ammostr = (msg.content.split("ammo=")[1]||'').split(" charname")[0];
}
_.each(ammostr.split(","), function(ammofull) {
ammofull = ammofull.trim();
var ammoid = "";
var ammoname = "";
// check to see if more than one ammo is used
var ammouseregex = /[[].*]/;
var ammousestr = ammouseregex.exec(ammofull) ? ammofull.match(ammouseregex)[0] : null;
ammofull = ammofull.replace(ammousestr,"");
var ammouses = ammousestr ? parseInt(ammousestr.replace("[","").replace("]")) : 1;
// get the name and id for the ammo resource
if(ammofull.substring(0,1) === "-") {
// ammofull was just the id for the resource
ammoid = ammofull;
var ammoresource = findObjs({type: 'attribute', characterid: character.id, id: ammoid}, {caseInsensitive: true})[0];
if(ammoresource) {
ammoname = getAttrByName(character.id, ammoresource.get("name") + "_name");
}
}
else if(ammofull.indexOf("|") > -1) {
// ammofull contained the id for the resource
var temp = ammofull.split("|");
ammoid = temp[1];
ammoname = temp[0];
}
else {
// no id was found for the resource
ammoname = ammofull;
var resources = filterObjs(function(obj) {
return obj.get("type") === "attribute"
&& obj.get("characterid") === character.id
&& (obj.get("current") + "").toLowerCase() === ammofull.toLowerCase()
&& obj.get("name").indexOf("resource") > -1;
});
if(!resources[0]) {
log("UNABLE TO FIND RESOURCE");
return;
}
var resname = resources[0].get("name").replace("_name","");
ammoid = findObjs({type: 'attribute', characterid: character.id, name: resname}, {caseInsensitive: true})[0].id;
var atkid = "";
if(msg.content.indexOf("~") > -1) {
atkid = (msg.content.split("repeating_attack_")[1]||'').split("_attack_dmg")[0];
}
else {
var atkname = (msg.content.split("{{rname=")[1]||'').split("}}")[0];
var attacks = filterObjs(function(obj) {
return obj.get("type") === "attribute"
&& obj.get("characterid") === character.id
&& obj.get("current") === atkname;
});
var reg = new RegExp(/.+?(?=_atkname_base)/);
_.each(attacks, function(atk) {
if(reg.test(atk.get("name"))) {
atkid = atk.get("name").replace("repeating_attack_","").replace("_atkname_base","");
}
});
}
if(!atkid) {
log("UNABLE TO FIND ATTACK ID");
return;
}
var atkammo = findObjs({type: 'attribute', characterid: character.id, name: "repeating_attack_" + atkid + "_ammo"}, {caseInsensitive: true})[0];
var value = ammousestr
? atkammo.get("current").replace(ammoname+ammousestr, ammoname+ammousestr+"|"+ammoid)
: atkammo.get("current").replace(ammoname, ammoname + "|" + ammoid);
atkammo.set({current: value});
}
ammoresource = findObjs({type: 'attribute', characterid: character.id, id: ammoid}, {caseInsensitive: true})[0];
if(ammoresource) {
ammoresource.set({current: ammoresource.get("current") - ammouses});
var ammoitemid = getAttrByName(character.id, ammoresource.get("name") + "_itemid");
if(ammoitemid) {
var ammoitem = findObjs({type: 'attribute', characterid: character.id, name: "repeating_inventory_" + ammoitemid + "_itemcount"}, {caseInsensitive: true})[0];
if(ammoitem) {
ammoitem.set({current: ammoitem.get("current") - 1});
}
var ammoweight = findObjs({type: 'attribute', characterid: character.id, name: "repeating_inventory_" + ammoitemid + "_itemweight"}, {caseInsensitive: true})[0];
var totalweight = findObjs({type: 'attribute', characterid: character.id, name: "weighttotal"}, {caseInsensitive: true})[0];
if(ammoweight && totalweight) {
totalweight.set({current: totalweight.get("current") - ammoweight.get("current")});
}
}
var output = ammoname + ":\n" + ((ammoresource.get("current") >= 0) ? ammoresource.get("current") + " LEFT" : "OUT OF AMMO");
var formattedOutput = (getAttrByName(character.id, "wtype") === "" ? "" : "/w gm ") + "&{template:desc} {{desc=" + output + "}}";
if(ammoresource.get("current") <= 0 || state.FifthEditionOGLbyRoll20.ammotracking !== "quiet") {
sendChat(msg.who, formattedOutput);
}
}
});
};
EDIT: Updated code to fix errors. EDIT 2: Updated code to be able to handle multiple uses of a single ammo type. You can now put Arrows[3] in the ammunition field to use 3 arrows at once.