playMaker

Author Topic: [SOLVED] How to auto-fill Action's variables when using "Paste Template"?  (Read 598 times)

Laappa

  • Playmaker Newbie
  • *
  • Posts: 9
    • View Profile
Hi,

I'm trying to create a smooth workflow for creating content to my game. The target is to have as automated process to add FSM 'nodes' as building blocks.

I want to:
1) Right-click on the FSM window, click "Paste Template" and paste my earlier saved template (e.g. a State, that has it's name, color and Action set ready so that the content creator doesn't need to fill those him/herself every time)

2) Then, I would want that the Action parameters would be filled automatically. And especially, auto-generating a GameObject: most of my States require an Unity GameObject reference. I want this object to be created automatically, when I "Paste Template". The script would Instantiate a copy of a Prefab that is in my Project Assets, and put it under in the Scene hierarchy

The problem is:
I have no issues, if I add my own Action - that has above functionality - manually to a State. It runs the "Reset()" code, which generates the GameObject and adds it to the Action's reference. All good. BUT then comes the tiny but annoying issue: if I "Paste Template", that has this Action, it does NOT run "OnReset". So it won't generate the GameObject. So this results with the situation that AFTER I "Paste Template", I need to manually add the Action so that it runs "Reset()". If the Action is already in the Template, it won't run "Reset()" on the Action.

My Question:
How can I automate this flow: "Paste Template", which contains my custom "Action", which calls "Reset()" method, that generates the required GameObject references, FULLY automatically?

I've been playing around this for hours now, and I'd like to seek for help...I could not find any similar posts on the Forum although I am surprised if anyone hasn't wanted to automate their workflow this far? Am I on the right track at all?

Thanks!
Lauri
« Last Edit: April 15, 2021, 10:55:22 AM by Laappa »

Laappa

  • Playmaker Newbie
  • *
  • Posts: 9
    • View Profile
Re: How to auto-fill Action's variables when using "Paste Template"?
« Reply #1 on: March 31, 2021, 06:32:02 AM »
Any help with this would be highly appreciated. My system is otherwise working smoothly, but this manual addition of GameObjects is way too slow...

I've tried also to use ErrorCheck() so that when the GameObject is required, and unassigned (i.e. empty) it will go to ErrorCheck in the Action's script and create the GameObject automatically but the issue is that the same ErrorCheck() is called on EACH similar Action, which results to a situation where EACH Action will EACH time create the GameObject again when I add multiple Templates to the same FSM. For some reason I can't make Unity or Playmaker to understand that the GameObject is already created for the previous ones (even by trying setting the script Dirty), it seems to read it each time "Null". And again highlighting that all this should be able to happen in Editor, not in Playmode.

Alex Chouls

  • Administrator
  • Hero Member
  • *****
  • Posts: 3912
  • Official Playmaker Support
    • View Profile
    • LinkedIn
Re: How to auto-fill Action's variables when using "Paste Template"?
« Reply #2 on: March 31, 2021, 10:39:10 AM »
Interesting problem. I don't think any of the existing callbacks will help in this situation. Just in case you didn't know, you can run Reset from an Action's Settings Menu (maybe quicker than adding it from scratch).

Your best bet might be a little editor script to call Reset on all actions (or specific actions) in the selected states after pasting. You would have to manually call it, but you could hook it up to a button or shortcut key. Are you comfortable making editor scripts? Let me know if you need some pointers in the right direction, or I can take a crack at it...

Laappa

  • Playmaker Newbie
  • *
  • Posts: 9
    • View Profile
Re: How to auto-fill Action's variables when using "Paste Template"?
« Reply #3 on: March 31, 2021, 11:28:38 AM »
Hi Alex,

thanks for you reply! Good to know all that, although I'm still wondering if I'm using Playmaker somehow wrong because I consider this missing feature so "obvious" for smooth workflow... ;D

But, to your question: I would LOVE and take all possible help you are able to give, because I'm not super comfortable with this topic, but at minimum some pointers would be needed, just to know where I need to dig & hook that additional script. I think the ideal would be to call Reset on the added Template's action(s) only, to avoid any unnecessary calls, so that it would work similarly when adding an action manually to a state. But as long as the required GameObject that I need, would be created just once (and not overrun by a new Reset-call), I'm fine with all options you can provide.

Cheers,

Alex Chouls

  • Administrator
  • Hero Member
  • *****
  • Posts: 3912
  • Official Playmaker Support
    • View Profile
    • LinkedIn
