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 .
Advertisement Create a free account Compendium in Game, Join Today

Ammo script refilling with a set number

 I am using the amazing ammo script to refill psionic points for a character in Rifts. but i am running into an interesting issue i didn’t see covered in the forums. To recover psionic points the character meditates and receives 8 points per hour of meditation. so i have a set number that goes to fill my psionic points. So the script i have is +8 but i get an error code that the number i am trying to fill is larger than the container or past my max number of points allowed. So what can I use to get it to fill my points and if it goes over max it fills to max and ignores the balance. so for a specific example. Bob the psychic has a max of 10 points he used up 5 leaving 5. They take a long rest. Bob uses the advanced meditation costing 2 points bringing his total to 3 points.  after an hour he gets 8 points which would be 11 but his max is 10.  How do i fill to max and ignore the final point. So i have an ability listed as the advanced meditation. It references Med1or Med2 for how long they meditated. Its the referenced ability that is doing the transfer. &{template:default}{{name=Advanced Meditation}} {{[1 hour](~selected|Med1)}} {{[2 hours](~selected|Med2)}} I.S.P are the psionic points this is the code from the ability i labeled Med1 for 1 hour meditation -2 is the psionic cost of the power. and its the +8 i need modified to fill isp to max and ignore the rest. !ammo @{selected|token_id}  character_isp  -2 I.S.P !ammo @{selected|token_id}  character_isp +8 I.S.P. Med 2 is same as med 1 except for its 2 hours and you get 16 points at the cost of 2.  Thanks for any assistance  -Chuck
1610483289
The Aaron
Forum Champion
API Scripter
You could do the math to get the exact amount to change by: !ammo @{selected|token_id} character_isp +[[{(@{selected|character_isp|max}-@{selected|character_isp}),8}kl1]] Assuming I did the math right there, you should end up with the lower value of (10-3)[7] or 8 being added.  If the character was at 1 point, the first term would be 9, so the 8 would get added.
1610493071

Edited 1610493143
Most excellent. Thank you for the prompt reply.  I plugged in the code and it looks good.  But if I could review the code with you. I am new to coding but i think i am grasping it.  !ammo @{selected|token_id} character_isp So this bit is the name of the string identified with a ! Then the @ and what follows signifies which character sheet i want this thing to happen to, by selecting an icon on the map. Lastly is what part of the sheet i want to change.  +[[{(@{selected|character_isp|max}-@{selected|character_isp}),8}kl1]] Now the interesting bit. The +  adds everything following to the part identified earlier. but then we have in ()  a math problem. max minus current giving us a number so x and a comma  then our static number of 8. Then kl1 compares the two numbers and the lower number gets added to the part identified earlier.  Is this a correct interpretation?  Question though, in the first part you tell it selected then token id then the part. 3 steps to id what you want work on but in the equation you use selected and the part you want only 2 steps to id.  Why the shortened version?
How do i change the name from ammo to I.S.P. Like in the first box? i tried adding it after the 8 but it caused the whole thing to barf cause of the periods. Colonel Harry Greene  uses 2 I.S.P and has 3 remaining.  Colonel Harry Greene  gains 2 ammo and has 5 remaining.  Chuck: Advanced Meditation 1 hour 2 hours : Colonel Harry Greene  uses 2 I.S.P and has 5 remaining.  Colonel Harry Greene  gains 0 ammo and has 5 remaining.
1610498270
Looking at the ammo script and The Aaron example all you would you need to do is add the resource name at the end of the line as follows: !ammo @{selected|token_id} character_isp +[[{(@{selected|character_isp|max}-@{selected|character_isp}),8}kl1]] isp
Yup. That’s what it was. Thank you.
1610552273

