This script is not pulling bonuses for Initiative Can anyone help need this for my next game on Sat. !script {{
--/|Script Name : 5E Initiative Roller (Commented Version)
--/|Version : 1.0
--/|Requires SC : 1.6.2+ (better with 1.6.4 or higher)
--/|Author : Kurt Jaegers
--/|Description : Handles rolling initiative for selected token(s).
--/|Note: Functionally, this script is identical to the 5e_Initiative.roll20 file. This version of the file
--/|has been updated to include extensive comments explaning how nearly every line of the script works and the
--/|intention behind the code. In-game, I suggest using the non-commented version as it is much smaller than this version.
--/|Style Notes: I begin all of my script with a comments header as displayed above, listing basic information about the
--/|script and any requirements (like if it needs another API script to work, etc.) After that I have the description
--/|section with any particular usage details, and then a customizable settings area. I try to use spelled-out names
--/|for my customizable variables so it is easier to understand what they are suppose to do, and I comment the possible
--/|values and the impacts that changing them will have on the script. At the end of the customization section I include
--/|a highly visible separator. I alwyas put a blank space above a new comments section and the code that goes with that
--/|comment immediately follows it without any blank lines to keep them grouped together. Finally, while it is possible
--/|to simply string a single comment along for as many lines as you like (whitespace doesn't really matter until you
--/|reach a new statement identifier (double-dash)), I generally keep my comment lines to around 120 characters in length
--/|and start each new line with a comment statement identifier so it looks good/is readable in my editor (Visual Studio Code)
--/|================ Customize the behavior of the script by setting these values ================
--/|If set to 1, the script will always ADD a token entry in the turn tracker instead of replacing the existing token
--/|If left at the default (0) the token in the tracker will be replaced/updated if it already exists.
--&alwaysAddExtraToken|0
--/|If set to 1, the turn tracker won't be modified
--&dontUpdateTurnTracker|0
--/|The title for the Script Card that will be output to the chat window
--#title|Initiative
--/|===================== There are no customization options below this line =====================
--/|We need these substitution variable to get around the 5E sheet's weirdness about using dice rolls that look like attributes
--&obrac|{
--&cbrac|}
--&at|@
--/|Create a list of all of the selected token ids (see the next couple of comments for additional information). In general, when
--/|calling a function, the variable that will be returned is placed in the "Tag" portion of the call (in this case, selectedTokens)
--/|and the content contains the name of the function, subfunction, and parameters. The "getselected" function has no subfunctions
--/|and takes no parameters.
--~selectedTokens|getselected
--/|As a result of running getselected, the variable &selectedTokensCount now contains the number of IDs that were selected.
--/|We can use this count to set up a for...next loop to run through each of the tokens.
--%loop|1;[&selectedTokensCount]
--/|The getselected function creates variables named selectedTokens1, selectedTokens2, etc. We can use nested variable
--/|refrencing to use &loop to build the name of the variable we are looking for on each loop iteration. This works because
--/|the innermost variable reference ([&loop]) will be evaluated first, and will be replaced with the value of &loop, so the
--/|outer reference becomes [&selectedTokens1] for example, which is one of our varible names. We then store the
--/|current token's ID in &thisId for convenience so we don't have to write out the whole nested thing every time.
--&thisId|[&selectedTokens[&loop]]
--/|Start by assuming we are just going to roll 1d20 for initiative. Note that this is creating a string variable (& command)
--/|and not actually rolling the dice. This is because we want to build a formula for a roll assignment (= command) to happen
--/|later. The &initDie string variable will literally contain the text "1d20"
--&initDie|1d20
--/|Now check the initiative_style entry for the character. It contains nested brackets and @ references, so we use oddrowbackground
--/|&at, &obrac, and &cbrac to avoid trouble trying to type those directly into the macro. If we find either of the two possible
--/|values that indicate advantage or disadvantage, we set &initDie to either 2d10kh1 (advantage) or 2d20kl1 (disadvantage). We use
--/|the shorthand method of setting a string variable in a conditional here.
--?"[*[&thisId]:initiative_style]" -eq "[&obrac][&at][&obrac]d20[&cbrac],[&at][&obrac]d20[&cbrac][&cbrac]kh1"|&initDie;2d20kh1
--?"[*[&thisId]:initiative_style]" -eq "[&obrac][&at][&obrac]d20[&cbrac],[&at][&obrac]d20[&cbrac][&cbrac]kl1"|&initDie;2d20kl1
--/|Assume there is no bonus to initiative. If we don't do this and try to use initiative_bonus directly, any character that
--/|doesn't have an initiative bonus will end up with a roll that looks like "1d20 +" when you hover over it. It will subtitlefontcolor
--/|work, but it doesn't look good, so by pre-setting the value to 0 we would end up with "1d20 + 0"
--&initBonus|0
--/|Check the initiative_bonus attribute for the character. We add an X here so that if the value is empty we get "X" back and it makes
--/|for an easy comparison. (otherwise, we would end up with blanks that would confuse the conditional function). If we do find a value
--/|for initiative_bonus, just update &initBonus with that value.
--?"X[*[&thisID]:initiative_bonus]" -ne "X"|&initBonus;[*[&thisID]:initiative_bonus]
--/|Using the &initDie and &initBonus we arranged above, roll the character's initiative. Both of these are string variables, so the
--/|line below will end up looking something like "1d20 + 4" when processed, which will be where we actually roll the dice and assignment
--/|the result to $initRoll
--=initRoll|[&initDie] + [&initBonus]
--/|Output the token's name and the initiative roll to the output card
--+[*[&thisId]:t-name]|[$initRoll]
--/|OPTIMIZATION NOTE: Because the results of detemining the turn tracker methods to use are the same for every token processed, it would
--/|likely be better to move this whole section to the part of the script before getting the selected tokens and only have it execute once.
--/|But I didn't think of that until after I started writing the comments, so I left it here. It won't hurt anything, but the script would
--/|run slightly faster if you had a lot of tokens selected if that change was made. The actual performance difference is likely unnoticable
--/|though, and if a player is using the script to roll initiative for just their own token there is no practical difference between the
--/|two approaches.
--/|Time to update the turn tracker. We start by assuming the version number for ScriptCards is 1.6.4 or higher, which will allow us to
--/|use "turnorder;replacetoken" as the function to update the tracker. Because of the change to the way the turn tracker works, and the
--/|fact that the 1.6.2 update of ScriptCards only fixed the "turnorder;addtoken" function, we need to figure out what calls we will
--/|use to update the tracker. If we have 1.6.4, we will just use replacetoken, setting method1 (the remove call) to dontremovetoken which
--/|is not a valid turnorder function, so it will just be ignored by ScriptCards.
--&method1|dontremovetoken
--&method2|replacetoken
--/|Check the &ScriptCards_Version variable. If it is blank (same adding an X trick described above), we are using a version before 1.6.4, so
--/|we need to remove the token from the tracker first and then re-add it, so we change method1 to removetoken and method2 to addtoken.
--?"[&ScriptCards_Version]X" -eq "X"|&method1;removetoken
--?"[&ScriptCards_Version]X" -eq "X"|&method2;addtoken
--/|Check the &alwaysAddExtraToken setting (top of the script). If it is 1, no matter what we came up with for version information, we just want
--/|to add a turn for the token and not replace anything that might already exist, so set method1 and method2 appropriately.
--?[&alwaysAddExtraToken] -eq 1|&method1;dontremovetoken
--?[&alwaysAddExtraToken] -eq 1|&method2;addtoken
--/|Next we need to check to see if &dontUpdateTurnTracker is set to 1 (settings at top of script). If it is, we don't actually want to make
--/|turn tracker changes, so set both method1 and method2 to invalid function names so they don't do anything.
--?[&dontUpdateTurnTracker] -eq 1|&method1;dontremovetoken
--?[&dontUpdateTurnTracker] -eq 1|&method2;dontaddtoken
--/|Call the two functions we determined we would use. The first call (method1) *potentially* removes the existing token entry from the
--/|turn tracker, while the second (method2) adds the token back into the tracker.
--~|turnorder;[&method1];[&thisId]
--~|turnorder;[&method2];[&thisId];[$initRoll.Raw]
--/|We've don all the processing for this token, so mark the end of the loop and let the script process the next token.
--%|
}}