Updating Macro Buttons Using a Macro (Label Method): Difference between revisions
Verisimilar (talk | contribs) m (Added to Tutorial category.) |
m (Taustin moved page updating Macro Buttons Using a Macro (Label Method) to Updating Macro Buttons Using a Macro (Label Method) over redirect) |
||
(13 intermediate revisions by 6 users not shown) | |||
Line 1: | Line 1: | ||
==Updating the Label of our macro buttons using DnD4e as an Example.== | ==Updating the Label of our macro buttons using DnD4e as an Example.== | ||
For an example I will use DnD4e powers | For an example I will use DnD4e powers; when a power is used it will then update the button to show this. Although this example is for DnD4e the technique is applicable for many other systems. | ||
Let's assume that you have macro groups named "Daily Powers" and "Encounter Powers" where we place all of our powers, and we want to set the color of the macro button to blue if the power has been used. If you want another way to do this without using different groups see Tracking Used DnD 4e Powers (Macro Prefix Method) | |||
'''Version update:''' | '''Version update:''' | ||
This tutorial was written before 1.3b50. As of 1.3b50 you can use [[Macros:Functions:getMacroButtonIndex|getMacroButtonIndex]] to determine the index of the button that was pressed | This tutorial was written before 1.3b50. As of 1.3b50 you can use [[Macros:Functions:getMacroButtonIndex|getMacroButtonIndex]] to determine the index of the button that was pressed. Instead of setting/getting the properties of the button by name you can use the index of the button in place of the name, this way you will not run the risk of updating buttons with the same name. | ||
===Short Rest, resetting label of buttons in the "Encounter Powers" group=== | ===Short Rest, resetting label of buttons in the "Encounter Powers" group=== | ||
So first we create a [[macro button]] called "Short Rest", for this tutorial I will assume that you create it as a [[campaign macro]] --just remember to check the [[ | So first we create a [[Macro_Button|macro button]] called "Short Rest", for this tutorial I will assume that you create it as a [[Introduction_to_Macro_Writing#Campaign Macros|campaign macro]] --just remember to check the [[Introduction to Macro Writing#Referencing a Token Property in a Macro|Apply to Selected Tokens]] check mark-- you can just as easily create these as macros in [[Token:library token|library token]] macros and call them from from [[Macro_Button|macro buttons]] on your [[Token|tokens]]. | ||
[[image:ShortRest1MacroButton.png]] | [[image:ShortRest1MacroButton.png]] | ||
So create the "Short Rest" [[macro button]] and copy the following code into it | So create the "Short Rest" [[Macro_Button|macro button]] and copy the following code into it | ||
< | <syntaxhighlight lang="mtmacro" line> | ||
[abort(hasImpersonated())] <!-- Abort macro if no token is impersonated --> | [abort(hasImpersonated())] <!-- Abort macro if no token is impersonated --> | ||
Line 36: | Line 36: | ||
}] | }] | ||
[abort(0)] <!-- Suppress the macros output --> | [abort(0)] <!-- Suppress the macros output --> | ||
</ | </syntaxhighlight> | ||
This macro will get the labels of all the macros with [[ | This macro will get the labels of all the macros with [[Macros:Functions:getMacros|getMacros()]]. It will then loop through all of these labels and get the index for each [[Macro_Button|macro button]] with that label. Then it will check to see if the macro label ends with the text "(Used)" if it does and it is in the "Encounter Powers" group it will use the [[Macros:Functions:replace|replace()]] function to replace the " (Used)" with nothing ("") there by removing it from the label. | ||
You can test this by creating a macro on your [[Token | You can test this by creating a macro on your [[Token|token]] called "Something or other (Used)" with a group of "Encounter Powers", impersonating the token and clicking on the "Short Rest" [[Macro_Button|macro button]]. | ||
Line 45: | Line 45: | ||
===Extended Rest, resetting label of buttons in the "Encounter Powers" and "Daily Powers"group=== | ===Extended Rest, resetting label of buttons in the "Encounter Powers" and "Daily Powers"group=== | ||
For an extended rest we want to reset the color of any [[macro buttons]] that start with either "Encounter:" or "Daily:". So create a [[campaign macro]] called "Extended Rest" (don't forget to check the [[ | For an extended rest we want to reset the color of any [[Macro_Button|macro buttons]] that start with either "Encounter:" or "Daily:". So create a [[Introduction_to_Macro_Writing#Campaign Macros|campaign macro]] called "Extended Rest" (don't forget to check the [[Introduction to Macro Writing#Referencing a Token Property in a Macro|Apply to Selected Tokens]] check box) and copy the following code into it | ||
< | <syntaxhighlight lang="mtmacro" line> | ||
[abort(hasImpersonated())] <!-- Abort macro if no token is impersonated --> | [abort(hasImpersonated())] <!-- Abort macro if no token is impersonated --> | ||
Line 66: | Line 66: | ||
}] | }] | ||
[abort(0)] <!-- Suppress the macros output --> | [abort(0)] <!-- Suppress the macros output --> | ||
</ | </syntaxhighlight> | ||
The only difference between this macro and the previous one is where it checks the group. | The only difference between this macro and the previous one is where it checks the group. | ||
For "Short Rest" it is | For "Short Rest" it is | ||
< | <syntaxhighlight lang="mtmacro" line start=14> | ||
[isEnc = matches(getStrProp(props, "group"), "Encounter Powers")] | [isEnc = matches(getStrProp(props, "group"), "Encounter Powers")] | ||
</ | </syntaxhighlight> | ||
Where | Where for "Extended Rest" it is | ||
< | <syntaxhighlight lang="mtmacro" line start=14> | ||
[isEnc = matches(getStrProp(props, "group"), "(Encounter|Daily) Powers")] | [isEnc = matches(getStrProp(props, "group"), "(Encounter|Daily) Powers")] | ||
</ | </syntaxhighlight> | ||
The pattern (Daily|Encounter) Powers matches both the "Daily Powers" and "Encounter Powers" strings. Hopefully from this you can see how to add powers with different durations, say you wanted to add powers that could be used once per round and place them in the "Round Group", for your "New Round" macro which resets the label you would change the lines to | The pattern (Daily|Encounter) Powers matches both the "Daily Powers" and "Encounter Powers" strings. Hopefully from this you can see how to add powers with different durations, say you wanted to add powers that could be used once per round and place them in the "Round Group", for your "New Round" macro which resets the label you would change the lines to | ||
< | <syntaxhighlight lang="mtmacro" line start=14> | ||
[isEnc = matches(getStrProp(props, "group"), "Round Powers")] | [isEnc = matches(getStrProp(props, "group"), "Round Powers")] | ||
</ | </syntaxhighlight> | ||
And for your "Short Rest" you would change it to refresh encounter and round powers | And for your "Short Rest" you would change it to refresh encounter and round powers | ||
< | <syntaxhighlight lang="mtmacro" line start=14> | ||
[isEnc = matches(getStrProp(props, "group"), "(Round|Encounter) Powers")] | [isEnc = matches(getStrProp(props, "group"), "(Round|Encounter) Powers")] | ||
</ | </syntaxhighlight> | ||
And for your "Extended Rest" you would change it to refresh daily, encounter and round powers | And for your "Extended Rest" you would change it to refresh daily, encounter and round powers | ||
< | <syntaxhighlight lang="mtmacro" line start=14> | ||
[isEnc = matches(getStrProp(props, "group"), "(Round|Encounter|Daily) Powers")] | [isEnc = matches(getStrProp(props, "group"), "(Round|Encounter|Daily) Powers")] | ||
</ | </syntaxhighlight> | ||
===Marking Powers as Used=== | ===Marking Powers as Used=== | ||
So now all that is left is to set the color of the buttons when they are used. As of 1.3b48 there is no way to determine which button has been pressed from a macro, but what you can do is to add code like the following to your power macros | So now all that is left is to set the color of the buttons when they are used. As of 1.3b48 there is no way to determine which button has been pressed from a macro, but what you can do is to add code like the following to your power macros | ||
< | <syntaxhighlight lang="mtmacro" line> | ||
[h: setMacroProps("Burning Hands", "Burning Hands (Used)"] | [h: setMacroProps("Burning Hands", "Burning Hands (Used)"] | ||
</ | </syntaxhighlight> | ||
Replacing both occurrences of "Burning Hands" with the label of your [[macro button]]. So lets try it, on your [[Token | Replacing both occurrences of "Burning Hands" with the label of your [[Macro_Button|macro button]]. So lets try it, on your [[Token|token]] create a [[Macro_Button|macro button]] called "Sleep | ||
" in the group called "Daily Powers" and in the button place the following code | " in the group called "Daily Powers" and in the button place the following code | ||
< | <syntaxhighlight lang="mtmacro" line> | ||
Watch, the watch, you are getting sleepy, your eyelids are getting heavy.... [h: setMacroProps("Sleep", "label=Sleep (Used)")] | Watch, the watch, you are getting sleepy, your eyelids are getting heavy.... [h: setMacroProps("Sleep", "label=Sleep (Used)")] | ||
</ | </syntaxhighlight> | ||
Click on the button and hopefully you should see it change. | Click on the button and hopefully you should see it change. | ||
===Multiple Power Buttons with the same name=== | ===Multiple Power Buttons with the same name=== | ||
A word of warning though the above method will change the color of all buttons with that label so if you have duplicates and only want to set one (you may want to implement multi use per day powers as multiple buttons for example) | A word of warning though: the above method will change the color of all buttons with that label so if you have duplicates and only want to set one (you may want to implement multi use per day powers as multiple buttons for example) | ||
Drag a new [[Token | Drag a new [[Token|token]] onto the map and change its name to Lib:DnD4ePowers, and create a [[Macro_Button|macro button]] called "UseDailyPower", then copy in the following code | ||
< | <syntaxhighlight lang="mtmacro" line> | ||
[found = 0] | [found = 0] | ||
[indexes = getMacroIndexes(macro.args)] | [indexes = getMacroIndexes(macro.args)] | ||
Line 130: | Line 130: | ||
}] | }] | ||
}] | }] | ||
</ | </syntaxhighlight> | ||
This will loop through all of the indexes for the [[macro buttons]] with the specified name and if it finds one and it is in the "Daily Powers" group it will append " (Used)" to it. If you have more than one button it will append " (Used)" to each one as you push one of the buttons. | This will loop through all of the indexes for the [[Macro_Button|macro buttons]] with the specified name and if it finds one and it is in the "Daily Powers" group it will append " (Used)" to it. If you have more than one button it will append " (Used)" to each one as you push one of the buttons. | ||
Now create a [[macro button]] called "Lay On Hands" and copy the following in | Now create a [[Macro_Button|macro button]] called "Lay On Hands" and copy the following in | ||
< | <syntaxhighlight lang="mtmacro" line> | ||
Oooh tingly! | Oooh tingly! | ||
[h, macro("UseDailyPower@Lib:DnD4ePowers"): "Lay On Hands"] | [h, macro("UseDailyPower@Lib:DnD4ePowers"): "Lay On Hands"] | ||
</ | </syntaxhighlight> | ||
Duplicate that a few times and then when you click on on of the buttons then one of the "Lay On Hands" buttons will change to "Lay On Hands (Used)". | Duplicate that a few times and then when you click on on of the buttons then one of the "Lay On Hands" buttons will change to "Lay On Hands (Used)". | ||
Fine you say but I would like to stop players using powers that are used (or in the case of multi use powers where there are no non used ones remaining). | Fine you say but I would like to stop players using powers that are used (or in the case of multi use powers where there are no non-used ones remaining). | ||
We can do that by changing the "UseDailyPower" macro we created above on the Lib:DnD4ePowers [[Token:library token|library token]]. Change it to the following | We can do that by changing the "UseDailyPower" macro we created above on the Lib:DnD4ePowers [[Token:library token|library token]]. Change it to the following | ||
< | <syntaxhighlight lang="mtmacro" line> | ||
[found = 0] | [found = 0] | ||
[indexes = getMacroIndexes(macro.args)] | [indexes = getMacroIndexes(macro.args)] | ||
Line 169: | Line 169: | ||
}] | }] | ||
[abort(found)] <!-- Abort the macro if an unused power was not found --> | [abort(found)] <!-- Abort the macro if an unused power was not found --> | ||
</ | </syntaxhighlight> | ||
[[image:PowerUsedDialogGroup.png]] | [[image:PowerUsedDialogGroup.png]] | ||
It ain't pretty but the concept is there and you can easily expand on it to pretty it up. | It ain't pretty but the concept is there and you can easily expand on it to pretty it up. | ||
While we are at it we should add a "UseEncounterPower" macro to Lib:DnD4ePowers | While we are at it we should add a "UseEncounterPower" macro to Lib:DnD4ePowers | ||
< | <syntaxhighlight lang="mtmacro" line> | ||
[found = 0] | [found = 0] | ||
[indexes = getMacroIndexes(macro.args)] | [indexes = getMacroIndexes(macro.args)] | ||
Line 200: | Line 200: | ||
}] | }] | ||
[abort(found)] <!-- Abort the macro if an unused power was not found --> | [abort(found)] <!-- Abort the macro if an unused power was not found --> | ||
</ | </syntaxhighlight> | ||
You can also use this for cases where there is only a single button for a power. | You can also use this for cases where there is only a single button for a power. |
Latest revision as of 23:59, 15 March 2023
Updating the Label of our macro buttons using DnD4e as an Example.
For an example I will use DnD4e powers; when a power is used it will then update the button to show this. Although this example is for DnD4e the technique is applicable for many other systems.
Let's assume that you have macro groups named "Daily Powers" and "Encounter Powers" where we place all of our powers, and we want to set the color of the macro button to blue if the power has been used. If you want another way to do this without using different groups see Tracking Used DnD 4e Powers (Macro Prefix Method)
Version update:
This tutorial was written before 1.3b50. As of 1.3b50 you can use getMacroButtonIndex to determine the index of the button that was pressed. Instead of setting/getting the properties of the button by name you can use the index of the button in place of the name, this way you will not run the risk of updating buttons with the same name.
Short Rest, resetting label of buttons in the "Encounter Powers" group
So first we create a macro button called "Short Rest", for this tutorial I will assume that you create it as a campaign macro --just remember to check the Apply to Selected Tokens check mark-- you can just as easily create these as macros in library token macros and call them from from macro buttons on your tokens.
So create the "Short Rest" macro button and copy the following code into it
[abort(hasImpersonated())] <!-- Abort macro if no token is impersonated -->
[foreach(macro,getMacros()), code: {
<!--
== each label can appear more than once (i.e. more than one
== button with same label, so we need to get all the button
== indexes for a label
-->
[foreach(index, getMacroIndexes(macro)), code: {
[h:props = getMacroProps(index)]
[label = getStrProp(props, "label")]
[isUsed = matches(label, ".* \\(Used\\)")]
[isEnc = matches(getStrProp(props, "group"), "Encounter Powers")]
[if(isUsed && isEnc): setMacroProps(index, "label="+
replace(label, " \\(Used\\)", ""))]
}]
}]
[abort(0)] <!-- Suppress the macros output -->
This macro will get the labels of all the macros with getMacros(). It will then loop through all of these labels and get the index for each macro button with that label. Then it will check to see if the macro label ends with the text "(Used)" if it does and it is in the "Encounter Powers" group it will use the replace() function to replace the " (Used)" with nothing ("") there by removing it from the label.
You can test this by creating a macro on your token called "Something or other (Used)" with a group of "Encounter Powers", impersonating the token and clicking on the "Short Rest" macro button.
Extended Rest, resetting label of buttons in the "Encounter Powers" and "Daily Powers"group
For an extended rest we want to reset the color of any macro buttons that start with either "Encounter:" or "Daily:". So create a campaign macro called "Extended Rest" (don't forget to check the Apply to Selected Tokens check box) and copy the following code into it
[abort(hasImpersonated())] <!-- Abort macro if no token is impersonated -->
[foreach(macro,getMacros()), code: {
<!--
== each label can appear more than once (i.e. more than one
== button with same label, so we need to get all the button
== indexes for a label
-->
[foreach(index, getMacroIndexes(macro)), code: {
[h:props = getMacroProps(index)]
[label = getStrProp(props, "label")]
[isUsed = matches(label, ".* \\(Used\\)")]
[isEnc = matches(getStrProp(props, "group"), "(Encounter|Daily) Powers")]
[if(isUsed && isEnc): setMacroProps(index, "label="+
replace(label, " \\(Used\\)", ""))]
}]
}]
[abort(0)] <!-- Suppress the macros output -->
The only difference between this macro and the previous one is where it checks the group. For "Short Rest" it is
[isEnc = matches(getStrProp(props, "group"), "Encounter Powers")]
Where for "Extended Rest" it is
[isEnc = matches(getStrProp(props, "group"), "(Encounter|Daily) Powers")]
The pattern (Daily|Encounter) Powers matches both the "Daily Powers" and "Encounter Powers" strings. Hopefully from this you can see how to add powers with different durations, say you wanted to add powers that could be used once per round and place them in the "Round Group", for your "New Round" macro which resets the label you would change the lines to
[isEnc = matches(getStrProp(props, "group"), "Round Powers")]
And for your "Short Rest" you would change it to refresh encounter and round powers
[isEnc = matches(getStrProp(props, "group"), "(Round|Encounter) Powers")]
And for your "Extended Rest" you would change it to refresh daily, encounter and round powers
[isEnc = matches(getStrProp(props, "group"), "(Round|Encounter|Daily) Powers")]
Marking Powers as Used
So now all that is left is to set the color of the buttons when they are used. As of 1.3b48 there is no way to determine which button has been pressed from a macro, but what you can do is to add code like the following to your power macros
[h: setMacroProps("Burning Hands", "Burning Hands (Used)"]
Replacing both occurrences of "Burning Hands" with the label of your macro button. So lets try it, on your token create a macro button called "Sleep " in the group called "Daily Powers" and in the button place the following code
Watch, the watch, you are getting sleepy, your eyelids are getting heavy.... [h: setMacroProps("Sleep", "label=Sleep (Used)")]
Click on the button and hopefully you should see it change.
Multiple Power Buttons with the same name
A word of warning though: the above method will change the color of all buttons with that label so if you have duplicates and only want to set one (you may want to implement multi use per day powers as multiple buttons for example)
Drag a new token onto the map and change its name to Lib:DnD4ePowers, and create a macro button called "UseDailyPower", then copy in the following code
[found = 0]
[indexes = getMacroIndexes(macro.args)]
[foreach(button, indexes), code: {
[if(found==0), code: {
[props = getMacroProps(button)]
[label = getStrProp(props, "label")]
[isPower = matches(label, macro.args)]
[isDay = matches(getStrProp(props, "group"),
"Daily Powers")]
[if(isPower == 1 && isDay == 1 && found == 0):
setMacroProps(button, "label=" + label + " (Used)")
]
[if(isPower == 1 && isDay == 1 && found == 0): found=1]
}]
}]
This will loop through all of the indexes for the macro buttons with the specified name and if it finds one and it is in the "Daily Powers" group it will append " (Used)" to it. If you have more than one button it will append " (Used)" to each one as you push one of the buttons.
Now create a macro button called "Lay On Hands" and copy the following in
Oooh tingly!
[h, macro("UseDailyPower@Lib:DnD4ePowers"): "Lay On Hands"]
Duplicate that a few times and then when you click on on of the buttons then one of the "Lay On Hands" buttons will change to "Lay On Hands (Used)".
Fine you say but I would like to stop players using powers that are used (or in the case of multi use powers where there are no non-used ones remaining).
We can do that by changing the "UseDailyPower" macro we created above on the Lib:DnD4ePowers library token. Change it to the following
[found = 0]
[indexes = getMacroIndexes(macro.args)]
[foreach(button, indexes), code: {
[if(found==0), code: {
[props = getMacroProps(button)]
[label = getStrProp(props, "label")]
[isPower = matches(label, macro.args)]
[isDay = matches(getStrProp(props, "group"),
"Daily Powers")]
[if(isPower == 1 && isDay == 1 && found == 0):
setMacroProps(button, "label=" + label + " (Used)")
]
[if(isPower == 1 && isDay == 1 && found == 0): found=1]
}]
}]
<!-- if "free" one is not found then inform user they can't do it -->
[if(found==0), code: {
[dialog("PowerUsed"): {
<title>Can Not Use Power</title>
<meta name="temporary" content="true">
You have already used [r: macro.args]
}]
}]
[abort(found)] <!-- Abort the macro if an unused power was not found -->
It ain't pretty but the concept is there and you can easily expand on it to pretty it up.
While we are at it we should add a "UseEncounterPower" macro to Lib:DnD4ePowers
[found = 0]
[indexes = getMacroIndexes(macro.args)]
[foreach(button, indexes), code: {
[if(found==0), code: {
[props = getMacroProps(button)]
[label = getStrProp(props, "label")]
[isPower = matches(label, macro.args)]
[isEnc = matches(getStrProp(props, "group"),
"Daily Powers")]
[if(isPower == 1 && isEnc == 1 && found == 0):
setMacroProps(button, "label=" + label + " (Used)")
]
[if(isPower == 1 && isEnc == 1 && found == 0): found=1]
}]
}]
<!-- if "free" one is not found then inform user they can't do it -->
[if(found==0), code: {
[dialog("PowerUsed"): {
<title>Can Not Use Power</title>
<meta name="temporary" content="true">
You have already used [r: macro.args]
}]
}]
[abort(found)] <!-- Abort the macro if an unused power was not found -->
You can also use this for cases where there is only a single button for a power.
You can download this part of the tutorial in in a campaign file which was made using MapTool 1.3b48.