Edited 1610552425
The Aaron
Forum Champion
API Scripter
Chuck said: But if I could review the code with you. I am new to coding but i think i am grasping it.  !ammo @{selected|token_id} character_isp So this bit is the name of the string identified with a ! Then the @ and what follows signifies which character sheet i want this thing to happen to, by selecting an icon on the map. Lastly is what part of the sheet i want to change.  !ammo is the command the API is looking for.  If a message has ! in the first position, it is an API message, the remainder is processed by scripts.  The ammo script looks for !ammo at the start of a message to identify messages for it. @{selected|token_id} will reference the currently selected (for first selected if you selected multiple) token, and pull its unique id from it.  That value is filled in on the client side, before the message is submitted, so the API will only see something like '-Jk132f23f5', instead of the reference.  Ammo uses characters, so if given a token id, it will look up the character that token represents and use that. character_isp is the name of an attribute to find on the character. Chuck said: +[[{(@{selected|character_isp|max}-@{selected|character_isp}),8}kl1]] Now the interesting bit. The +  adds everything following to the part identified earlier. but then we have in ()  a math problem. max minus current giving us a number so x and a comma  then our static number of 8. Then kl1 compares the two numbers and the lower number gets added to the part identified earlier.  Is this a correct interpretation?  Question though, in the first part you tell it selected then token id then the part. 3 steps to id what you want work on but in the equation you use selected and the part you want only 2 steps to id.  Why the shortened version? You've got the right of it regarding the math. If I understand your question correctly, you're asking, why wouldn't it just be: !ammo @{selected|character_isp} It's because @{} and ?{} references are expanded on the client, and only the result of the expansion is seen by the API.  In the above, the API would see this as: !ammo 2 In the case of the math, I'm using the immediate value in the client and the thing I want to pass is the result, so the above would end up looking like this to the API: !ammo -Jk132f23f5 character_isp +7 Which is the minimum amount of information the API needs to know who to change, what to change, and by how much. Hope that helps, ask me anything, I love to answer questions!
Thanks for your time.  Yes that helps me to try to understand what the code is doin and how to get it to do what i want. The forum is helping and asking you has really improved things. What kind of code is being used here? Are there any books or websites to learn the more basic functions. I’m new to all this and it’s quite fascinating.
1610559594
The Aaron
Forum Champion
API Scripter
Well, I can help with that. =D Start with learning (or honing) your Javascript skills.&nbsp; There are lots of sites out there, this one is pretty nice:&nbsp; <a href="http://jsforcats.com/" rel="nofollow">http://jsforcats.com/</a> Then delve into writing API scripts. Here are some links that talk about writing API scripts: <a href="https://app.roll20.net/forum/post/6605115/namespaces-novice-seeks-help-exploring-the-revealing-module-pattern" rel="nofollow">https://app.roll20.net/forum/post/6605115/namespaces-novice-seeks-help-exploring-the-revealing-module-pattern</a> <a href="https://app.roll20.net/forum/post/6584105/creating-an-object-that-holds-specific-character-dot-id-and-character-name/?pagenum=1" rel="nofollow">https://app.roll20.net/forum/post/6584105/creating-an-object-that-holds-specific-character-dot-id-and-character-name/?pagenum=1</a> <a href="https://app.roll20.net/forum/post/6237754/slug%7D" rel="nofollow">https://app.roll20.net/forum/post/6237754/slug%7D</a> And of course, ask lots of questions, try lots of things.&nbsp; I usually tell people a few good starting learner scripts are: Write a script that responds to the !parrot command and repeats the arguments back to chat with some small change, like adding "Polly says " to the front.&nbsp; Write a script that reports the name of a token that is moved, and how far it went.&nbsp; Write a script that whispers to the DM how many duplicate numbers where rolled in the last dice roll.&nbsp; That should give you a nice tour of the API's features without being too ambitious.&nbsp;
1610560198

Edited 1610560316
I do have another power i am trying to script out, but it is much more complicated. For me at least. Should i start a new thread or can it be discussed here? and thanks for the links i will check those out for sure
1610562575
The Aaron
Forum Champion
API Scripter
I'd suggest starting a new thread. =D