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

Spending MP

1392264521

Edited 1392269865
I'm trying to create a simple script of API that does does the following: 1. Sees a command in the text box, such as "!Robert MP -1" or something to that effect. 2. Deducts a number from bar2 of a token to indicate spending MP, such as -1 for a token named Robert as per the above example. From what I've seen, I think this should be very simple for the API guys, but I haven't figured it out. Could you explain or just type it out if possible? Disclaimer: I don't understand API and I'm really bad at it. I've tried CodeAcademy and whatnot, but I just don't have the mind for it. Please help!
This should do it. You can use it like this: !mp Robert -1 Where the command !mp of course never changes, but you can specify the name of the token to be affected and then the quantity of the MP to be algebraically added to its bar2 value, separated by a space. on('chat:message', function(msg) { if(msg.type == "api" && msg.content.indexOf("!mp") !== -1) { var tString = msg.content.replace("!mp ", ""); var aString = tString.split(" "); if(aString.length<2){ sendChat("Script", "/w gm malformed MP command string (missing name or MP quantity)!"); return; } oToken = findObjs({ _pageid: Campaign().get("playerpageid"), _type: "graphic", _name: aString[0] }); if(oToken == ""){ sendChat("Script", "/w gm No token find by name of '" + aString[0] + "'!"); return; } else { var mpPoints = oToken[0].get("bar2_value"); if(mpPoints === ""){ sendChat("Script", "/w gm No MP found for token '" + aString[0] + "'!"); return; } else{ if(isNaN(aString[1])){ sendChat("Script", "/w gm MP quantity inserted is not a number!"); return; } else{ mpPoints = parseInt(mpPoints); mpPoints += parseInt(aString[1]); oToken[0].set("bar2_value", mpPoints); } } } } });
Wow, that works! It will take a lot of thinking for me to figure out how to make sense of the whole thing, but it should help that I know exactly what it's meant to do. Thanks a lot!
1392301049

