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] Alter Bar on Token

September 29 (10 years ago)

Edited February 10 (10 years ago)
This API script will add or subtract from a token's bars. Just define Bar#Keys and Bar#Names variables and you're good to go. The script has been updated to try and prevent errors by players putting in a plus or minus sign and healing the target they wanted to damage or damaging a target they wanted to heal, though it is still very possible. You can use the macros below to help limit those problems as well, but it requires two macros for each bar you want to be able to alter instead of just one.

Add to Bar of Selected Token:
!alter @{selected|token_id} 1 +?{Add to Bar|0}
Subtract from Bar of Selected Token:
!alter @{selected|token_id} 1 -?{Subtract from Bar|0}
Add to Bar of Target Token:
!alter @{target||token_id} 1 +?{Add to Bar|0}
Subtract from Bar of Target Token:
!alter @{target||token_id} 1 -?{Subtract from Bar|0}

Updates

  • November 9th, 2014 12:01 am eastern (ver 1.1) ~ Added option to hide chat announcement and send to GM instead and option to always send to GM in addition to sending to the chat window.
    • To hide chat announcement, simply add any kind of text at the end of the macro. You CANNOT have any spaces in the value however. So you cannot do 1d8 + 4. It has to be something like 1d8+4 unless you're using a roll query like ?{Healed for|0}. Spaces will work in queries.
  • November 8th, 2014 11:30 pm eastern (ver 1.0) ~ Cleaned up the script and made it smaller. Script will no longer edit the msg object and will not break other scripts that rely on the msg object.
  • Old Update ~ 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.
  • Old Update ~ 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 used if you set ANNOUNCE_CHANGE to true.
  • Old Update ~ Added ANNOUNCE_CHANGE to send a message to the chat window to announce which token gained or lost points from which bar.
  • Old Update ~ 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.
  • Old Update ~ Added STOP_AT_ZERO to prevent the script from dropping the current value of the bar being altered to a value below zero.
  • Old Update ~ Added an inline roll like look to the script. Currently not optional, but I will get to that. It has a tooltip so the GM can see the roll or value entered by the player but does NOT function like the full regular tooltip with highlighted numbers and such. That would make this script very much more complicated.

// VERSION INFO
	var AlterBars_Version = 1.1;

// FUNCTION DECLARATION
	var AlterScript = AlterScript || {};

on("chat:message", function (msg) {
	// Exit if not an api command...
	if (msg.type != "api") return;
	
	// Get the api command...
	var command = msg.content.split(" ", 1)[0];
	if (command === "!alter") AlterScript.Process(msg);
});

AlterScript.Process = function(msg, who) {
	// USER CONFIG
		// BAR CONFIGURATION - These are used to identify which bar to adjust. You can
		// also use any lowercase letters as well such as 'h' for hit points/health.
		var Bar1Key = "1";
		var Bar2Key = "2";
		var Bar3Key = "3";
		
		// 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 = ["hit points", "hit points", "hit points"];
		
		// PREVENT OVERMAX - Set this variable to true to prevent the current value of
		// the 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;
		
		// ANNOUNCE CHANGE IN CHAT - Set to true to send a message to the chat window
		// showing which token gained or lost points and how much.
		var ANNOUNCE_CHANGE = true;
		
		// SEND TO GM - Set to true to send the results to the GM. This will also trigger
		// if a hidden change is sent.
		var ALERT_GM = false;
		
		// STOP AT ZERO - Prevents the current value of the bar from dropping below zero.
		var STOP_AT_ZERO = true;
	// END USER CONFIG
	
	// Get and/or create variables...
	var n = msg.content.split(" ");
	var who = msg.who;
	var Target = getObj("graphic", n[1]);
	var Bar = 0;
	Bar = (n[2].toLowerCase().toString() == Bar1Key) ? 1 : 0;
	Bar = (n[2].toLowerCase().toString() == Bar2Key) ? 2 : Bar;
	Bar = (n[2].toLowerCase().toString() == Bar3Key) ? 3 : Bar;
	if (Bar === 0) {
		sendChat("ERROR", "/w " + who.replace(" (GM)", "") + " That is not a valid bar.");
		return;
	}
	var AlterValue = n[3];
	var CurrentValue = parseInt(Target.get("bar" + Bar + "_value"));
	var MaxValue = parseInt(Target.get("bar" + Bar + "_max"));
	var NoAnnounce = n[4];
	
	// Check for a + or - sign...
	var Operand1 = AlterValue.charAt(0);
	var Operand2 = AlterValue.charAt(1);
	if (Operand2 === "+" || Operand2 === "-") AlterValue = AlterValue.substring(2);
	else if (Operand1 === "+" || Operand1 === "-") AlterValue = AlterValue.substring(1);
	
	// Save the value for the tooltip...
	var Expression = AlterValue;
	
	// Define CSS...
	var AddStyle = "display: inline-block; text-align: center; min-width: 1.75em; font-size: 1em; font-weight: bold; color:#040; background-color: #8C8; border: 1px solid #040; padding: -1px 2px; border-radius: 3px;";
	var MinusStyle = "display: inline-block; text-align: center; min-width: 1.75em; font-size: 1em; font-weight: bold; color:#600; background-color: #FAA; border: 1px solid #600; padding: -1px 2px; border-radius: 3px;";
	
	// Main process...
	sendChat("", "/r " + AlterValue, function(outs) {
		AlterValue = parseInt(JSON.parse(outs[0].content).total);
		var Tooltip = "Rolling " + Expression + " = " + AlterValue + "' class='a inlinerollresult showtip tipsy-n'";
		if (Operand1 != "-") {
			// Add to bar...
			if (PREVENT_OVERMAX) AlterValue = (AlterValue + CurrentValue > MaxValue) ? MaxValue - CurrentValue : AlterValue;
			if (ANNOUNCE_CHANGE && NoAnnounce == undefined) sendChat("DiceBot", Target.get("name") + " gained <span title='" + Tooltip + "' style='" + AddStyle + "'>" + AlterValue + "</span> " + BarNames[Bar-1] + ".");
			if (ALERT_GM || NoAnnounce != undefined) sendChat(who, "/w GM " + Target.get("name") + " gained <span title='" + Tooltip + "' style='" + AddStyle + "'>" + AlterValue + "</span> " + BarNames[Bar-1] + ".");
			Target.set("bar" + Bar + "_value", CurrentValue += AlterValue);
		} else {
			// Subtract from bar...
			if (STOP_AT_ZERO && (CurrentValue - AlterValue < 0)) AlterValue = CurrentValue;
			if (ANNOUNCE_CHANGE && NoAnnounce == undefined) sendChat("DiceBot", Target.get("name") + " lost <span title='" + Tooltip + "' style='" + MinusStyle + "'>" + AlterValue + "</span> " + BarNames[Bar-1] + ".");			
			if (ALERT_GM || NoAnnounce != undefined) sendChat(who, "/w GM " + Target.get("name") + " lost <span title='" + Tooltip + "' style='" + MinusStyle + "'>" + AlterValue + "</span> " + BarNames[Bar-1] + ".");
			Target.set("bar" + Bar + "_value", CurrentValue -= AlterValue);
		}			
	});
};
September 29 (10 years ago)
Gen Kitty
Forum Champion
Nice!
September 30 (10 years ago)

Edited September 30 (10 years ago)
  • Added an inline roll like look to the script. Currently not optional, but I will get to that. It has a tooltip so the GM can see the roll or value entered by the player but does NOT function like the full regular tooltip with highlighted numbers and such. That would make this script very much more complicated.

The inline roll like look is basically green for adding to a bar and red for subtracting to a bar as shown below:




The macros I use in my campaign are:

Heal: !alter @{target||token_id} h +?{HP Gained|0}
Damage: !alter @{target||token_id} h -?{Damage Taken|0}
October 01 (10 years ago)
Honeybadger on fire! Good job!
November 05 (10 years ago)
Thank you so much for this one HoneyBadger.
November 05 (10 years ago)
No problem. Glad people get some use of this one. Aaron's TokenMod can do this and much more though if you want more features.
November 05 (10 years ago)
The Aaron
Roll20 Production Team
API Scripter
Yeah, but yours has that snazzy output!! Really need to update mine...
November 05 (10 years ago)
Wes
Pro
Sheet Author
Agreed on the Fancy output, my players are loving watching the hit points to see if it matched the damage rolled and if the creature was going to die.
November 05 (10 years ago)
I think I'm an idiot. I couldn't get Aarons script to do what I wanted. This script was idiot proof so it worked.
November 05 (10 years ago)
The Aaron
Roll20 Production Team
API Scripter
You're not an idiot. The TokenMod script is very powerful, but also fairly arcane. Even I have to look up the syntax and I wrote it! :)
November 05 (10 years ago)
If only we could get HoneyBadger and The Aaron working for Roll20...


