playMaker

Author Topic: Best practice??  (Read 2904 times)

escpodgames

  • Hero Member
  • *****
  • Posts: 687
    • Assets
Best practice??
« on: December 05, 2013, 05:11:38 PM »
Hi,

Have wondered about this for a while -
Is creating a gameobject variable and assigning the gameobject to it ... better than just dragging in the gameobject into a slot on an action (send event to gameobject) OR is it the same?

1982

  • Junior Playmaker
  • **
  • Posts: 71
Re: Best practice??
« Reply #1 on: December 05, 2013, 05:15:45 PM »
I would go with the variable, because it most likely wont get broken. Nowadays I try to save everything into variables, because in past I've noticed that sometimes fields with dragged objects go empty.

escpodgames

  • Hero Member
  • *****
  • Posts: 687
    • Assets
Re: Best practice??
« Reply #2 on: December 05, 2013, 05:39:40 PM »
My current game is all variables but it has been a pain dealing with global variables as they can only be set at runtime and any FSM data on that gameobject is unknown when trying to send an event to a gameobject that is a global variable.

I've never had issues with dragging the gameobject into the action slot and see it as the same method as setting a variable. It would be so much simpler to just drag in a gameobject and thus I'm interested in the behind the hood impact on performance.
« Last Edit: December 05, 2013, 05:41:32 PM by LampRabbit »

Andy22

  • Junior Playmaker
  • **
  • Posts: 99
Re: Best practice??
« Reply #3 on: December 06, 2013, 05:28:33 AM »
They are not really equivalents, i mean if u need a "variable" as the name suggest it can be dynamic or at least user settable via the inspector. If its static and never changes, then a direct reference is a valid way to setup things.

The true question u should ask yourself is: Will this FSM be part of a prefab?
If the answer is yes, u now must ensure that all FSM gameobject variables are either set to "owner" or reference a gameobject that is part of the same prefab.

This can get very hard to ensure/setup and thats why we rarely use direct set-able gameobject references. This means if a FSM needs a gameobject it is initialized in the start state and gets its references via name/tag/layer/component actions.

We also do this because we noticed that prefabs with a FSM change quite a lot over time and rechecking and rewiring all the references properly did result in errors. So now we mainly expose a gameobject name field and reference by name, while our Init state then gets all references in variables and ensures they are not null.

A other tip would be to use templates if possible, since they allow to quickly create variants, that still can be maintained at a central point.


If u wonder, why all this trouble. Thats because Unity can't save object references outside of the same prefab.

bye Andy
« Last Edit: December 06, 2013, 05:48:52 AM by Andy22 »

jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 15500
  • Official Playmaker Support
Re: Best practice??
« Reply #4 on: December 09, 2013, 02:29:13 PM »
Hi,

 no differences in terms of performances, but indeed, as it's been said, it's easier and more flexible to use a fsmvariable instead of referencing direclty in the action. Then you can expose that fsmGameObject in the inspector for example, or reuse that gameobject in different states and action, and be sure that you only need to retarget the fsmGameObject itself, instead of refactoring all your actions, and obviously miss one state and boom: very hard bug to track down then...


bye,

 Jean

escpodgames

  • Hero Member
  • *****
  • Posts: 687
    • Assets
Re: Best practice??
« Reply #5 on: December 09, 2013, 06:16:41 PM »
Thx Jean! Exactly the answer I was after. There are some instances when the use of a variable is a lot of work and will never be changed so using a gameobject would be best.