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

Why the infinite loop? I'm flummoxed

So every time I enable this script, it floods my chat window with an infinite loop WITHOUT me using the macro. I cannot figure this behavior out, and it's been 3 days off and on. What's supposed to happen? I click the macro button to remove HP from "The Ship" in Stars Without Number. I enter an amount to subtract from shipAC (basic custom attr).  Script checks to see if the msg is a good one, then gets the current HP, and sees if the damage is >= shipHP. If the damage is >= than the current shipHP, it sets shipHp to 0, sets token sides to 0, and sends a "you died" message. Else, it subtracts from shipAC, and changes currentside by 1. What happens I enable the script and NOTHING ELSE (never touch the macro at all, just enable the script) It runs the else infinitely with this output:              There was an error parsing the damage macro params              Current HP is undefined              Damage is undefined HELP! The Macro !ShipDamage ?{Amount|0}  The CODE on ( "ready" , function () { sendChat ( "api" , "!token-mod --config players-can-ids|on" ); on ( "chat:message" , function ( msg ){ if ( msg . type == "api" && msg . content . indexOf ( "!ShipDamage" )== 0 ){ var args = msg . content . split ( " " ); var damage = parseInt ( args [ 1 ], 10 ); var currentHP = getShipHP (); var adj = currentHP - damage ; setHP ( currentHP , damage ) } else { let current = `Current HP is ${ currentHP } ` ; sendChat ( "TEST" , current ); let dmg = `Damage is ${ damage } ` ; sendChat ( "TEST" , dmg ); sendChat ( "TEST" , "There was an error parsing the damage macro params" ); return ; } }); function setHP ( currentHP , damage ){ if ( damage >= currentHP ){ sendChat ( "api" , "!modattr --name Ship --shipHP|0" ); sendChat ( "api" , "!token-mod --api-as -NgLL39aMVMGSgYa69Tq --ids -NmhzxkuwfkUhKcGXkuq --set currentside|0" ); sendChat ( "api" , "!token-mod --api-as -NgLL39aMVMGSgYa69Tq --ids -Nmhlw3OUp-m0eUxoo6w --set currentside|0" ); sendChat ( "api" , "The Ship has been destroyed!" ); } else { for ( i = 0 ; i < damage ; i ++){ let msg = `Reducing HP by 1 for count ${ i } of ${ damage } .` ; sendChat ( "api" , msg ); sendChat ( "api" , "!modattr --name Ship --shipHP|-1" ); sendChat ( "api" , "!token-mod --api-as -NgLL39aMVMGSgYa69Tq --ids -NmhzxkuwfkUhKcGXkuq --set currentside|-1" ); sendChat ( "api" , "!token-mod --api-as -NgLL39aMVMGSgYa69Tq --ids -Nmhlw3OUp-m0eUxoo6w --set currentside|-1" ); } } } function getShipHP (){ var Ship = findObjs ({ type : 'character' , name : 'Ship' })[ 0 ]; var hitPoints = findObjs ({ type : 'attribute' , characterid : Ship . id , name : 'shipHP' })[ 0 ]; var hpValue = parseInt ( hitPoints . get ( 'current' )); return hpValue ; } });
1705334961
The Aaron
Roll20 Production Team
API Scripter
Here's the path causing the infinite loop (other lines removed for clarity: on("ready", function() { ... on("chat:message",function(msg){ if(...){ ... } else{ ... sendChat("TEST",current); Basically: on Ready Get a message Send a message  (goto 2)
1705335140
The Aaron
Roll20 Production Team
API Scripter
Basically, if you are inside a chat:message  event handler, you need to be careful about using sendChat().  If you want to test things, use log() instead. else { let current = `Current HP is ${ currentHP } ` ; log (` TEST: ${ current}` ); let dmg = `Damage is ${ damage } ` ; log (` TEST: ${ dmg}` ); log (` TEST: There was an error parsing the damage macro params` ); return ; }
You gotta be kidding me. My error handling/testing did this?? Lmao.  Thanks, The Aaron! Works perfectly now!
1705336941
GiGs
Pro
Sheet Author
API Scripter
When I have an error, the first thing I look at is my error handling code. It causes the error so many times :)
@GiGs - lol same. I'm quite new to javascript, and sorta new to coding generally, but Ii am FRIGGIN LOVING THIS.
1705341294
The Aaron
Roll20 Production Team
API Scripter
It is so fun!&nbsp; Definitely reach out if you have questions, or just want to ask some what if programming questions related to Roll20 or javascript.&nbsp; Here's some links that might be helpful as you start your journey: <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>