playMaker

Author Topic: Is there a way to make "functions" in PlayMaker? [Solved]  (Read 7654 times)

Tricky_Widget

  • Junior Playmaker
  • **
  • Posts: 62
Is there a way to make "functions" in PlayMaker? [Solved]
« on: February 22, 2015, 09:29:17 AM »
I'm coming from a traditional scripting/programming background and one of the core concepts I'm used to working with is functions.  That is to say making a bit of code usable in more than one place.  That way when you have a common activity you can edit the code in just one place rather than having individual copies of it in each place you use it.

For example, I'm making a 2D game and something I do often is randomly set a sprite's rotations.  This requires a series of five PlayMaker actions.  Right now I'm copying in the same five actions in every place I want to do this orienting.  Which works fine, but any time I want to make a change I have to go hunt down every copy of the actions.

If I were scripting, I would simply drop the commands into a function and call the function wherever I needed it.  Is there any way to do something similar with PlayMaker?  (I realize I could make actual scripting functions and work with them through PlayMaker, but I strongly want to avoid writing code.)

Thank you very much!
« Last Edit: February 23, 2015, 02:20:33 PM by Tricky_Widget »

jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 15500
  • Official Playmaker Support
Re: Is there a way to make "functions" in PlayMaker?
« Reply #1 on: February 22, 2015, 09:45:57 AM »
Hi,


 For this, you have several ways, none ideal I should add, I am facing also this kind of issues, but after some time with PlayMaker I got used to not end up with this kind of case,

First you'll need to save/create that logic as a template. Then you can do two things:

-- Use Sub-Fsm to run this template inside any states
-- Use that Template as the source for a given Fsm ( in the unity's Fsm Inspector)
 Unity

SubFsm is what's going to give me maximum reusability but you may find the interaction between the host and the sub fsm limited for complex cases. I had some though about creating a series of "Set Host variable" actions, so if you think you need them let me know, this is something I have pending and burried in my things to do.

Bye,

 Jean

invayne

  • Full Member
  • ***
  • Posts: 105
Re: Is there a way to make "functions" in PlayMaker?
« Reply #2 on: February 22, 2015, 09:46:31 AM »
you could just make a out side fsm can call what you from there basically same concept instead of copying just get what you need by using the get fsm var actions dont know if that will help you in this situation but same concept

Tricky_Widget

  • Junior Playmaker
  • **
  • Posts: 62
Re: Is there a way to make "functions" in PlayMaker?
« Reply #3 on: February 22, 2015, 10:02:15 AM »
Use Sub-Fsm to run this template inside any states

Thank you very much for the tip!

I'm new to PlayMaker and I confess this is the first I've heard of Sub-FSM.  I've searched around, but I can't immediately find any documentation or tutorials on this.  Could you direct me to more information on what Sub-FSM is and how it works?

Thanks again!

wetcircuit

  • Full Member
  • ***
  • Posts: 158
    • wetcircuit.com
Re: Is there a way to make "functions" in PlayMaker?
« Reply #4 on: February 22, 2015, 11:13:11 AM »
I can't find sub-FSM... it doesn't appear to be an actual action script, maybe a design concept (or figure of speech)?

I found Run FSM and this tutorial.


https://www.youtube.com/watch?v=ET1V1Loftzw
« Last Edit: February 22, 2015, 11:15:12 AM by wetcircuit »

wheretheidivides

  • Sr. Member
  • ****
  • Posts: 496
Re: Is there a way to make "functions" in PlayMaker?
« Reply #5 on: February 22, 2015, 11:27:28 AM »
I THINK what he was referring to as a sub-FSM was a FSM that runs another FSM.  So you have a FSM setting up your scene and in the middle you run another FSM to countdown from 5-4-3-2-1.  Then you can keep running the scene FSM and somewhere run the spawn FSM, then keep running the main scene FSM and then run another FSM.  That way you can run the countdown FSM from other FSMs but with different variables.  The main FSM runs but triggers other FSMs that do smaller jobs.

As far as the function-subroutine goes, it's the same thing.  You create a FSM with variables.  So you create a game object called 'add score', then on that game object create a FSM.  This FSM has a variable called 'points'.  So whenever you score a hit, you 'send event' to that FSM with the number of 'points' to add.  So this could be called a sub-FSM.  This way you can decide the number of points scored and put it into a variable.
(Now, that was a bad example.  Actually scoring points would be better suited having a game manager.  A listener would be better suited for scoring....but that is another topic).


jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 15500
  • Official Playmaker Support
Re: Is there a way to make "functions" in PlayMaker?
« Reply #6 on: February 23, 2015, 01:26:07 AM »
Hi,

 yes, sorry for the confusion, the related action is "RunFsm",

in the template, you'll be using "FinishFsm" action to tell the host ( the fsm that you use RunFsm) that you have finished your task, and the RunFsm will in turn fire the finish event.

Bye,

 Jean


Lane

  • Administrator
  • Hero Member
  • *****
  • Posts: 2511
  • Mender of the past
    • Cleverous
Re: Is there a way to make "functions" in PlayMaker?
« Reply #7 on: February 23, 2015, 09:22:42 AM »
SubFSM's and Templates are kind of the 'dark arts' right now and can be a bit tricky to work with at first until you get the hang of it. We really want to shed more light on using this stuff soon and have talked about some ways to do so in the beta group.

Basically you can make an FSM and save it as a Template, then access it through the Run FSM action. If you want variables exposed in the Run FSM action then go to your Template and toggle "Inspector" on all of the variables you want to be listed in the Run FSM action when that Template is targeted to be run.. Its just like passing arguments this way.

The video linked above uses this method for a generic DoDamage function.

If you need to return variables, the best way to do that right now is by using the Get Host Info action (not vanilla, search forum/ecosystem for it) and passing variables up like you would communicate between FSMs normally... Or alternatively just pass the owner down as an argument and return to it generically in the function.
« Last Edit: February 23, 2015, 09:25:37 AM by Lane »
Products by Cleverous
|| Vault Core : Database
|| Vault Inventory : Multiplayer Inventory
|| Vault Attributes : Character Stats
|| That Hurt! : Dmg Floaties
|| Quinn : 3D

Tricky_Widget

  • Junior Playmaker
  • **
  • Posts: 62
Re: Is there a way to make "functions" in PlayMaker?
« Reply #8 on: February 23, 2015, 02:20:06 PM »
Ah, thank you all for the clarification!  That does sounds like a very useful technique.  I will give it a try and see how it goes.  :)

