playMaker

Author Topic: Awake in custom actions are called after game stops in Unity Editor  (Read 1945 times)

plinm

  • Playmaker Newbie
  • *
  • Posts: 25
    • View Profile
Hi, there

I found the Awake in custom actions are called right after game stops in Unity Editor, which means it is called twice in a single run. I wonder if it is a BUG or intended design?

Since the Awake is called after the game stops, which causes any changes I made to scene in Awake function will be kept forever. So I have to add following code to make it work:

Code: [Select]
public override void Awake()
{
    #if UNITY_EDITOR

    if (!Application.isPlaying)
    {
        return;
    }

    #endif

    // the actual Awake code here
}

I am complete beginner to PlayMaker, so I am NOT sure if I was doing something wrong. Any help is appreciated.

Deek

  • Full Member
  • ***
  • Posts: 130
    • View Profile
Re: Awake in custom actions are called after game stops in Unity Editor
« Reply #1 on: August 01, 2017, 09:18:06 AM »
I've tried to replicate your situation with a Debug.Log in Awake and it only throws it once, not a second time when exiting Play-Mode (Unity 5.6, PlayMaker 1.8.4). So I can only assume that there might be something wrong with your PlayMaker-Installation (maybe reinstall through the Welcome Screen to see if it fixes it) or you derived from a class that has an Awake()-Implementation which also gets called when leaving Play-Mode (very unlikely, I tested it with the default "FsmStateAction").

If none of that's the case, you already seem to have found a good work-around, so why not stick to it. You can also shorten it to 3 lines so that it's less of a visual/programmatical burden:
Code: [Select]
#if UNITY_EDITOR
if (!Application.isPlaying) return; //suppress second execution when leaving Play-Mode
#endif

jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 15082
  • Official Playmaker Support
    • View Profile
Re: Awake in custom actions are called after game stops in Unity Editor
« Reply #2 on: August 01, 2017, 10:11:47 AM »
Hi,

 Indeed the Awake() function should not do anything apart for caching components or classes that will be needed when Action will be executed either in OnEnter, OnUpdate, etc etc

 but yeah, it seems to be a bug. I'll replicate locally and submit a bug for this to be addressed.

 Thanks for spotting this!

 Bye,

 Jean

plinm

  • Playmaker Newbie
  • *
  • Posts: 25
    • View Profile
Re: Awake in custom actions are called after game stops in Unity Editor
« Reply #3 on: August 02, 2017, 09:40:07 AM »
Thanks for the info.

I can replicate this issue in an empty project with a fresh installation of PlayMaker (Unity 5.6.1f1 with PlayMaker 1.8.4f5).

Here is the action I use:

Code: [Select]
public class CustomAction : FsmStateAction
{
    public override void Awake()
    {
        Debug.Log("AWAKE IS CALLED");
    }

    public override void OnEnter()
    {
        Finish();
    }
}

The message shows in the console when you click the PLAY button, and once more when you click the PLAY button (to stop) again.
« Last Edit: August 02, 2017, 12:16:14 PM by plinm »

Deek

  • Full Member
  • ***
  • Posts: 130
    • View Profile
Re: Awake in custom actions are called after game stops in Unity Editor
« Reply #4 on: August 03, 2017, 12:22:18 PM »
I think I figured it out. You probably have an attribute like [ExecuteInEditMode] before your class. That gives certain functions like Awake() and Update() different meaning.
Awake gets called both when Play-Mode initializes and when the Editor initializes, thus you get 2 calls to Awake (and that's exactly like your described behaviour).
Update on the other hand doesn't get called every frame but once something in the scene gets changed.
The more you know.

plinm

  • Playmaker Newbie
  • *
  • Posts: 25
    • View Profile
Re: Awake in custom actions are called after game stops in Unity Editor
« Reply #5 on: August 03, 2017, 11:57:12 PM »
I think I figured it out. You probably have an attribute like [ExecuteInEditMode] before your class. That gives certain functions like Awake() and Update() different meaning.
Awake gets called both when Play-Mode initializes and when the Editor initializes, thus you get 2 calls to Awake (and that's exactly like your described behaviour).
Update on the other hand doesn't get called every frame but once something in the scene gets changed.
The more you know.

Unfortunately, that's not the case I have, I don't have any attribute in the class. But still, thanks for your help.

BTW, the PlayMaker Action class (FsmStateAction) is not a MonoBehaviour (or child of it), it just use a similar interface that MonoBehaviour has (to reduce learning curve I guess), which means ExecuteInEditMode should not have any effect on it, unless PlayMaker itself is using this attribute internally.
« Last Edit: August 04, 2017, 12:32:25 AM by plinm »

jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 15082
  • Official Playmaker Support
    • View Profile
Re: Awake in custom actions are called after game stops in Unity Editor
« Reply #6 on: August 04, 2017, 12:07:39 AM »
Hi,

 yes, a PlayMaker Action is indeed NOT a MonoBehaviour and do not derived from it at all.

 PlayMaker doesn't execute fsm and actions during edit time.

 Bye,

 Jean

plinm

  • Playmaker Newbie
  • *
  • Posts: 25
    • View Profile
Re: Awake in custom actions are called after game stops in Unity Editor
« Reply #7 on: August 04, 2017, 12:30:47 AM »
I think I might find the cause.

If you have the GameObject (with PlayMakerFSM) selected, the Awake functions of all actions in this state machine will be called when you exit Play Mode.

In fact, in Edit Mode, any Awake function in the scene will be called once (and only once) when you select the GameObject contains PlayMakerFSM it belongs to. (switching scene or entering Play Mode will reset this behaviour and the Awake function can be called again when re-entering Edit Mode)

jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 15082
  • Official Playmaker Support
    • View Profile
Re: Awake in custom actions are called after game stops in Unity Editor
« Reply #8 on: August 04, 2017, 12:36:04 AM »
Hi,

 yes, The issue has been raised internally, we are aware of this, but it won't be fixed for the next update ( to be released asap).

 Basically, you should never set values inside Awake, and only do this during the regular OnEnter, OnUpdate, OnExit, etc, awake should actually never be used or only for caching components to improve performances.

Bye,

 Jean