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

[HELP] A quick question about efficiency/optimization

1533079823
Missingquery
Pro
Sheet Author
API Scripter
I am trying to make it so that my script runs whenever a token enters the aura of another token. Now, I could probably achieve this by checking for this every single time a token on the page moves, but that would make things incredibly slow, and probably wouldn't be very efficient, either. Is there a more efficient way that I haven't thought of, or is this the only solution?
1533081566

Edited 1533133530
The Aaron
Pro
API Scripter
The efficiency will come from how you manage the data. If you want to go a bit overboard, you could look into Binary Space Partition Trees (BSP Trees), but probably keeping a per page index of token location, aura radius, and id will be plenty fast. Then do your lookup in the index to get the id of all the tokens in range on the page. You would build the index at start up, then adjust it when tokens are added and removed, or their auras are changed. You will probably have a short pretty short list for a given page. One trick you can use is comparing the squared distance, as the square root of in the Pythagorean theorem is the expensive part. (This is what we did in the game industry for distance comparisons.)  For example, if the aura is 10 and the token moved is 8 down and 5 right, it's faster to check if (10*10) > (  (8*8) + (5*5) ) , than check if 10 > sqrt( (8*8) + (5*5) ) .