The campaign object stores the list of token markers available in the game (built-in and custom). This information is stored as _token_markers. It is read-only. Access it from a Mod Script with:
Campaign().get("_token_markers");The return value is a stringified JSON array containing an object for each token marker currently in the game.
{
"id": 59, // the database id for the marker
"name": "Bane", // the name (non-unique) of the marker
"tag": "Bane::59", // how the token is actually referenced
// this will include the id for custom markers, but not
// for default markers.
"url": "https://s3.amazonaws.com/files.d20.io/images/59/yFnKXmhLTtbMtaq-Did1Yg/icon.png?1575153187"
// ^the url for the token marker's image
}We’ve written an example script to show how you can use this to find Token Markers available in your Campaign.
Add this Script to your game and these commands will be available:
!markernames will output all markers to the chat with the image, name, and id
!markerids will output any/all markers to the chat that match the provided name
!settokenmarker will add the provided string to the currently selected token’s statusmarkers list. Note that this doesn’t do any validation to make sure the Token Marker exists, it simply adds the provided value to the token markers.
!gettokenmarkers outputs the currently selected token’s statusmarkers attribute to the chat
on("ready", () => {
const tokenMarkers = JSON.parse(Campaign().get("_token_markers"));
const getChatMessageFromTokenMarkers = markers => {
let chatMessage = '';
_.each(markers, marker => {
chatMessage += `<p><img src='${marker.url}'> ${marker.id}: ${marker.name}</p>`;
});
return chatMessage;
};
on("chat:message", msg => {
if (msg.content.split(" ")[0].toLowerCase() === '!markernames') {
let chatMessage = getChatMessageFromTokenMarkers(tokenMarkers);
sendChat("Token Markers", chatMessage);
} else if (msg.content.split(" ")[0].toLowerCase() === '!markerids') {
const markerName = msg.content.split(" ")[1].toLowerCase();
let results = [];
_.each(tokenMarkers, marker => {
if (marker.name.toLowerCase() === markerName) results.push(marker);
});
log(results);
let chatMessage = getChatMessageFromTokenMarkers(results);
chatMessage = chatMessage || 'Unable to find any matching token markers'
sendChat("Token Markers", chatMessage);
} else if (msg.content.split(" ")[0].toLowerCase() === '!settokenmarker') {
const markerName = msg.content.split(" ")[1].toLowerCase();
if (!msg.selected && msg.selected[0]._type == "graphic") return;
obj = getObj(msg.selected[0]._type, msg.selected[0]._id);
currentMarkers = obj.get("statusmarkers").split(',');
currentMarkers.push(markerName);
obj.set("statusmarkers", currentMarkers.join(','));
} else if (msg.content.split(" ")[0].toLowerCase() === '!gettokenmarkers') {
if (!msg.selected) return;
if (msg.selected[0]._type !== "graphic") return;
obj = getObj(msg.selected[0]._type, msg.selected[0]._id);
currentMarkers = obj.get("statusmarkers");
sendChat("Token Markers", currentMarkers);
}
});
});