playMaker

Author Topic: Handling multiple simultaneous Trigger Events  (Read 8146 times)

Jernau

  • Playmaker Newbie
  • *
  • Posts: 19
Handling multiple simultaneous Trigger Events
« on: July 26, 2012, 08:33:09 AM »
I have an FSM (let's call it A) attached to an "Is Trigger" Box Collider, which uses the "Trigger Event" Action to detect a collision and notify another FSM (let's call it B).

This setup worked perfectly when A only detects one collision, but when it detects multiple simultaneous collisions I hit a problem. That's because FSM B has to wait for an animation to complete before it can complete the processing of the collision, and while it's waiting for the animation it receives additional events from FSM A for the other simultaneous collisions, stopping FSM B from completing processing of the first collision.

If I remove the animation wait in FSM B then everything behaves as expected, in other words FSM B has time to complete all other processing before FSM A sends the next collision event. However, I need this animation wait for the game to behave properly.

My first attempt at a solution was to add a wait to FSM A, so that it would only send a collision notification to FSM B after FSM B had replied that it had finished processing the first collision. This didn't work because while FSM A was waiting for FSM B it wasn't sitting in the "Trigger Event" Action and missed the other collisions.

My current solution is to completely de-couple FSM B from events sent by FSM A, by adding a new FSM (C) that listens to FSM A's collision events, but just adds the detected collision to an ArrayList. Then I re-structured FSM B to start with a check-wait on the ArrayList contents, only handling a collision when it finds an item in the ArrayList.

This current solution is working, but it doesn't seem very efficient to be polling an ArrayList once every one-hundredth of a second, just to de-couple FSM A and FSM B. Is there a better approach to handling multiple simultaneous Trigger Events in PlayMaker?

Lane

  • Administrator
  • Hero Member
  • *****
  • Posts: 2511
  • Mender of the past
    • Cleverous
Re: Handling multiple simultaneous Trigger Events
« Reply #1 on: July 26, 2012, 08:54:15 AM »
You might use global transitions to initiate the damage and animation separately. Then it can quickly apply the damage and stay hung up in the animation. You may have to add a bool test that executes before the animation that opens and closes when the animation starts and finishes so that the animation isn't restarted when another collision occurs.

This would effectively separate damage and animation while the bool can ask if and animation is running and divert action if it is.

Below is how I have mine set up but I have not tested multiple simultaneous impacts. You might need a way to 'store' the damage or something until the action is freed up. It's not very in depth since I'm not doing any animations or anything but you get the idea.
Products by Cleverous
|| Vault Core : Database
|| Vault Inventory : Multiplayer Inventory
|| Vault Attributes : Character Stats
|| That Hurt! : Dmg Floaties
|| Quinn : 3D

Jernau

  • Playmaker Newbie
  • *
  • Posts: 19
Re: Handling multiple simultaneous Trigger Events
« Reply #2 on: July 26, 2012, 10:30:48 AM »
Hi Lane,

Thanks for your suggestions and examples!

Like you, I'm using global transitions (and my setup looks quite similar to your example). However, my problem occurs in exactly the scenario you haven't tested, i.e. when I get multiple simultaneous collisions and have to "block" the collision events from disturbing my FSM from completing its task (waiting for an animation to complete).

I've removed my ArrayList check-wait solution (as it was causing performance issues), and now I'm trying to think of a new approach. I'm hoping someone here might help me come up with an approach that will allow me to record multiple collision events whilst protecting the FSM that's waiting for the animation to complete.

Lane

  • Administrator
  • Hero Member
  • *****
  • Posts: 2511
  • Mender of the past
    • Cleverous
Re: Handling multiple simultaneous Trigger Events
« Reply #3 on: July 26, 2012, 10:52:06 AM »
Hmm, maybe you can try sending the event but make an Int variable that says if > 0 then applyDamage so you can have any recorded collision just Add Int 1 to dmgBuffer and have the enemy Subtract Int 1 from the dmgBuffer when it runs the applyDamage event. That would make a sort of queue system if you loop it on the >0 condition.

The local damage system just needs to test the dmgBuffer for >0 to run its process.
Products by Cleverous
|| Vault Core : Database
|| Vault Inventory : Multiplayer Inventory
|| Vault Attributes : Character Stats
|| That Hurt! : Dmg Floaties
|| Quinn : 3D

Jernau

  • Playmaker Newbie
  • *
  • Posts: 19
Re: Handling multiple simultaneous Trigger Events
« Reply #4 on: July 27, 2012, 04:07:46 AM »
I think I can re-formulate my problem in a more generic way, which will hopefully make it a little easier to understand:

If you have events that happen in close succession, and where the processing of those events takes time, what is the best approach for queuing up those events, so you don't interrupt the processing?

jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 15500
  • Official Playmaker Support
Re: Handling multiple simultaneous Trigger Events
« Reply #5 on: July 27, 2012, 06:14:41 AM »
hi,

 It depends on the nature of the processes that takes times, are they supposed to be synchrone or asynchrone, meaning.

is it a case where

process A must finish before process B starts

OR

process B has to start after process A is started even tho it's not yet completed?

Bye,

 Jean

Jernau

  • Playmaker Newbie
  • *
  • Posts: 19
Re: Handling multiple simultaneous Trigger Events
« Reply #6 on: July 27, 2012, 06:44:10 AM »
Hi Jean,

If I understand you correctly, I believe that the processing in FSM B would work asynchronously. In other words, if I could spawn a new thread for each event that FSM B receives, then it should continue to work.

FSM A (the Collider with "Trigger Event" Action) sends the first trigger event to FSM B, which waits on an animation completion before continuing its processing. The problem occurs when a second collision occurs simultaneously and FSM A sends another trigger event to FSM B, making it hop out of the animation wait Action and up to the first action triggered by the global event, thus not completing its processing.

I guess that I need some way of de-synchronizing FSM A from FSM B, so that FSM A is still recording all of the collisions, but only sends events to FSM B when FSM B has finished processing.

jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 15500
  • Official Playmaker Support
Re: Handling multiple simultaneous Trigger Events
« Reply #7 on: July 30, 2012, 03:55:39 AM »
Hi,

 you simpl yneed to maintain a boolean flag describing the "busy" state of process B and ONLY fire an event to the FSM responsible of process B if it's not busy.

 So FSM A will first test if FSM B is busy and then act appropriatly. You can even stack calls to FSM B. for example maintain an Fsm Int variable that counts the number of called during which FSM B is busy and when FSM B ends ( and eventually fire back to FSM A that's it's ended), FSM A can check that there are pending processing and fires again.

 I had a working example of this, but I really can't remember where, I'll try to remember, it's there on this forum, just can't find it!

bye,

 Jean