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

[HELP] extracting character sheet ID for given sheet name.

Hi I'm new to javascript and I'm trying to write a script that extracts the character ID for a given input name. Here's what i have so far. //-----------------------------------------------------------Start Script------------------------------------------------------------------------------------- on("chat:message", function(msg) { if(msg.type == "api" && msg.content.indexOf("!getid ") !== -1) { var selected = msg.selected _.each(selected, function(obj) { //create variable for Input Name var slice = msg.content.split(" ") var inputNam = slice[1] //create function that will search characters using given name var findChar = function(charNam) { var characters = findObjs({_type: "character", name: charNam}) } //use function and save to variable test var test = findChar(inputNam); //extract ID from test var id = test.get("_id"); // Output in log says test is undefined therefore has no get function //output ID log(id) }) } }); //--------------------------------------------------------------------End Script---------------------------------------------------------------------------- End game i would like to link this ID to a newly created token.
1394576365
Lithl
Pro
Sheet Author
API Scripter
on("chat:message", function(msg) { if (msg.type == "api" && msg.content.indexOf("!getId ") === 0) { // Create variable for Input Name var inputName = _.rest(msg.content.split(" ")).join(" "); var list = findObjs({ _type: "character", name: inputName }); if (list.length == 0) { // There is no matching character return; } var characterId = list[0].id; // Assuming characters in the journal have unique names var selected = msg.selected; _.each(selected, function(obj) { if (obj.get("type") !== "graphic" && obj.get("subtype") !== "token") { return; // Only tokens will be linked to the character } obj.set("represents", characterId); }); } }); Your main problem is in the findChar function: you don't return anything. You also have to remember that findObjs returns an array, not a single object. Here is the full list of changes I made above: Changed !== -1 to === 0 so that the script looks for your API command at the start of the message, rather than anywhere within the method Included more EOL semicolons; JavaScript may function without them, but it's good practice to always include them, as it can help avoid certain kinds of bugs Joined your input name with spaces, to allow you to look for names which have spaces in them. _.rest gives all of the elements in the array except for the first element. Removed the findChar function entirely; you're only using it once, and it's in a limited scope, so you can't use it elsewhere, anyway. Moved the input name search outside the _.each loop, since you really only need it once. A Roll20 object's _id attribute can be accessed with obj.id , there's no need for get("id") . For that matter, when it comes to the read-only attributes of a Roll20 object, you can omit the leading underscore when using get . Linked the character to the selected tokens Disclaimer: I have not tested the above script. =)
Wow thanks a lot man, I've finally got it working!! One more thing, how do i get the avatar image from the character sheet? var characterImage = list[0].avatar; does not work :(
bump. Would i need to make a function that gets the avatar ID and returns it?
1394769602
Lithl
Pro
Sheet Author
API Scripter
id is the only property you can access directly. Other properties must be retrieved with the get function. Eg, var characterImage = list[0].get('avatar');
Ah thanks! I just realised u already addressed that in your previous post -.-