Skip to content

Instantly share code, notes, and snippets.

@randyhoyt
Last active August 29, 2015 14:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save randyhoyt/1a8fc35136fb07e0c525 to your computer and use it in GitHub Desktop.
Save randyhoyt/1a8fc35136fb07e0c525 to your computer and use it in GitHub Desktop.
Code I use in the Roll20 API to make it easy for players to return lanterns to the supply by dropping them in one of two "return" boxes. See comments for line-by-line explanation.
var locations = {};
// blues // you'll to need to know the id of each object
locations["-JNL9wFJ1utz-benCwRR"] = {left:1715,top:665};
locations["-JNL9AedkClTVq5shQW6"] = {left:1802,top:665};
locations["-JNL99iugvAJkfivJtXX"] = {left:1890,top:665};
locations["-JNL9AIuUfnYkIR5Cr2v"] = {left:1977,top:665};
locations["-JNL98m8z0S7ukGPJx1e"] = {left:2065,top:665};
locations["-JNL97z0KJ-p67PQ_nUy"] = {left:2152,top:665};
locations["-JNL94ZnHe0pAj1OaKUq"] = {left:2240,top:665};
locations["-JNL94seH5o-TrLOLXbw"] = {left:2327,top:665};
locations["-JNL7wBZardlVQRs3D-0"] = {left:2415,top:665};
// green
locations["-JNL9vNnSLhgOJz_lwzy"] = {left:1715,top:752};
locations["-JNL9fhz6V0TUwUm_p5S"] = {left:1802,top:752};
locations["-JNL9oooCw3A3sFLUTil"] = {left:1890,top:752};
locations["-JNL9nWTfi1-271CZYLJ"] = {left:1977,top:752};
locations["-JNL9qraSRrrhAs53Nfp"] = {left:2065,top:752};
locations["-JNL9rp3OLgCGyfTsxFm"] = {left:2152,top:752};
locations["-JNL9sbYRKg1b-yaty_q"] = {left:2240,top:752};
locations["-JNL9tdR69iWGLH53j30"] = {left:2327,top:752};
locations["-JNL9uPBCC4pZgtL3npt"] = {left:2415,top:752};
// red
locations["-JNLAEzF-cyCmCFAayKN"] = {left:1715,top:840};
locations["-JNLA5UG4Cq1ciNpXyDI"] = {left:1802,top:840};
locations["-JNLA9Y8aiV9Z8lET3lo"] = {left:1890,top:840};
locations["-JNLAAAs3e48UIXdweA0"] = {left:1977,top:840};
locations["-JNLAAq85UrJGd_SNjml"] = {left:2065,top:840};
locations["-JNLABV-8Y347IRJpqjp"] = {left:2152,top:840};
locations["-JNLACHz3Uyuak1bkb-p"] = {left:2240,top:840};
locations["-JNLADG738b9BxO_xEAZ"] = {left:2327,top:840};
locations["-JNLADusrOURn69xTG2V"] = {left:2415,top:840};
// yellow
locations["-JNLAOlMtaZOZMcSRF7k"] = {left:1715,top:927};
locations["-JNLAVhSlVfATVrbksFu"] = {left:1802,top:927};
locations["-JNLAd4nyj_jwzGYTid0"] = {left:1890,top:927};
locations["-JNLAadZaxwn6CdPbnby"] = {left:1977,top:927};
locations["-JNLA_v_oTHrBwD5d2Jl"] = {left:2065,top:927};
locations["-JNLAZykvgH1bX8gczeN"] = {left:2152,top:927};
locations["-JNLAZ8LuwYFvzVp17KF"] = {left:2240,top:927};
locations["-JNLAYHoC61K2oGla7zv"] = {left:2327,top:927};
locations["-JNLAXPjDfjrjFxVTtkF"] = {left:2415,top:927};
// pink
locations["-JNLAmoYZrUZa4gN0s2x"] = {left:1715,top:1015};
locations["-JNLAs6XQOWfP7DBc-2M"] = {left:1802,top:1015};
locations["-JNLAt0WEE_9zRt04D99"] = {left:1890,top:1015};
locations["-JNLAtptLpA_NfPyuggn"] = {left:1977,top:1015};
locations["-JNLAuWstVIm3QYCLKJu"] = {left:2065,top:1015};
locations["-JNLAvN-USAmV3a9HNCj"] = {left:2152,top:1015};
locations["-JNLAwA-4mJO5Crbg1GR"] = {left:2240,top:1015};
locations["-JNLAws0v_VDtLlIXlzt"] = {left:2327,top:1015};
locations["-JNLAxc-mll_wCbw9iKs"] = {left:2415,top:1015};
// white
locations["-JNLBWf-XB4xgAiQhJIO"] = {left:1715,top:1102};
locations["-JNLBi9fURKC1hpcmvaS"] = {left:1802,top:1102};
locations["-JNLBh9IcV9Vd6rXqN4m"] = {left:1890,top:1102};
locations["-JNLBgHo669Byn34CB6-"] = {left:1977,top:1102};
locations["-JNLBfX3uaIKoKFhl3Dm"] = {left:2065,top:1102};
locations["-JNLBeXZ9MFUyN_D2ysc"] = {left:2152,top:1102};
locations["-JNLBdIR_0vzer-zuNkg"] = {left:2240,top:1102};
locations["-JNLBah4arzhBaowfDUZ"] = {left:2327,top:1102};
locations["-JNLBbjgN8dVNcN6UuE1"] = {left:2415,top:1102};
// black
locations["-JNLC3cE3l6ekwO7NEEh"] = {left:1715,top:1190};
locations["-JNLC7jofNeTCEYVHz7R"] = {left:1802,top:1190};
locations["-JNLCAnYlCcdaEAuXsfu"] = {left:1890,top:1190};
locations["-JNLCBevaKfdIZOkB0rR"] = {left:1977,top:1190};
locations["-JNLCCevzifBpdLsA5yN"] = {left:2065,top:1190};
locations["-JNLCDexKplc9YZXAsW9"] = {left:2152,top:1190};
locations["-JNLCEImLKA0_U9LbPUt"] = {left:2240,top:1190};
locations["-JNLCF0Pd8g5CNBa6NTR"] = {left:2327,top:1190};
locations["-JNLCG2AYoAyxY4FMl9D"] = {left:2415,top:1190};
on('change:graphic', function(obj) {
if (locations[obj.get("_id")]) {
x = obj.get("left");
y = obj.get("top");
if ( (x > 4767 && x < 5368 && y > 476 && y < 718 ) ||
(x > 2267 && x < 2872 && y > 2416 && y < 2691) )
{
obj.set("left", locations[obj.get("_id")].left);
obj.set("top", locations[obj.get("_id")].top);
}
}
});
@randyhoyt
Copy link
Author

Lines 1-78 sets the coordinates for the starting positions for each resource. I have 63 resources (nine each in seven different colors), so I have a JavaScript object with 63 properties. Each property is the ID of a resource, and the value for that object is another object with a left and a top property for the resources starting coordinates.

Lines 80-91 contains the functional code for this script. It listens for any changes to a graphic; when a change happens, this code is executed. The graphic that changed is available inside this function as the obj variable.

Line 81 is a conditional that checks if the ID of the changed graphic is in the list of resources. If it's not one of these resources, then nothing happens.

Line 82 and 83 only runs if the changed graphic is one of the resources; it loads the current coordinates of the resource into x and y variables for easy access.

Line 84 only runs if the changed graphic is one of the resources; it checks to see if the resource is currently located within one of the two return boxes. If it's not, then nothing happens.

Line 87 and 88 only run if the changed graphic is one of the resources and it is now located within a return box. Line 87 changes the resource's left coordinate to match its starting left position. Line 88 changes the graphic's top coordinate to match is starting top position. These two lines together move the resource to its starting position.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment