Difference between revisions of "Mod:Basic Examples"
From Roll20 Wiki
Andreas J. (Talk | contribs) m (1223200 moved page API:Basic Examples to Mod:Basic Examples) |
|||
(8 intermediate revisions by 2 users not shown) | |||
Line 1: | Line 1: | ||
− | + | {{revdate}}{{HCbox| {{hc|articles/360037256814-API-Basic-Examples Here}} }} | |
− | + | {{clear}}{{apibox}} | |
− | + | ||
− | + | ||
− | + | ||
− | {{apibox}} | + | |
− | + | ||
− | + | ||
+ | Here are some examples of simple scripts that can help you get started. Feel free to take them and modify them for use in your own campaigns. There's also an {{forum|category/46806 Mod Scripts}} where you can discuss Mod Scripting and share your scripts with the community. | ||
+ | __TOC__ | ||
====Bloodied and Dead Status Markers==== | ====Bloodied and Dead Status Markers==== | ||
''(Contributed by Ken Bauer)'' | ''(Contributed by Ken Bauer)'' | ||
Line 13: | Line 9: | ||
This script automatically adds the Red marker to represent the "bloodied" state for any tokens that drop below half their health, and the "dead" marker for any that drop to 0 or less. It's assumed that health is stored in Bar 1. | This script automatically adds the Red marker to represent the "bloodied" state for any tokens that drop below half their health, and the "dead" marker for any that drop to 0 or less. It's assumed that health is stored in Bar 1. | ||
− | <pre data-language="javascript"> | + | <pre data-language="javascript">on("change:graphic", function(obj) { |
− | on("change:graphic", function(obj) { | + | |
if(obj.get("bar1_max") === "") return; | if(obj.get("bar1_max") === "") return; | ||
Line 43: | Line 38: | ||
====The Darkness is Closing In...==== | ====The Darkness is Closing In...==== | ||
− | This script reduces the light radius of a token by 10% every time that token moves. Great for simulating "lamps running out of oil" or similar high-stress situations. | + | This script reduces the light radius of a [[token]] by 10% every time that token moves. Great for simulating "lamps running out of oil" or similar high-stress situations. |
− | <pre data-language="javascript"> | + | <pre data-language="javascript">on("change:token", function(obj, prev) { |
− | on("change:token", function(obj, prev) { | + | |
//Only do this if we actually moved. | //Only do this if we actually moved. | ||
if(obj.get("left") == prev["left"] && obj.get("top") == prev["top"]) return; | if(obj.get("left") == prev["left"] && obj.get("top") == prev["top"]) return; | ||
Line 56: | Line 50: | ||
====sendChat as a Player or Character==== | ====sendChat as a Player or Character==== | ||
− | ''(Contributed by Brian Shields)'' | + | ''(Contributed by [[Brian Shields]])'' |
− | This | + | This script provides a few lines you can use in a <code>chat:message</code> event script to ensure you're sending a message with {{{c|sendChat}} accurately as either a [[Player]] or a [[Character]], depending on who triggered the event. |
− | <pre data-language="javascript"> | + | <pre data-language="javascript">on("chat:message", function(msg) { |
− | on("chat:message", function(msg) { | + | |
var message = ''; | var message = ''; | ||
// Determine the contents of `message' | // Determine the contents of `message' | ||
Line 73: | Line 66: | ||
}); | }); | ||
</pre> | </pre> | ||
− | [[Category:API | + | |
+ | [[Category:API Development]] |
Latest revision as of 09:03, 9 June 2024
Page Updated: 2024-06-09 |
Attention: This page is community-maintained. For the official Roll20 version of this article, see the Help Center for assistance: Here .
Roll20 Mod
Use Mods
- Use & Install
- Mod:Script Index & Suggestions
- Short Community Scripts
- Meta Scripts
- User Documentation
- Mod Scripts(Forum)
- Mod Update 2024🆕
- Macro Guide
Mod Development
Reference
- Objects
- Events
- Chat Events & Functions
- Utility Functions
- Function
- Roll20 object
- Token Markers
- Sandbox Model
- Debugging
Cookbook
- Cookbook
- Basic Examples
- Advanced Examples
- Best Practices
Here are some examples of simple scripts that can help you get started. Feel free to take them and modify them for use in your own campaigns. There's also an Mod Scripts(Forum) where you can discuss Mod Scripting and share your scripts with the community.
Contents |
[edit] Bloodied and Dead Status Markers
(Contributed by Ken Bauer)
This script automatically adds the Red marker to represent the "bloodied" state for any tokens that drop below half their health, and the "dead" marker for any that drop to 0 or less. It's assumed that health is stored in Bar 1.
on("change:graphic", function(obj) { if(obj.get("bar1_max") === "") return; if(obj.get("bar1_value") <= obj.get("bar1_max") / 2) { obj.set({ status_redmarker: true }); } else{ obj.set({ status_redmarker: false }) } if(obj.get("bar1_value") <= 0) { obj.set({ status_dead: true }); } else { obj.set({ status_dead: false }); } });
[edit] The Darkness is Closing In...
This script reduces the light radius of a token by 10% every time that token moves. Great for simulating "lamps running out of oil" or similar high-stress situations.
on("change:token", function(obj, prev) { //Only do this if we actually moved. if(obj.get("left") == prev["left"] && obj.get("top") == prev["top"]) return; obj.set({ light_radius: Math.floor(obj.get("light_radius") * 0.90) }); });
[edit] sendChat as a Player or Character
(Contributed by Brian Shields)
This script provides a few lines you can use in a chat:message
event script to ensure you're sending a message with {sendChat
accurately as either a Player or a Character, depending on who triggered the event.
on("chat:message", function(msg) { var message = ''; // Determine the contents of `message' var characters = findObjs({_type: 'character'}); var speaking; characters.forEach(function(chr) { if(chr.get('name') == msg.who) speaking = chr; }); if(speaking) sendChat('character|'+speaking.id, message); else sendChat('player|'+msg.playerid, message); });