bumps into each other...
"You got my HoneyBadger in my The Aaron". You got my The Aaron in my HoneyBadger." Works great together!

Jim
November 05 (10 years ago)
The Aaron
Roll20 Production Team
API Scripter
hahahaahah =D
November 06 (10 years ago)
I'm a hack when it comes to scripting. I steal/beg/borrow from anything I can that makes the api do what I want it to do... and it is usually the most inefficient way possible. Aaron is far better at this than I am.
November 06 (10 years ago)
I am just suggesting that your strengths as api coders would be awesome together. Roll20 would not be the same for me without your Power Cards, Monster and Character importers.

Keep up the good work.


Now which one is Peanut butter and which is Chocolate?


Jim
November 06 (10 years ago)
The Aaron
Roll20 Production Team
API Scripter
Thanks HB! I think we both excel in different ways. Certainly the output of your scripts looks a whole lot more polished than mine does. (I'm having to ask one of the CSS guys at work to moonlight just to try and catch up!) =D

I'll take the Peanut Butter, so long as it's crunchy. =D
November 09 (10 years ago)

Updates

  • November 8th, 2014 11:30pm eastern ~ Cleaned up the script and made it smaller. Script will no longer edit the msg object and will not break other scripts that rely on the msg object.
November 09 (10 years ago)

Edited November 09 (10 years ago)

Updates

  • November 9th, 2014 12:01 am eastern (ver 1.1) ~ Added option to hide chat announcement and send to GM instead and option to always send to GM in addition to sending to the chat window.
    • To hide chat announcement, simply add any kind of text at the end of the macro. You CANNOT have any spaces in the value however. So you cannot do 1d8 + 4. It has to be something like 1d8+4 unless you're using a roll query like ?{Healed for|0}. Spaces will work in queries.
December 09 (10 years ago)

Edited December 09 (10 years ago)
Hey folks, I'm getting an "unexpected token" error in the API for this script and I just cannot find the missing character or whatever minute detail might be out of place. I've been using an older version that worked fine, but I would like to use this updated version since it has the option to only show the GM the results. I'm only trying to alter bar 1 for hp and set it to only announce to the gm. I've never had a problem with the older version.

I named each "Bar#Key" with a differing value and did the same with BarNames (to make sure nothing was repeated). I set PREVENT_OVERMAX = true, ANNOUNCE_CHANGE = false, and ALERT_GM = true.

Need some fresh eyes and a brain that knows more about code than mine. Thanks!


EDIT: Nevermind. Must have hit a key accidentally. Deleted script and repasted and did not change anything except the announce function and it works fine now. Although, as a side note, this script doesn't seem to work with the Token Status Manager script.
December 09 (10 years ago)
The Aaron
Roll20 Production Team
API Scripter
When I see unexpected token errors, it's usually a copy paste problem with the script source, of a bug in the API editor that expands HTML Entities in the code. That would be the case of the error is immediate on save of the script.

If it is happening during a command, can you post the command you are running?

Try getting a fresh copy if the script. If that doesn't work, you can PM me an invite and I'll see if I can track it down.
December 09 (10 years ago)

The Aaron said:

When I see unexpected token errors, it's usually a copy paste problem with the script source, of a bug in the API editor that expands HTML Entities in the code. That would be the case of the error is immediate on save of the script.

If it is happening during a command, can you post the command you are running?

Try getting a fresh copy if the script. If that doesn't work, you can PM me an invite and I'll see if I can track it down.

Thanks Aaron. Ya beat me to my repost ;)
December 09 (10 years ago)
The Aaron
Roll20 Production Team
API Scripter
=D
December 09 (10 years ago)
The Aaron
Roll20 Production Team
API Scripter

Wade said:

Although, as a side note, this script doesn't seem to work with the Token Status Manager script.
How do you mean?

December 09 (10 years ago)

The Aaron said:

Wade said:

Although, as a side note, this script doesn't seem to work with the Token Status Manager script.
How do you mean?


Token Status Manager doesn't respond to the bar value being changed by the Alter script. But if I changed the values in the bar "bubbles" manually the Token Status Manager script responded accordingly. And I'm using that script as written in the example (Link above in quote); meaning at 0.5 of bar 1 value add red dot to token and when bar 1 value = 0 add Dead X to token.

Thoughts?
December 09 (10 years ago)

