Character Vault
Any Concept / Any System
Compendium
Your System Come To Life
Roll20 for Android
Streamlined for your Tablet
Roll20 for iPad
Streamlined for your Tablet

Personal tools

Difference between revisions of "Mod:Token Markers"

From Roll20 Wiki

Jump to: navigation, search
m (Changed protection level for "API:Token Markers" (‎[edit=docwriters] (indefinite) ‎[move=docwriters] (indefinite)))
m (1223200 moved page API:Token Markers to Mod:Token Markers)
 
(7 intermediate revisions by one user not shown)
Line 1: Line 1:
A new function has been added to the Roll20 API to support custom Token Markers.
+
{{revdate}}{{API dev}}{{apibox}}Roll20 [[Mod]] as a function to support custom [[Token Markers]].
  
This information is stored under the campaign node as ‘token_markers’. it can be accessed from the api via
+
This information is stored under the campaign node as {{c|token_markers}}. it can be accessed via
  
 
<code>Campaign().get("token_markers");</code>
 
<code>Campaign().get("token_markers");</code>
Line 9: Line 9:
 
The return value will a stringified JSON array containing an object for each token marker currently in the game.
 
The return value will a stringified JSON array containing an object for each token marker currently in the game.
  
<pre>{
+
<pre style="overflow:auto" data-language="javascript">{
 
     "id":59, // the database id for the
 
     "id":59, // the database id for the
 
     "name":"Bane", // the name (non-unique) of the marker
 
     "name":"Bane", // the name (non-unique) of the marker
Line 31: Line 31:
 
<code>!gettokenmarkers</code> outputs the currently selected token’s statusmarker attribute to the chat
 
<code>!gettokenmarkers</code> outputs the currently selected token’s statusmarker attribute to the chat
  
<pre>on("ready", () => {
+
<pre style="overflow:auto" data-language="javascript">on("ready", () => {
 
     const tokenMarkers = JSON.parse(Campaign().get("token_markers"));
 
     const tokenMarkers = JSON.parse(Campaign().get("token_markers"));
 
     const getChatMessageFromTokenMarkers = markers => {
 
     const getChatMessageFromTokenMarkers = markers => {
Line 71: Line 71:
 
});
 
});
 
</pre>
 
</pre>
 +
 +
[[Category:API Development]]

Latest revision as of 08:53, 9 June 2024

Roll20 Mod as a function to support custom Token Markers.

This information is stored under the campaign node as token_markers. it can be accessed via

Campaign().get("token_markers");

This information is read-only.

The return value will a stringified JSON array containing an object for each token marker currently in the game.

{
    "id":59, // the database id for the
    "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 <name> will output any/all markers to the chat that match the provided name

!settokenmarker <string> will add the provided string to the currently selected token’s marker 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 statusmarker 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);
        }
    });
});