Roll20 uses cookies to improve your experience on our site. Cookies enable you to enjoy certain features, social sharing functionality, and tailor message and display ads to your interests on our site and others. They also help us understand how our site is being used. By continuing to use our site, you consent to our use of cookies. Update your cookie preferences .
×
Create a free account

[Script] Add/Subtract from Current Bar Value

1392726309

Edited 1395271003
This API script will add or subtract from a token's bars. Just define Bar#Keys and BarNames variables and you're good to go. Selected Token Macros !alter @{selected|token_id} h ?{+/- Health|0} !alter @{selected|token_id} f ?{+/- Fatigue|0} !alter @{selected|token_id} m ?{+/- Mana|0} Target Token Macros !alter @{target||token_id} h ?{+/- Health|0} !alter @{target||token_id} f ?{+/- Fatigue|0} !alter @{target||token_id} m ?{+/- Mana|0} Updates Added variables to define the tag used to tell the script which bar to subtract from. These are Bar1Key, Bar2Key, and Bar3Key. In the script below, they are h, f, and m for (H)ealth, (F)atigue, and (M)ana. Added a variable to define the names of the three bars. You must use this format: ["Bar1Name", "Bar2Name", "Bar3Name"] or else it will not work. These are only use if you set ANNOUNCE_CHANGE to true. Added ANNOUNCE_CHANGE to send a message to the chat window to announce which token gained or lost points from which bar. Added PREVENT_OVERMAX to prevent a bar from gaining enough points to go over its max value. If a max value is not set for a bar, this variable has no effect. // USER CONFIGURATION // Set the PREVENT_OVERMAX variable to true to prevent the value of a // token's bar from being greater than its max value. If there is no max // value set, it will not stop the current bar value from increasing. var PREVENT_OVERMAX = true; // Set the ANNOUNCE_CHANGE variable to true to send a message to the chat // window, announcing which token gained or lost points on a bar. var ANNOUNCE_CHANGE = true; // BAR CONFIGURATION - These are used to identify which bar to adjust. The // example below was made for a specific Roll20 user's preferences. These // bar keys must be lowercase, though players can use any case in their // macro since the script converts everything to lower case. var Bar1Key = "h"; // (H)ealth var Bar2Key = "f"; // (F)atigue var Bar3Key = "m"; // (M)ana // BAR NAMES - These names are used if ANNOUNCE_CHANGE is set to true. The // format of the annoucement is: Name gained/lost # BarName. var BarNames = ["Health", "Fatigue", "Mana"]; on("chat:message", function(msg) { // Exit if not an api command if (msg.type != "api") return; // Get the API Command msg.who = msg.who.replace(" (GM)", ""); msg.content = msg.content.replace("(GM) ", ""); var n = msg.content.split(" "); var command = n[0].toLowerCase(); // Alter the Selected or Targeted Token's Bars // Usage : !alter target bar amount // Example : !alter @{selected|token_id} h -4 // Example : !alter @{target|token_id} m ? if (command == "!alter") { // Define variables... var Target = getObj("graphic", n[1]); var Bar = 0; Bar = (n[2].toLowerCase() == Bar1Key) ? 1 : 0; Bar = (n[2].toLowerCase() == Bar2Key) ? 2 : Bar; Bar = (n[2].toLowerCase() == Bar3Key) ? 3 : Bar; if (Bar == 0) { sendChat("ERROR", "/w " + msg.who + " That is not a valid bar. Please use the first letter as follows: H(ealth), F(atigue), or M(ana)."); return; } var AlterValue = n[3]; var TargetBarCurrent = parseInt(Target.get("bar" + Bar + "_value")); var TargetBarMax = parseInt(Target.get("bar" + Bar + "_max")); var StartingValue = TargetBarCurrent; // Check for a + or - sign at the start of the AlterValue... var OperandCheck = AlterValue.charAt(0); AlterValue = (OperandCheck == "-") ? AlterValue.substring(1) : AlterValue; AlterValue = (OperandCheck == "+") ? AlterValue.substring(1) : AlterValue; if (AlterValue.indexOf("d") != -1) { sendChat("", "/r " + AlterValue, function(outs) { AlterValue = parseInt(JSON.parse(outs[0].content).total); if (OperandCheck != "-") { if (PREVENT_OVERMAX == true) { AlterValue = (AlterValue + TargetBarCurrent > TargetBarMax) ? TargetBarMax - TargetBarCurrent : AlterValue; } Target.set("bar" + Bar + "_value", TargetBarCurrent += AlterValue); sendChat("", Target.get("name") + " gained " + AlterValue + " " + BarNames[Bar-1] + "."); } else { Target.set("bar" + Bar + "_value", TargetBarCurrent -= AlterValue); sendChat("", Target.get("name") + " lost " + AlterValue + " " + BarNames[Bar-1] + "."); } }); } else { if (OperandCheck != "-") { AlterValue = parseInt(AlterValue); if (PREVENT_OVERMAX == true) { AlterValue = (AlterValue + TargetBarCurrent > TargetBarMax) ? TargetBarMax - TargetBarCurrent : AlterValue; } Target.set("bar" + Bar + "_value", TargetBarCurrent += AlterValue); sendChat("", Target.get("name") + " gained " + AlterValue + " " + BarNames[Bar-1] + "."); } else { Target.set("bar" + Bar + "_value", TargetBarCurrent -= AlterValue); sendChat("", Target.get("name") + " lost " + AlterValue + " " + BarNames[Bar-1] + "."); } } } });
Updated the script to make it easier to modify for your specific game and added some other options.