Re: How to auto-fill Action's variables when using "Paste Template"?
« Reply #4 on: March 31, 2021, 01:27:18 PM »
It's hard to say if you're using Playmaker wrong without knowing more about what you're doing in Reset. Normally when you copy/paste actions, states, FSMs etc. you want to keep their settings - that's the whole point! Personally I haven't come across a use case for resetting on paste.

Normally, if you have a lot of actions that reference a GameObject you would use a variable. That variable will get copy/pasted with everything else, and then you can set its value once in the Variables tab. You can also expose it as an Input and set it in the PlayMakerFSM Inspector. This is the normal way to setup GameObject references.

Or you can initialize the variable at runtime. For example, a MonoBehaviour will often initialize variables in Awake or Start. You can do the same thing in Playmaker in the Start State, initializing variables before they're used.

It did just occur to me that there's an OnPreprocess action callback that you could use to do some custom setup. It's called when building or running in the editor. You could try moving the GameObject reference setup you're doing in Reset to OnPreprocess:

Code: [Select]
    public override void OnPreprocess()
    {
        // initialize variables...
    }

Anyway, those are a few options to try.

Let me know if any of those work for your use case.

Laappa

  • Playmaker Newbie
  • *
  • Posts: 9
    • View Profile
Re: How to auto-fill Action's variables when using "Paste Template"?
« Reply #5 on: April 01, 2021, 08:55:57 AM »
Hi & thanks again Alex,

I think I tried that OnPreprocess callback at some point, and tried it again: I placed that in the Action script with a Debug.Log and...unfortunately it won't call this method for some reason. I cleared all errors even, but no help, it won't call that method when pasting the Template or even when adding the action manually.

Going back to how I'm using Playmaker; I know that the "off the shelf" way is to use variables (like a GameObject variable) and attach the wanted GameObject to that Variable, even in the Inspector view. BUT that is one manual step too much in my opinion - the need to locate & drag-drop the wanted GameObjects to this Input or in the Variables tab. I'd like to spawn immediately a GameObject when adding the State. When building a "blueprint" of say a Level progression, there are dozens, hundreds of these States to be created (e.g. my enemy spawners, music starters, dialogue starters etc.), so always creating and assigning the GameObject manually to the PlayMaker State is a time-consuming activity.

Initializing things in runtime won't be a help either. When I design those Playmaker scripts, I'm doing it in Editor mode and I need to visually place those created GameObjects in the world space to their right places, before hitting play and testing it out.

If there was just a place to add code, that when pasting a Template, it would call  Reset() on its attached Action(s)...that would be essentially what I'm looking for.

Cheers
Lauri

Alex Chouls

  • Administrator
  • Hero Member
  • *****
  • Posts: 3912
  • Official Playmaker Support
    • View Profile
    • LinkedIn
Re: How to auto-fill Action's variables when using "Paste Template"?
« Reply #6 on: April 01, 2021, 11:29:15 AM »
OnPreprocess is called when you press Play or make a build. Not sure if that helps in your case or not...

Do you really want to Reset all parameters in all actions in the pasted states? That seems like it would do a lot of damage! Or do you want to call a specific setup method in some actions? I'll take a look at writing a little editor script for this, but want to be clear about how it should work.



Laappa

  • Playmaker Newbie
  • *
  • Posts: 9
    • View Profile
Re: How to auto-fill Action's variables when using "Paste Template"?
« Reply #7 on: April 06, 2021, 01:33:38 PM »
Hi,

sorry I was out of office for the Easter and just saw your post. Thanks for baring with me! Unfortunately I can't use OnPreprocess, because I want to populate Action in Editor, i.e. in the phase I'm crafting my game's logic.

What I would specifically need, step by step - in the Editor mode is:

1) Paste Template that I have created (attachment 1)

2) The Template contains one State, which again contains one Action. When I paste this template as described in step #1, the Action inside the State will create the "My Game Object Reference" (attachment 2) which is a standard GameObject - in practice a copy of a Prefab - and place it in the Hierarchy as a Child of the gameobject that has the FSM. In practice I could of course do other setting here as well, but creating the gameobject is the key thing I need.

3) Then I will just fine-tune the settings of the newly created GameObject itself, if it's Prefab settings are not good off-the-shelf.

4) Then I carry on creating more States like this, independent of the previous States, NOT destroying or clearing out anything from the earlier States or their Actions.

Cheers,
Lauri

Alex Chouls

  • Administrator
  • Hero Member
  • *****
  • Posts: 3912
  • Official Playmaker Support
    • View Profile
    • LinkedIn
