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 with a function defined in a namespace

1598053742

Edited 1598053912
Ok, since I have found that modifying a bar value on a token from the API does not trigger on(change:graphic) I decided to delve into creating some functions of my own using a name space.  I can get this to work, however, I am having an issue when trying to grab the bar value from a token.  I am passing the token id to the function when it is called.  I have set up to log the value to the API console, and it shows up.  but when I try to use any functions on it, nothing happens, and the rest of the script is skipped with no error logged.  What am I doing wrong. Here is the code for my namespace creation and the function - var cnc = cnc || {}; cnc.tokenFunctions = function() { this.setMarker = function(obj) { log ('Token Id - ' + obj); let tokenHP = obj.get('bar1_value'); log(tokenHP); log('End'); }; }; And here is what I am using to call this - let token = new cnc.tokenFunctions(); token.setMarker(targetTokenID); This is the output in the API console - Spinning up new sandbox... "--> Notice: Monsters creation disabled" "-=> C&C Combat Computer v2.0 - Scott Westberg - 2020/08/20 <=-" "Token Id - -LAAWw58Tcn-rS2caq6d"
1598055433
timmaugh
Pro
API Scripter
So that namespace formation is a little old, and I'm not sure you're doing it in a way that is giving you the protections of a namespace... Also, you're not getting the data from the token because you're passing the token id into the function, but you never resolve that into an obj. Try something like this... const cnc = (()=>{     const tokenFunctions = () => {         this.setMarker = (obj) => {              // leaving it labeled obj for now, even though it's just an id             log(`Token Id - ${obj}`);            // this will output the token id, but it isn't the token object, yet             let tok = getObj('graphic', obj);    // now you have the token object, and can reference the properties/methods from here             let tokenHP = tok.get('bar1_value');             log(tokenHP);             log('END');         };       };     return {                                     // public interface         TokenFunctions: tokenFunctions,     }; })(); You would call it the same way you did originally, but now that you have the object, you should return/log the proper value.
if obj is just an id, a call to non-existent obj.get method should raise an error in the console.
Damnit, thank you.  I had the code to get the object initially and deleted it during troubleshooting!  This is what you get from hammering your head against a wall!  The script I was calling it from makes use of two token object, and grabbing their character objects...  Sometimes, sometimes you should walk away and come back when you are fresh! And thanks for the pointer on the namespace, I was working off of the documentation I found on the Roll20 Wiki for that.  I'll make some changes timmaugh said: So that namespace formation is a little old, and I'm not sure you're doing it in a way that is giving you the protections of a namespace... Also, you're not getting the data from the token because you're passing the token id into the function, but you never resolve that into an obj. Try something like this... const cnc = (()=>{     const tokenFunctions = () => {         this.setMarker = (obj) => {              // leaving it labeled obj for now, even though it's just an id             log(`Token Id - ${obj}`);            // this will output the token id, but it isn't the token object, yet             let tok = getObj('graphic', obj);    // now you have the token object, and can reference the properties/methods from here             let tokenHP = tok.get('bar1_value');             log(tokenHP);             log('END');         };       };     return {                                     // public interface         TokenFunctions: tokenFunctions,     }; })(); You would call it the same way you did originally, but now that you have the object, you should return/log the proper value.