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

API doesn't detect a simple change on attribute

Hi, I'm a newbe in Javascript and API, and simply want to begin with the bases. So I tried a simple code to log a message when I change the value of an attribute. The attribute is named "vie". In the html code : <input class="caracteristique-value" type="text" name="attr_vie"/> I put this in API : on("change:vie", function() {     log("change attribute !!!"); }); In another tab on Firefox, I have the game session opened. I change the value of the attribute "vie" but  the log message "change attribute !!!" is never displayed. What I do wrong ?
1590498103

Edited 1590498152
GiGs
Pro
Sheet Author
API Scripter
That would work for sheet workers, which work in the context of a character, but not for the API. The API doesnt know what 'vie' is. You need to use change:attribute on("change:attribute", function(obj) { log("change attribute !!! " + obj.get('name'));      }); Then you have to use functions to identify on which character the attribute changed... See the wiki on the API, Objects, and Events for more. Describe what you're trying ti do and we can help you along.
1590498373
The Aaron
Roll20 Production Team
API Scripter
The events only trigger on object types and object properties, not the values of those properties. on('EVENT:OBJECT:PROPERTY', ...) Where EVENT is one of add,change,destroy Where OBJECT is one of the Roll20 Object types, like graphic, token, character, attribute, text, drawing, etc Where PROPERTY (which is optional) is on of the properties of one of the OBJECTs, such as name, current, max, top, left, width, height, etc depending on the OBJECT. In your case, you want something like: on('change:attribute',(obj,prev)=>{ if('vie' === obj.get('name')){ log('Attribute "vie" was changed!!!'); } });
Oh thanks guys it's so clear now. I didn't understand the difference between API and sheet workers. I thought API was just here to test your Javascript code before putting it in sheet worker. So a newbe misunderstanding lol. First, I just want to initialize some attribute values and create them if they are not already created. Is it something we can do in both API and sheet workers ?
1590502654
GiGs
Pro
Sheet Author
API Scripter
Yes, you can do this in both. But if you are creating sheet workers, you have access to the sheet html, and it's easier just to set default values in the sheet inputs where the attributes are defined.
Ok thanks. And to enable/disable an object like an input, is it possible in both API and sheet workers ?
1590509967
GiGs
Pro
Sheet Author
API Scripter
If you doing work directly on a character sheet, dont both using the APi - use sheet workers, because they are built in. Enabling or disabling an input can mean different things. Do you mean stop players from editing it? If so, that can be done with a sheet worker (kind of), or with CSS (swapping between an enabled and disabled input as appropriate).
Ok so let's use sheet workers. Yeah I have simulated disabled/enabled using "display" in css. Using some div with a border to replace a checkbox with "display:none" for example. Thanks again for all these advices ! That's help me a lot !