Edited December 09 (10 years ago)
The Aaron
Roll20 Production Team
API Scripter
Yeah, adjusting the bars in code does not trigger an on('change:') event, so the script never gets activated.

There are ways you could fix that in the code, if it's important to you.
December 10 (10 years ago)

The Aaron said:

Yeah, adjusting the bars in code does not trigger an on('change:') event, so the script never gets activated.

There are ways you could fix that in the code, if it's important to you.

Nah, it's not really important. Just thought it was neat and might help me visually track things a little easier. I honestly have never used the Token Status Manager script before. Just came across it while updating the Alter script.
January 05 (10 years ago)

Edited January 05 (10 years ago)

Wade said:

The Aaron said:

Yeah, adjusting the bars in code does not trigger an on('change:') event, so the script never gets activated.

There are ways you could fix that in the code, if it's important to you.

Nah, it's not really important. Just thought it was neat and might help me visually track things a little easier. I honestly have never used the Token Status Manager script before. Just came across it while updating the Alter script.

I've just made a little injection from one of my codes to work with his. If your situation was less complicated than mine, all you'd have to do is add a line to the Alter Bar script to make the Target variable global. That way, you can access Target (which returns as an object) in any other way you would use an object in your other script, kind of how I demonstrated in the injection.

(My situation is a bit different because it's conditional to whether or not they are adding or subtracting from a bar, so I needed to modify the script altogether).
February 10 (10 years ago)
I'm new to this API thing. I'm trying to use the macros you posted; the prompt came up asking how much health to add, but nothing happens. I've fiddled with the bar part. I've tried "1" I've tried "Bar1" among several others. I'm missing something really fundamental, aren't I?

!alter @{selected|token_id} H +?{Add to Bar|0}
February 10 (10 years ago)
Did you try replace the H with 1? THe H/F/M was part of a specific set of bar names for a specific game. I changed it to 1,2,3 later.
February 10 (10 years ago)
Ok, I altered it and this is what my macro looks like now

!alter @{target||token_id} 1 +?{Add to Bar|2d8+2}

Still no luck.
February 10 (10 years ago)
Did you copy and paste the script into a spot into the api?
February 10 (10 years ago)
Yes I did.
February 10 (10 years ago)
Invite me to your campaign and promote me to GM once I've joined.
February 13 (10 years ago)
Any idea why using this script to lower a bar to 0, would make this script not work?

/**
* Set various token markers based on bar cur/max ratios
*
* The CONFIG array can have any number of configuration objects. These objects
* are processed in order.
*
* barId - The ID of the bar to look at the values for [1, 2, 3]
* barRatio - The ratio of bar value to max value that triggers setting the status marker [0 - 1]
* status - The name of the status marker to toggle [redmarker, bluemarker, greenmarker, brownmarker, purplemarker, dead]
* whenLow - The state of the marker when the bar value is <= the ratio [true, false]
*/
var CONFIG = [
// {barId: 1, barRatio: .5, status: "redmarker", whenLow: true},
{barId: 1, barRatio: 0, status: "dead", whenLow: true}];
on("change:token", function(obj) {
CONFIG.forEach(function(opts) {
var maxValue = parseInt(obj.get("bar" + opts.barId + "_max"));
var curValue = parseInt(obj.get("bar" + opts.barId + "_value"));
//log(opts.barId + ": " + curValue + "/" + maxValue);
if (maxValue != NaN && curValue != NaN) {
var markerName = "status_" + opts.status;
if (curValue <= (maxValue * opts.barRatio)) {
obj.set(markerName, opts.whenLow);
}
else {
obj.set(markerName, !opts.whenLow);
}
}
});
});
February 14 (10 years ago)
The Aaron
Pro
API Scripter
You should really start a new thread for this kind of question. =D

When you do, be sure to include what you mean by "not work."
February 14 (10 years ago)

The Aaron said:

You should really start a new thread for this kind of question. =D

When you do, be sure to include what you mean by "not work."

Good idea, moving question to: https://app.roll20.net/forum/post/1606382/conflict-between-honeybadgerss-alter-token-and-status/#post-1606382
February 14 (10 years ago)
The Aaron
Pro
API Scripter
Sorry, when I read your comment I thought it was a completely unrelated script question post. =/
February 14 (10 years ago)
Np. I re-read my post, and it had the grammer of someone that was totally new to english. So your reading makes sense. And you give a good explanation in the other thread. Thanks!