Re: How to auto-fill Action's variables when using "Paste Template"?
« Reply #8 on: April 06, 2021, 11:04:22 PM »
You could try using the ErrorCheck method for this.

Normally ErrorCheck is used to check the setup of the action and returns a string with any errors. But you could do more work in there and return an empty string (assuming the setup succeeded).

Laappa

  • Playmaker Newbie
  • *
  • Posts: 9
    • View Profile
Re: How to auto-fill Action's variables when using "Paste Template"?
« Reply #9 on: April 07, 2021, 03:40:02 AM »
Yeah I think we are getting near, and the issue might be on Unity side... Initially, I actually started by using ErrorCheck to do this whole thing. I was able to create the GameObject automatically if the reference was empty. All looked good at first glance.

BUT.

The problem was that if I had already added a Template having this Action, and I added another one, the new Action called ErrorCheck on all the previous Actions as well, even though they had their GameObjects already created and assigned. So even though it _looked_ like the previous Actions HAD the GameObject (which were shown also in the Hierarchy), FSM still ran the ErrorCheck again on those States too, and created ANOTHER GameObject for them. So every time I added the template, my Hierarchy was flooded with new GameObjects, as they were created every time to each State containing the same Action. Naturally I put a check in the ErrorCheck to check if the GameObject was there already, like

if(referenceGameObject == null) InstanceNewReferenceGameObject
else don't do anything

But this still didn't catch it. The ErrorCheck still created the GameObject again. I tried SetDirty so that Unity would recognize the GameObject created in Edit mode without luck. Maybe I'm just missing the right code to check if the GameObject is there already...? But as said, checking referenceGameObject == null, or checking !referenceGameObject each return false in the Editor mode, and new GameObject was created.

Cheers,

Alex Chouls

  • Administrator
  • Hero Member
  • *****
  • Posts: 3912
  • Official Playmaker Support
    • View Profile
    • LinkedIn
Re: How to auto-fill Action's variables when using "Paste Template"?
« Reply #10 on: April 07, 2021, 10:32:55 AM »
if referenceGameObject is a variable you should check referenceGameObject.Value == null.

Laappa

  • Playmaker Newbie
  • *
  • Posts: 9
    • View Profile
Re: How to auto-fill Action's variables when using "Paste Template"?
« Reply #11 on: April 07, 2021, 12:47:55 PM »
No, it's a basic Unity GameObject.

Alex Chouls

  • Administrator
  • Hero Member
  • *****
  • Posts: 3912
  • Official Playmaker Support
    • View Profile
    • LinkedIn
Re: How to auto-fill Action's variables when using "Paste Template"?
« Reply #12 on: April 07, 2021, 02:53:48 PM »
Can you post the code for the action? You should be able to check an action parameter for null.

Laappa

  • Playmaker Newbie
  • *
  • Posts: 9
    • View Profile
Re: How to auto-fill Action's variables when using "Paste Template"?
« Reply #13 on: April 08, 2021, 03:09:16 AM »
Sure, here's the most simplified version of the code. So the GameObject is created, but like I wrote before, it is created every time again when I paste a new Template with this same Action. I feel like it shouldn't be a major thing to get working but I'm just stuck...

public GameObject myGameObjectReference;

public override string ErrorCheck()
{
  if (myGameObjectReference == null)
   {
      CreateNewGameObjectReference();
      return "GameObject reference missing, auto-creating it.";
    } else return null;
}

private void CreateNewGameObjectReference()
{
  myGameObjectReference = new GameObject();
  UnityEditor.EditorUtility.SetDirty(this.myGameObjectReference);
}

Laappa

  • Playmaker Newbie
  • *
  • Posts: 9
    • View Profile
Re: How to auto-fill Action's variables when using "Paste Template"?
« Reply #14 on: April 15, 2021, 10:49:34 AM »
Just to let the community know - this issue was finally solved with help of my colleague. Solution was beautifully simple, all I was missing was this:

Fsm.SaveActions();

So after creating the gameObject variable, I was saving the gameobject itself (with SetDirty), but that didn't save the GameObject owning the FSM itself thus not saving the reference.

So here's the working code if someone aims to do similar automation:

========================================

public GameObject myGameObjectReference;

public override string ErrorCheck()
{
  if (myGameObjectReference == null)
   {
      CreateNewGameObjectReference();
      return "GameObject reference missing, auto-creating it.";
    } else return null;
}

private void CreateNewGameObjectReference()
{
  myGameObjectReference = new GameObject();
  Fsm.SaveActions();
}


========================================

Cheers,
Lauri