Playmaker Forum

PlayMaker Help & Tips => PlayMaker Help => Topic started by: wirejaw on November 20, 2020, 07:10:52 PM

Title: Send an Event when using Destroy Object? [SOLVED]
Post by: wirejaw on November 20, 2020, 07:10:52 PM
I'm using Destroy Object to destroy a bomb that is thrown by my player character.  Is there a way to send an event when the object is destroyed?  I want to add other things like a particle explosion and a screen shake when the bomb explodes.  Thanks for any help.
Title: Re: Send an Event when using Destroy Object?
Post by: Broken Stylus on November 21, 2020, 06:36:26 AM
In a FSM you can use a System Event event that's called DISABLE which lets you throw some functions within the object's last frame of activity before going off (as it receives the order to deactivate itself from Unity. Don't use any action that would last longer and, generally, make it short no matter what.

For particles there's also an option called Stop Action in the main settings that allows you to disable or destroy the object once the particle effect is finished.
Title: Re: Send an Event when using Destroy Object?
Post by: djaydino on November 21, 2020, 09:58:58 AM
Hi.
On the 'Bomb' when you Create it (store the object when you create), you can use a 'Set Fsm Gameobject' and set the 'player' gameobject  (on the bomb Game object/fsm).

Now just before destroying the bomb you can use a Send Event By Name to  the 'player'

Here are some videos that might help :


Title: Re: Send an Event when using Destroy Object?
Post by: wirejaw on November 21, 2020, 08:30:59 PM
Thank you both for your replies and suggestions.  I will give these ideas a try.
Title: Re: Send an Event when using Destroy Object?
Post by: Broken Stylus on November 22, 2020, 06:17:57 AM
When the bomb blows up, maybe you'd want to spawn (or pick from a pool) an instance of a prefab that receives the last coordinates of the bomb. Set the instance's parent in some logical "folder" I would say so you can let the bomb game object be destroyed or repooled without worrying about it waiting for some particles effect to complete, because the particles effect would be on a different object. You would essentially be swapping objects here within the same frame, before the graphic rendering phase.
Title: Re: Send an Event when using Destroy Object?
Post by: wirejaw on December 10, 2020, 06:51:31 PM

On the 'Bomb' when you Create it (store the object when you create), you can use a 'Set Fsm Gameobject' and set the 'player' gameobject  (on the bomb Game object/fsm).

Now just before destroying the bomb you can use a Send Event By Name to  the 'player'

v=0D815G0Uwkw&t=&ab_channel=JinxterGames

djaydino - I'm revisiting this and still having issues understanding how to get this to work (I also watched both videos you provided - thank you - but having trouble understanding some of the concepts).

I've managed to get most of what I need to work without using anything global by building things into my "bomb" prefab (explosion force, particles, animated point light, etc.).  I have my Player object set to Create Object with each button press to "throw" the bomb prefab, and after 2 seconds it blows up.  However, I still want to add a camera shake (and probably other actions) to each generated bomb object when it blows up.

This is where it appears I need to use global events/transitions to add a shake to my camera by communicating between objects.  For my Camera, I am using Pro Camera 2D, and I have successfully added camera shaking to other game actions, like when doing a ground pound I have a shake using the "PC2D Shake With Values" action on the camera, and that works fine.

In your suggestion, I can store the Bomb when it's created, but I'm confused by the following steps, so could you elaborate a bit?  Apologies if this is basic stuff and thanks much for any help!
 
Title: Re: Send an Event when using Destroy Object?
Post by: djaydino on December 11, 2020, 06:21:59 AM
Hi.
For your camera you can set your camera as a global (gameobject)

For this its good to have it on a global as many objects might need to reference it.

I use Pro Camera 2D as well and i have some custom actions i made.

I added then in the attachment below, they might be useful :)
Title: Re: Send an Event when using Destroy Object?
Post by: wirejaw on December 11, 2020, 04:49:06 PM
Cool thanks I'll take a look!
Title: Re: Send an Event when using Destroy Object?
Post by: wirejaw on January 04, 2021, 09:30:10 PM
I've come back to this yet again and still can't figure out how to get my camera shake to trigger when each of the prefab bombs explode.

-Player presses button to throw bomb (using "Create Object" to spawn a prefab bomb).
-Bomb (prefab) is thrown in direction player is facing and explodes after 2 seconds (particles, light, etc. are built into the prefab and play fine).

I want to trigger the "PC2D Shake With Values" action when the bomb explodes.  I'm using this same action successfully to shake the camera using other FSM's, so I know it actually works, I just can't figure out how to trigger it with each prefab bomb explosion.

When the bomb is thrown, I can Store Object (let's call it "StoredBomb").  Is StoredBomb what needs to be referenced somehow and then used to call the Shake With Values action? How do I reference StoredBomb and tell it to play the screen shake action?

Apologies if the solution is basic but I may need some hand holding as I'm still finding my way around.  Thanks much.
Title: Re: Send an Event when using Destroy Object?
Post by: Broken Stylus on January 05, 2021, 08:33:03 AM
Prefabs cannot reference objects in the scene directly, you have to store them as targets in FsmGameObject variables on a G.O. that is at the root of your hierarchy and use an action like Get Parent Advanced that allows you to go up the hierarchy (parent = 0; grandparent = 1; great-grandpa = 2; etc.).

An alternative is to attach tons of data to an event and either use "Broadcast to all" or "Send to children", and then all instances that have a FSM that's listening to this event will therefore receive it and will have, in the state receiving the event, an action to collect the data.

You can have your bomb prefab set up so instances of the bomb send an event to the camera (Shake Cam) when they explode. You can attach some data to events right before sending with Set Data Event and get it on the receiving end with Get Event Info, or you can use Get and Set Event Properties.
Title: Re: Send an Event when using Destroy Object?
Post by: djaydino on January 05, 2021, 08:33:49 AM
Hi.
The explode is handled with a fsm on the created bomb?

You can do the shake there.

to target the camera, create a global (call it 'Main Camera' for example)
On the start of you scene use 'Set GameObject' and set the camera object to the global variable.

Then on the action "PC2D Shake With Values" on the prefab bomb use the global variable as the 'Main Camera'

Btw you also might want to look into pooling, Create/Destroy are 'heavy' cpu usages.
Title: Re: Send an Event when using Destroy Object?
Post by: wirejaw on January 05, 2021, 11:27:06 AM
That worked.  Thank you so much djdino.  Also thanks for the tip about pooling.  That is on my list of things to learn in the near future.