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

Recognize page_id player is on, and compare with an object page_id

1585254404

Edited 1585254769
I'm working on a script that allows players to scroll a map on a landing page for a campaign. However, I discovered that a user could still scroll the map around even when not on the page. I wanted to make a check at the start of the code that verifies if they player inputting the command request is viewing the page with the map. A short little example of what the script does:&nbsp; <a href="https://imgur.com/W2dl0Vq" rel="nofollow">https://imgur.com/W2dl0Vq</a> function doMapMove(token, orientation, distance){ try { log(orientation + " " + distance); token.set(orientation, token.get(orientation) + distance); } catch(err) { log("movemap command detected but failed"); return; } } on("ready", function() { on("chat:message", function(msg) { try { var mapObject = findObjs({_type: "graphic", name: "movable_map"})[0]; } catch(err) { log("No movable_map object detected"); return; } map_pageID = mapObject.get('pageID'); player_pageID = ... if (map_pageID == player_pageID) { if(msg.type == "api" &amp;&amp; msg.content == "!movemap-up") { var distance = 250; var move_orientation = "top"; doMapMove(mapObject, move_orientation, distance); } if(msg.type == "api" &amp;&amp; msg.content == "!movemap-down") { var distance = -250; var move_orientation = "top"; doMapMove(mapObject, move_orientation, distance); } if(msg.type == "api" &amp;&amp; msg.content == "!movemap-left") { var distance = 250; var move_orientation = "left"; doMapMove(mapObject, move_orientation, distance); } if(msg.type == "api" &amp;&amp; msg.content == "!movemap-right") { var distance = -250; var move_orientation = "left"; doMapMove(mapObject, move_orientation, distance); } } }); }); I know that&nbsp; playerspecificpages &nbsp; grabs a list of players and what pages they're on, but how do I get a hold of the player ID of the person entering the command to be able to search that list for a match?
1585254793
The Aaron
Roll20 Production Team
API Scripter
I have some functions you might be interested in: const getActivePages = () =&gt; [...new Set([ Campaign().get('playerpageid'), ...Object.values(Campaign().get('playerspecificpages')), ...findObjs({ type: 'player', online: true }) .filter((p)=&gt;playerIsGM(p.id)) .map((p)=&gt;p.get('lastpage')) ]) ]; const getPageForPlayer = (playerid) =&gt; { let player = getObj('player',playerid); if(playerIsGM(playerid)){ return player.get('lastpage'); } let psp = Campaign().get('playerspecificpages'); if(psp[playerid]){ return psp[playerid]; } return Campaign().get('playerpageid'); }; const getPlayersOnPage = (pageid) =&gt; { let pages = {}; let ribbonPage = Campaign().get('playerpageid'); let psp = Campaign().get('playerspecificpages'); findObjs({type:'player'}) .forEach(p=&gt;{ if(playerIsGM(p.id)){ const lp = p.get('lasgtpage'); pages[lp]=pages[lp]||[]; pages[lp].push(p.id); } else if(psp.hasOwnProperty(p.id)){ pages[psp[p.id]]=pages[psp[p.id]]||[]; pages[psp[p.id]].push(p.id); } else { pages[ribbonPage]=pages[ribbonPage]||[]; pages[ribbonPage].push(p.id); } }); if(pageid){ return pages[pageid]||[]; } return pages; }; const getGMPlayers = (pageid) =&gt; findObjs({type:'player'}) .filter((p)=&gt;playerIsGM(p.id)) .filter((p)=&gt;undefined === pageid || p.get('lastpage') === pageid) .map(p=&gt;p.id) ; This would do what you want: let playerCurrentPageId = getPageForPlayer(msg.playerid);
Ah excellent, got the pageID just fine thank you. You're the best Aaron.
1585255737
The Aaron
Roll20 Production Team
API Scripter
Heheheh...&nbsp; no problem!
1585256225

Edited 1585262486
on("ready", function() { const getPageForPlayer = (playerid) =&gt; { let player = getObj('player',playerid); if(playerIsGM(playerid)){ return player.get('lastpage'); } let psp = Campaign().get('playerspecificpages'); if(psp[playerid]){ return psp[playerid]; } return Campaign().get('playerpageid'); }; function doMapMove(token, orientation, distance){ try { log(orientation + " " + distance); token.set(orientation, token.get(orientation) + distance); } catch(err) { log("movemap command detected but failed"); return; } }; on("chat:message", function(msg) { try { var mapObject = findObjs({_type: "graphic", name: "movable_map"})[0]; } catch(err) { log("No movable_map object detected"); return; } //var map_pageID = findObjs({_type: "graphic", name: "movable_map"})[0]; var map_pageID = mapObject.pageid; log(map_pageID); let playerCurrentPageId = getPageForPlayer(msg.playerid); log(playerCurrentPageId); //if (map_pageID == player_pageID) //{ if(msg.type == "api" &amp;&amp; msg.content == "!movemap-up") { var distance = 250; var move_orientation = "top"; doMapMove(mapObject, move_orientation, distance); } if(msg.type == "api" &amp;&amp; msg.content == "!movemap-down") { var distance = -250; var move_orientation = "top"; doMapMove(mapObject, move_orientation, distance); } if(msg.type == "api" &amp;&amp; msg.content == "!movemap-left") { var distance = 250; var move_orientation = "left"; doMapMove(mapObject, move_orientation, distance); } if(msg.type == "api" &amp;&amp; msg.content == "!movemap-right") { var distance = -250; var move_orientation = "left"; doMapMove(mapObject, move_orientation, distance); } //} }); }); Final script solution for future scripters. &amp;{template:atk} {{mod=mod}} {{rname=rname}} {{rnamec=test}} {{r1=[▲ Up](!movemap-up) [▼ Down](!movemap-down) }} {{always=1}} {{r2=[◄ Left](!movemap-left) [► Right](!movemap-right) }}{{charname=Landing Page Map Controls}} Macro for buttons using the 5e OGL template
1585256938
The Aaron
Roll20 Production Team
API Scripter
You should throw those two functions inside the on('ready',...) scope just to keep the global namespace clean.
Ah, fixed.