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

How do you get an entire object, instead of just properties?

Sorry if the title question is confusing. Here's what I'm trying to do. My script to generate a daily random encounter table is almost done, but I want to prevent it from making a new handout every time I use it, and instead overwrite the one that's there. This is what i've got so far. The else works swimmingly. The main If works (it detects that there is something there), but when i get to set handout to whatever is there, it returns null. if(findObjs({_type:"handout",name:"Daily Encounter"}) != ""){ log("handout does exists") handout = getObj({_type: "handout",name:"Daily Encounter"}) log(handout) //example from the wiki // var character = getObj("character", obj.get("represents")); } else{ log("handout doesnt exist") var handout = createObj("handout",{name:'Daily Encounter'}) } I've also tried getObj(...).id, but it also returned couldn't get the id of null.
1560726289
The Aaron
Roll20 Production Team
API Scripter
getObj() takes two parameters, a type string, and an id string: let handout = getObj('handout', '-kFg3675775786'); however, it's more efficient to capture the output of findObjs() and use the object from it: let handout = findObjs({_type:"handout",name:"Daily Encounter"})[0] || createObj('handout',{name:'Daily Encounter'});
1560726296

Edited 1560727766
Scott C.
Forum Champion
Sheet Author
API Scripter
Compendium Curator
So, there are a couple things here. The first is that findObjs returns an array of all objects it finds, and getObj requires that you pass an object type and an id. The second is that I would highly recommend using !== instead of != (and === instead of == if you are using that elsewhere). This is because !== tests both the type and value of the two things you are comparing and does not try to coerce the values into relatable types; this coercion can cause unexpected results when comparing certain value/type combinations. So, here's what I would recommend for your code: let handout = findObjs({_type:"handout",name:"Daily Encounter"})[0]; if(handout){ log("handout does exists") log(handout)             //Do something with your handout } else{ log("handout doesnt exist") handout = createObj("handout",{name:'Daily Encounter'}) } Heh, ninja'd by Aaron EDIT: Edited the if to be more concise
Hah, you are both awesome... Now to research actually posting it as a project, and then generalizing it from my use case....
1560727720

Edited 1560727849
Scott C.
Forum Champion
Sheet Author
API Scripter
Compendium Curator
Aku said: Now to research actually posting it as a project, and then generalizing it from my use case.... A good start is usually posting the code to a gist (or pastebin, or similar service) and then make a forum post here with a link to the code, what it's for, and instructions. Good luck, and welcome to the dark side! ;) EDIT: Also, note I had a typo in my adaptation of your code. The if was triggering when there was no handout, rather than when there was. I've fixed this in the post above.