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] Equipment swap script

While this script works, it doesn't change the value of @{selected|weapon-type}. I have the field in the character sheet for that attribute as 'ranged' for testing. The sendChat still says the attack type is ranged. on('chat:message', function(msg) { if (msg.type != 'api') return; var parts = msg.content.split(' '); var command = parts.shift().substring(1); if (command == 'equip-melee') { var selectedId = parts[0]; var selectedweapontype = parts[1];; //Get the token.id as tokens var selectedToken = getObj('graphic', selectedId); if (selectedweapontype != 'melee') { selectedToken.set('weapon-type', 'melee'); } sendChat(msg.who, "/desc Your equiped weapon's attack type is now " + selectedweapontype); } });
1401088745

Edited 1401088866
Lithl
Pro
Sheet Author
API Scripter
First and foremost, selectedToken is a graphic object. Graphic objects do not have a property named "weapon-type", so selectedToken.set('weapon-type', 'melee') won't do anything. (In fact, I'm surprised you're not getting an error.) I suspect you're actually wanting to modify the attribute named "weapon-type" on the character that the token represents. var weaponType = findObjs({ type: 'attribute', name: 'weapon-type', characterid: selectedToken.get('represents') })[0]; if (weaponType) weaponType.set('current', 'melee'); // else weapon-type doesn't exist on the character, or the token doesn't represent a character Note that on Dev (and on main come Wednesday), if you just want the value of the attribute (but you don't need to change that value), you should use getAttrByName, as that will handle character sheets with default values correctly. If you want to change attribute values in your script, you still need to use getObj/findObjs/filterObjs. The other problem is that in your sendChat call, you're outputting selectedweapontype... but selectedweapontype is just your second input parameter. You're not changing it at all. While it might have been defined by @{selected|weapon-type}, by the time the API sees it, it's just a string. Even if you successfully change the weapon-type attribute, selectedweapontype won't change.
I use getAttrByName for my combat resolution script. Makes sense, I have to find the actual reference point for the value before it could be set. Thanks Brian.
an old script of mine that works. might find a few bits in there to make your script work. <a href="https://gist.github.com/DarokinB/5864579" rel="nofollow">https://gist.github.com/DarokinB/5864579</a>
1401168826

Edited 1401170272
Ok so I got it all working, except I still don't see the attribute value change on the attr panel unless I F5 the page. Also my else statement isn't triggering properly. If the weaponType isn't melee, set it to melee, else output that it is already melee. on('chat:message', function(msg) { if (msg.type != 'api') return; var parts = msg.content.split(' '); var command = parts.shift().substring(1); if (command == 'equip-melee') { var selectedId = parts[0]; var selectedToken = getObj('graphic', selectedId); var weaponType = findObjs({ type: 'attribute', name: 'weapon-type', _characterid: selectedToken.get('represents') })[0]; log(weaponType); if (weaponType !== 'melee') { weaponType.set('current', 'melee') sendChat(msg.who, "/desc Your equiped weapon's attack type is now " + weaponType.get('current')); } else { sendChat(msgwho, "/desc You already have a melee weapon equipped."); } } });
1401170632
Lithl
Pro
Sheet Author
API Scripter
weaponType is an attribute object, not a string. weaponType !== 'melee' will always be true; use weaponType.get('current') !== 'melee' instead. I'm not sure why you aren't seeing the value change without refreshing the page. To you have the character sheet dialog open when you use your command? I suppose that might be the reason you don't see it change. If that's true, try closing and re-opening the dialog rather than refreshing the whole page, and see if that updates the field.
1401199655

Edited 1401217125
No it's closed. I also opened, closed and reopened before refreshing.
I got it working. Moving on to a character creation script. Thanks for the help guys.