thatnzguy

  • Playmaker Newbie
  • *
  • Posts: 43
Re: Is there a way to make "functions" in PlayMaker? [Solved]
« Reply #9 on: June 04, 2015, 07:54:56 PM »
This all sounds pretty cool, the returning value thing is a little tricky at the moment.
I'm not sure how to provide a variable to the Run FSM for receiving the returned value. Any ideas? Can the sub FSM create variables in the host, or only modify?

jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 15500
  • Official Playmaker Support
Re: Is there a way to make "functions" in PlayMaker? [Solved]
« Reply #10 on: June 05, 2015, 01:09:16 AM »
Hi,

 you can use "set Fsm xxx", have you tried that. but you can't create variables on the fly, not even in regular Fsm.

 Bye,

 Jean

Tricky_Widget

  • Junior Playmaker
  • **
  • Posts: 62
Re: Is there a way to make "functions" in PlayMaker? [Solved]
« Reply #11 on: June 06, 2015, 10:40:26 AM »
you can use "set Fsm xxx"

This is what I do.  If you create variable to hold the return value in the calling FSM, and then set "Use Owner" with "Set FSM X" in the sub FSM, it works like a charm.

thatnzguy

  • Playmaker Newbie
  • *
  • Posts: 43
Re: Is there a way to make "functions" in PlayMaker? [Solved]
« Reply #12 on: June 07, 2015, 04:57:27 PM »
Ah you can't create variables on the fly, that explains it.
I was trying to make a Run FSM that other developers could use without knowing they would also have to create a variable with the correct name to handle the return.

Gua

  • Beta Group
  • Sr. Member
  • *
  • Posts: 309
    • Andrii Vintsevych
Re: Is there a way to make "functions" in PlayMaker? [Solved]
« Reply #13 on: July 18, 2017, 11:26:10 AM »
Such a powerful feature. It's a shame that after so many years of using Playmaker, I only now discovered it.