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

Working with the Page Object

I created a simple script to test out working with the Page object. The point of the script is to collect all the pages in the campaign (there are two), and then log the name of each in the Console Output window. Here is the script: log('@@@@@@') var pagesExist = filterObjs({ _type: 'page'}); _.each(pagesExist, function(page) { var pageName = obj.get('name'); log('*** ' + pageName + '***') }); log('&&&&&&') When the script runs, it logs the initial "@@@@@@" and the final "&&&&&&" to the Console Output window, but it does not log the name of either of the two pages. Any thoughts on what I am doing wrong with the syntax? Thanks.
1437402317
The Aaron
Pro
API Scripter
filterObjs() expects a function for a second argument, the return of which determines if the object is retained for the collection. You probably mean to use findObjs() , which takes an object and fills a collection with all the roll20 objects whose properties match the properties of the supplied object. log('@@@@@@') var pagesExist = findObjs ({ _type: 'page'}); _.each(pagesExist, function(page) { var pageName = obj.get('name'); log('*** ' + pageName + '***') }); log('&&&&&&')
I can see the difference between those two. So using findObjs, actually your exact code block, I run the script, in isolation, in a campaign with two pages named "The One" and "The Two". The log output is still: "@@@@@@" "&&&&&&"
1437429169
The Aaron
Pro
API Scripter
Try this instead: on('ready',function(){ log('@@@@@@') var pagesExist = findObjs({ _type: 'page'}); _.each(pagesExist, function(page) { var pageName = obj.get('name'); log('*** ' + pageName + '***') }); log('&&&&&&') });
1437432075

Edited 1437432098
The Aaron
Pro
API Scripter
Ok, just tried it. the on('ready',...) gets it to run, but you need one more correction: on('ready',function(){ log('@@@@@@') var pagesExist = findObjs({ _type: 'page'}); _.each(pagesExist, function(page) { var pageName = page .get('name'); log('*** ' + pageName + '***') }); log('&&&&&&') });
Excellent! Thanks. Now to work it into the larger script I am working on.
What is is exact nature of the on('ready', function()}? I have seen some scripts that use it (but not always the beginning) and others that do not.
1437449264

Edited 1437449342
The Aaron
Pro
API Scripter
It's registering a function for the 'ready' event. See: <a href="https://wiki.roll20.net/API:Events#Campaign_Events" rel="nofollow">https://wiki.roll20.net/API:Events#Campaign_Events</a> This event fires after all the current data for the campaign has been loaded. So if you want to find a list of all objects in a campaign, or a specific object that is already in the Campaign, be sure to only look for it after the ready event fires. In addition, if you bind to add events (such as add:graphic ) before the ready event fires, you will receive add events for objects that were already present in the campaign. Basically, it delays acting until all of the campaign'so data is loading. In the case of the script without it, the API ran it before it had loaded any pages.
Quick question. When I ran the script, it logged the page IDs below. There is a difference in the format of the ID between the page titled "The One" and the other two pages. Is that a correct page ID, or do I have an error somewhere? "************** PAGE IDs **************" "The Two: -JuNlvf7OR32UJz2wJF2" "The Three: -JukQXE54ix0WOT4t5ym" "The One: 7F73B956-71C3-4C5B-8C72-576EE4F15EA4" "The player bookmark is set at page ID: 7F73B956-71C3-4C5B-8C72-576EE4F15EA4" "**************************************"
1437496281
The Aaron
Pro
API Scripter
They may have just changed the id generation code. &nbsp;So long as the page can be pulled up by that ID with getObj('page','7F73B956-71C3-4C5B-8C72-576EE4F15EA4'); It should be fine.