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

Unsure how to set up spawning variable FX

Here’s another situation where I grasp the general idea, but I’m not sure how to write my particular idea. Trying to add an extra bit of functionality and flair to my comprehensive attack/ damage roll.  A new field on the sheet now contains the FX attribute, so that each repeating weapon can have its own paired FX.  I want to have the unique weapon fx go off between attacker and target when that weapon goes through the script.  From what I understand you must use the " spawnFxBetweenPoints(point1, point2, type, pageid)" function to do this once you go through the API?  If so, how can I substitute the target and character token ids for point1, point2, and the custom fx variable for type?  I guess it would be something where you get the left and top properties of the target and character tokens, right?   My test script uses 3 arguments sent by an ability button: @{selected|token_id} @{target|token_id} @{weapon_fx_rep} which become: const ctoken = args[1]; const ttoken = args[2]; const fx = args[3]; NOTE: The full script does grab character ids for the target and caster.  If there’s a way to get the location info from the default token object I suppose that can work too, but I’d need some guidance on that too.  Thanks!  
1559224912

Edited 1559225599
GiGs
Pro
Sheet Author
API Scripter
I've never used it before, but looking at the wiki, it's expecting this format spawnFxBetweenPoints ({ x: 1400 , y: 1400 }, { x: 2100 , y: 2100 }, 'beam-acid' ); So you need to grab the x and y coordinates of the relevant tokens. I'd create a function and pass it the token id, and return the x and y coordinates in the above format. Something like const tokenCenter = (id) => {     const token = getObj ( 'graphic' , id); const left = token.get('left'); const top = token.get('top'); const width = token.get('width'); const height = token.get('height'); const coords = (left, width, top, height ) => `{ x: ${Math.round(left + width/2)}, y: ${Math.round(top - height/2)} }`; return coords(left, width, top, height); }; Or in a shorter form const tokenCenter = (id) => {     const token = getObj ( 'graphic' , id); return `{ x: ${Math.round(token.get('left') + token.get('width')/2)}, y: ${Math.round(token.get('top') - token.get('height')/2)} }`; }; then in your script spawnFxBetweenPoints (tokenCenter(ctoken), tokenCenter(ttoken), fx ); I haven't tested this, but it looks right. Edit: corrected code
Thanks Gigs. I'm not sure why this isn't working; my logs show the correct coordinates, and fx id being sent to the spawn function.  I tried changing my custom fx call to "beam-acid" to see if that was the issue but there is still no FX spawn. (I forgot to mention that some of these are custom FX so they require a line like  "let fxid = findObjs({_type: "custfx", name: fx})[0].id;" to find them.  But as mentioned, that doesn't seem to be the problem.)
1559231508

Edited 1559231517
GiGs
Pro
Sheet Author
API Scripter
That's probably my mistake, change the return line in the short script to: return {     x: Math.round(token.get('left') + token.get('width')/2),     y: Math.round(token.get('top') - token.get('height')/2) };
Oh nice, that's got it working.   It's firing off to the top-right of the selected token - is there a way to make it more on-center?
1559233857

Edited 1559233926
GiGs
Pro
Sheet Author
API Scripter
I thought the coordinates I gave would have it sending to and from the centre. The +width/2 and -height/2 terms are supposed to centre the fx You could try removing the height and width terms and see where it goes this time, then adjust till you get it where it should be. 
Ooh, yeah, haha, that was it.  Just removing them leaves it on center.  Thank you!
1559238854
GiGs
Pro
Sheet Author
API Scripter
You're welcome :) I guess the fx is configured to go from the centre automatically.
OOPS, broke it again.   I'm trying to set it up so Melee attacks cause the fx spawn to happen in a slightly different way, with the blood splatter or whatever effect only showing up on the target.  So it's no longer a SpawnFXBetween function - it's just SpawnFX.  However, these functions list their parameters in a different way.  Where the Between uses ({x: 100, y: 200}, {x:200, y: 100}, type), I believe Spawn FX just uses (x, y, type).  I tried to give that a conditional and where if the skill is Melee it sends only the coordinates, but I'm getting a "TypeError: Cannot read property 'indexOf' of undefined."   Here's what I changed it to:   const tokenCenter = (id) => {        const token = getObj('graphic', id);    if (skill == "Melee") {        return Math.round(token.get('left')), Math.round(token.get('top'))    } else {    return {     x: Math.round(token.get('left')),      y: Math.round(token.get('top'))    }    } }; And below: if (skill == "Melee") {             spawnFx(tokenCenter(ttoken), fxid)             } else if (skill == "Ballistics") {             spawnFxBetweenPoints(tokenCenter(ctoken), tokenCenter(ttoken), fxid);             log(tokenCenter(ttoken));             }
1559254940
The Aaron
Roll20 Production Team
API Scripter
GiGs said: I thought the coordinates I gave would have it sending to and from the centre. The +width/2 and -height/2 terms are supposed to centre the fx You could try removing the height and width terms and see where it goes this time, then adjust till you get it where it should be.  Left and Top refer to the center of a Graphic, basically the rotation point.  Not immediately obvious.  You can see this with TokenMod: !token-mod --set left|35 top|35
1559255071
The Aaron
Roll20 Production Team
API Scripter
Ryan B. said: OOPS, broke it again.   I'm trying to set it up so Melee attacks cause the fx spawn to happen in a slightly different way, with the blood splatter or whatever effect only showing up on the target.  So it's no longer a SpawnFXBetween function - it's just SpawnFX.  However, these functions list their parameters in a different way.  Where the Between uses ({x: 100, y: 200}, {x:200, y: 100}, type), I believe Spawn FX just uses (x, y, type).  I tried to give that a conditional and where if the skill is Melee it sends only the coordinates, but I'm getting a "TypeError: Cannot read property 'indexOf' of undefined."   Here's what I changed it to:   const tokenCenter = (id) => {        const token = getObj('graphic', id);    if (skill == "Melee") {        return Math.round(token.get('left')), Math.round(token.get('top'))    } else {    return {     x: Math.round(token.get('left')),      y: Math.round(token.get('top'))    }    } }; And below: if (skill == "Melee") {             spawnFx(tokenCenter(ttoken), fxid)             } else if (skill == "Ballistics") {             spawnFxBetweenPoints(tokenCenter(ctoken), tokenCenter(ttoken), fxid);             log(tokenCenter(ttoken));             } Leave the tokenCenter() function the way it was and change to this: if (skill == "Melee") { let p = tokenCenter(ttoken); spawnFx(p.x, p.y, fxid) } else if (skill == "Ballistics") { spawnFxBetweenPoints(tokenCenter(ctoken), tokenCenter(ttoken), fxid); log(tokenCenter(ttoken)); }
Oh nice!  Thanks Aaron.  How does that work, with p grabbing the x and y?  I didn't think the x and y values were attached to anything; I thought that part of the return was just text.
1559263184
The Aaron
Roll20 Production Team
API Scripter
No, it returns an object. {x: 2, y: 2}, the object is stored in the variable p, and then you access the properties on it.
1559283204
GiGs
Pro
Sheet Author
API Scripter
The Aaron said: GiGs said: I thought the coordinates I gave would have it sending to and from the centre. The +width/2 and -height/2 terms are supposed to centre the fx You could try removing the height and width terms and see where it goes this time, then adjust till you get it where it should be.  Left and Top refer to the center of a Graphic, basically the rotation point.  Not immediately obvious.   Aha, my inexperience of messing with tokens shows. I'd never have guessed that. :)