Edited 1392301337
And what would I do if I wanted to add a warning message when the modifier exceeds the existing "bar2_value"? Forgive my noobishness, but would it look like this? if{"bar2_value" < mpPoints ) { sendChat(Script","/w gm not enough MP!"); return; } else{ mpPoints = parseInt(mpPoints); mpPoints += parseInt(aString[1]); oToken[0].set("bar2_value", mpPoints); } I imagine this would be framed around the final chunk, which appears to the the MP modification command. Edit: Nope, I'm getting an error message. I've bolded the part that I think is causing the error, but, hey, what do I know?
Try this. Note you have a { instead of a ( in your if statement which needs to be changed as well. if(oToken[0].get("bar2_value") < mpPoints) {
I'd add these lines just after the one with "mpPoints += parseInt(aString[1]);" if(mpPoints < 0){ sendChat("Script","/w gm Not enough MP!"); return; }
No, because then you've spent MP and have to readd it.
Brandon, I implemented your suggestion, but it's not working as intended. It lets me go into negative once. For example, if I spend 4 MP consecutively starting at 12, the sequence is 12, 8, 4, 0, -4, Not enough MP Alternatively, if I spend 4 MP consecutively starting at 3, the sequence is 3, -1, Not enough MP. hmmm...
Alright, I actually looked at the script. var mpPoints = oToken[0].get("bar2_value"); you're basically asking it to check itself. if(oToken[0].get("bar2_value") < mpPoints) { So let's fix it. if (mpPoints < (-1*parseInt(aString[1]))) {
Brandon W. said: No, because then you've spent MP and have to readd it. No, because it doesn't set the new value in case it is below 0, since it is after the return. Just look at the code above. It's just as simple as that.
Alright, then look at it this way. If you have 3 MP and it costs 4, it should still fire. Yours wouldn't.
Brandon W. said: Alright, then look at it this way. If you have 3 MP and it costs 4, it should still fire. Yours wouldn't. In his reply with the tentative code, Robert inserted a "return", so I imagined he wanted a warning and no MP detraction. That's why my code just exits and doesn't change the total MP. But if you want a warning and then proceed anyway to adjust the total, simply remove the "return" (although I'm pretty sure this is not the desired behaviour, only Robert can tell): if(mpPoints < 0){sendChat("Script","/w gm Not enough MP!");}
You're still checking if mpPoints is less than zero. He wants to check if it's less than the amount needed for the spell. I wasn't putting the entire code block in there. You need to check if mpPoints is less than what the API sent through.
Brandon W. said: You're still checking if mpPoints is less than zero. He wants to check if it's less than the amount needed for the spell. I wasn't putting the entire code block in there. You need to check if mpPoints is less than what the API sent through. It is really easy if you take few seconds to understand the script above: I'm checking whether the MP amount LESS the amount passed by the command is less than zero. It means that I check if the token has at least MP >= Quantity passed by the command, because the check happens after I've done the math. Did you at least try once the script before saying it wasn't working? Anyway, too much has already been written for such a simple script, no point in further debating (for me at least).
If I wanted it to deal with stats instead of bars, what would I change? Let's say, for example, that all three of my bars were full but I wanted to create a command that, say, added points to the EXP Attrubite of the selected token. How much would I have to change?
1392829729

Edited 1392829747
Lithl
Pro
Sheet Author
API Scripter
var token = ... // the selected token var characterid = token.get('represents'); if (characterid == '') {...} // Error condition; token isn't linked to a character var attrib = findObjs({ _type: 'attribute', _characterid: characterid, name: 'EXP' // replace as appropriate }); if (attrib.length > 0) attrib = attrib[0]; // uses var cur = attrib.get('current'); attrib.set('current', ...); var max = attrib.get('max'); attrib.set('max', ...); attrib.set({ current: ... max: ... });
Wow, that's still a little bit over my head. How do I insert this into the existing string? And I'm assuming the ellipses don't do anything, and it's something I need to type myself?
1392839528
Lithl
Pro
Sheet Author
API Scripter
Correct, the ellipses are just placeholders; it's not a full script, just a placeholder. var token = ... -- I'm not defining how you obtain a reference to the token, since it's identical to getting a reference to the token in the script using the three bars. if (characterid == '') {...} -- I'm not defining what you should do in case of an error. Logging the error is reasonable, as is reporting it with sendChat. Using return will ensure the script stops trying to do any more execution. attrib.get and attrib.set are used identically to token.get('bar#_value') and token.set('bar#_value', someNumber) .
I really don't get it. This is what I'm starting from: on('chat:message', function(msg) { if(msg.type == "api" && msg.content.indexOf("!mp") !== -1) { var tString = msg.content.replace("!mp ", ""); var aString = tString.split(" "); if(aString.length<2){ sendChat("Script", "/w gm malformed MP command string (missing name or MP quantity)!"); return; } oToken = findObjs({ _pageid: Campaign().get("playerpageid"), _type: "graphic", _name: aString[0] }); if(oToken == ""){ sendChat("Script", "/w gm No token find by name of '" + aString[0] + "'!"); return; } else { var mpPoints = oToken[0].get("bar2_value"); if(mpPoints === ""){ sendChat("Script", "/w gm No MP found for token '" + aString[0] + "'!"); return; } else{ if(isNaN(aString[1])){ sendChat("Script", "/w gm MP quantity inserted is not a number!"); return; } else{ if(mpPoints < (-1*parseInt(aString[1]))){ sendChat("Script","/w gm Not enough MP!"); return; } else{ mpPoints = parseInt(mpPoints); mpPoints += parseInt(aString[1]); oToken[0].set("bar2_value", mpPoints); } } } } } }); For the most part, I understand each of the lines in a vacuum, but that's just for reading them. I don't know how to piece any of this together. Can you show me what it would look like as a part of the existing code?