playMaker

Author Topic: Global Event/Invoke Method Best Practices  (Read 3181 times)

Aislin

  • Playmaker Newbie
  • *
  • Posts: 6
Global Event/Invoke Method Best Practices
« on: December 12, 2017, 08:27:32 AM »
Hello!

I bought Playmaker a few weeks ago and have really been enjoying it!

I had a quick question regarding performance of the InvokeMethod action. To prevent FSM's to be globally accessible, I am handling communication between them via scripts. So if an FSM on object A wants to trigger an event to an FSM on object B, Object A's FSM is invoking a method on itself, and that method is then calling the event on Object B.

My question: Is using Invoke Method frequently a good practice? Best practice is to cache those method calls into delegates. I wasn't sure if PlaymakerFSM caches those upon initialization or if it is using that reflection every time the action occurs.

If Invoke Method doesn't cache the method name into an action, is there any way to do so or is there a better way to accomplish my goal here? I know I could just make the events global, but then I would end up with too many global events and every FSM would have access to every other FSM.

Also, when it comes to using Global Events, I've been told it's best practice to use the fewest amount of static or global objects in a game in favor of Dependency Injection. If we create a Global Event every time one FSM wants to talk to another, it seems like we would have hundreds or even thousands of Global Events, which may get messy as the game builds.

Here's my current task I'm trying to accomplish:

I am adding a Pause Menu to my game. The 4 objects involved are: Input Manager, Game Manager, Pause Menu, and Battle Manager.

Currently, the Input Manager calls events (in script) whenever an Input is received.

The Game Manager subscribed to the event "MenuButtonDown". When that event triggers, my Game Manager sends an event to its PlaymakerFSM by string name. The FSM then invokes the method on the GameManager "ActivateCombatPauseMenu". This method does 2 things:

  • Calls the "Activate()" method on the Pause Menu
    Calls the "Suspend Combat()" method on the Battle Manager

The Pause Menu's Activate() method then sends an event by string to its own FSM called "DISPLAY". Then it fires off events in code based on what selections the player makes in the menu (such as resuming, quitting, etc).

If I used Playmaker FSM's for everything and use global events instead of calling methods, I would have the following global events:

MenuButtonPressed (sent from InputManager)
ActivatePauseMenu (sent from Game Manager)
PauseMenuResumeGame (sent from Pause Menu)
PauseMenuRestartBattle (sent from Pause Menu)
PauseMenuQuitGame (sent from Pause Menu)
DeactivePauseMenu (sent from GameManager)
SuspendCombat (sent from GameManager)
ResumeCombat (sent from GameManager)

That just seems like a lot of global events just from a pause menu. I'd be worried my event list would get pretty overwhelming over time as the game builds.

Below are the screenshots of my two FSMs. Thank you SO much for your help :)

Thanks!

Nick







Thanks so much!

djaydino

  • Administrator
  • Hero Member
  • *****
  • Posts: 7615
    • jinxtergames
Re: Global Event/Invoke Method Best Practices
« Reply #1 on: December 12, 2017, 05:03:55 PM »
Hi.
I never used InvokeMethod action so i can't really say much about it.

But i usually use "Send Event By Name" and "Get/Set Fsm..." actions.

I just recently made a video on how to connect prefab fsms to scene fsms,
maybe it can be useful to you :


Fat Pug Studio

  • Beta Group
  • Hero Member
  • *
  • Posts: 1294
    • Fat Pug Studio
Re: Global Event/Invoke Method Best Practices
« Reply #2 on: December 13, 2017, 01:54:55 AM »
Invoking method is quite quick actually, so don't worry about performance ;)
Available for Playmaker work

Aislin

  • Playmaker Newbie
  • *
  • Posts: 6
Re: Global Event/Invoke Method Best Practices
« Reply #3 on: December 17, 2017, 09:17:14 AM »
Thanks for the responses! That video was very helpful on how to reduce global events/variables, and it's nice to know that the Invoke Method isn't too expensive.

Thanks so much!

Nick

jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 15500
  • Official Playmaker Support
Re: Global Event/Invoke Method Best Practices
« Reply #4 on: December 18, 2017, 02:05:15 AM »
Hi,

 If you don't know how to code, then use make use of the Invoke action, but be aware that it uses reflection and when you publish on devices the method you call may get stripped out of the build, for this you need to use the Linker wizard which checks your reflection usage and will tell Unity not to miss these methods.

http://hutonggames.com/playmakerforum/index.php?topic=11126.0

in general, if you can code, I strongly advice creating dedicated custom action calling the method you want by code, instead of via reflection.

Bye,

 Jean

Fat Pug Studio

  • Beta Group
  • Hero Member
  • *
  • Posts: 1294
    • Fat Pug Studio
Re: Global Event/Invoke Method Best Practices
« Reply #5 on: December 18, 2017, 02:15:45 AM »
Wait, i don't understand, how it may get stripped. Either it works in the build or not. Can i get some more info since i'm using invoking a lot.
Available for Playmaker work

jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 15500
  • Official Playmaker Support
Re: Global Event/Invoke Method Best Practices
« Reply #6 on: December 18, 2017, 02:29:46 AM »
Hi,

 when you use Reflection, you don't bind by code a direct access to that method, therefore when Unity scans your project and code, it doesn't know you need that method and if no other code is using that method then Unity strip it as it thinks it's not needed ( thus saving build size).

so, indeed reflection makes it harder and it's not a black and white situation where ti works or not, it may works, it may not work, as it depends on wether your methods is used elsewhere or not.

more info here: https://docs.unity3d.com/Manual/iphone-playerSizeOptimization.html

 Bye,

 Jean

Fat Pug Studio

  • Beta Group
  • Hero Member
  • *
  • Posts: 1294
    • Fat Pug Studio
Re: Global Event/Invoke Method Best Practices
« Reply #7 on: December 19, 2017, 09:20:56 AM »
I'm always hungry for knowledge, thanks a lot!
Available for Playmaker work