Hi guys, I’m just a beginner here, trying to figure out particle fx for the Getting Started Tutorial. I want a particle effect to happen when the frog picks up a coin, right where it picks up the coin.
-
So far I’ve got an object in my main collection, with the desired particle effect attached to it.
-
In the on_message function, in my coin.script, I have a coditional statement to activate the fx…
– code fragment
if self.collected == false and message_id == hash(“collision_response”) then
self.collected = true
msg.post(“gui#gui”, “add_score”, {amount = score})
msg.post("#sprite", “disable”)
particlefx.play("/coin_fx#particlefx") – THIS IS THE IMPORTANT LINE HERE!
– end code fragment
Right now when I collect a coin the effect goes off on the right hand side of the screen far away from where the coin collecting takes place. Admittedly this is probably because of where I have the particlefx object set up in the main collection. Is there a way to set up the fx programatically (or otherwise) in relation to the position of the collected coin? I’ve had a look through the forums, documentation, and tutorials but I haven’t found much about manipulating fx cooridnates via a lua script. Any help would be appreciated, thanks. 
The fx component is in a gameobject ( namely "/coin_fx ") which means you can move that gameobject around as much as you want to with: go.set_position()
So before you call play you could just set the position to the coin’s gameobject position.
Now this would probably work fine for most times but if you would like to enhance it there is still a small problem.
If your particle fx is a long one (long duration that is) you will probably see that the emitter can only be in one place at a time so if you pick a new coin before the effect has been played out on the old one the fx object will move to the new location and start playing there instead leaving the old one not finished. So here is option 2:
Instead of placing your fx object in a collection directly you can create a gameobject with a factorycomponent.
Factories creates new gameobjects in runtime and also let you set the position where the new gameobject should be spawned.
So instead of moving your fx around you just create new effects where you want the fx to be. This way you can have several fx on screen at the same time and type.
If going with option 2 you should write a small script for the fx object so it starts on it’s own in init function and even more importantly removes itself when the fx has been played out.
3 Likes
IIRC, you should be able to just put the particlefx component straight onto the coin game object and play it as the coin is hit, which would give it the correct position. Normally components are deleted along with the owning game object, but particlefx are special in this regard and remain in the world until they have stopped playing. The reasoning behind it is how often this is the behaviour you want, i.e. a ‘death effect’. So as long as the play mode is ‘once’, and the script fires it off through particlefx.play, the engine will remove the particlefx as they have finished playing.
7 Likes
Thank you so much! Very helpful 
Awesome, cheers for the help 