playMaker

Author Topic: ConvertIntPairs - Finally a case-switch action for Playmaker!  (Read 8764 times)

MaDDoX

  • 1.2 Beta
  • Full Member
  • *
  • Posts: 165
    • FluidPlay Studios
So, before passing on the code+instructions, allow me to explain where did the need for this action come from. Suppose the following (very common) scenario, in pseudocode:
Code: [Select]
  If difficulty = 1
   --- gamespeed = 2
   If difficulty = 2
   --- gamespeed = 4
   (...)
In C#, that would be written as such:
Code: [Select]
SWITCH (difficulty){
CASE 1:
gamespeed = 2
BREAK;
CASE 2:
gamespeed = 4
BREAK;
}

To do that in "vanilla" playmaker it's pretty hard. You have to create one boolean variable for the tests (depending on the situation, multiple boolean variables), and since all logic actions only fire up new events, for each possible result you need a new state. Each time you change to a new state you stop the current action-chain execution, so if all you want to do is a quick test of a variable and assign one result for each possible value in a *second* variable, you're in for a lot of work.

I must say that if the result involves more than a simple attribution of value to another variable, it's probably more interesting to split the logic into other states, you can even have a common "descendant" state to all case-states. Like so:



That said, I do intend to release some other non-state-switch logic functions in the near future, just because I'm going to use them anyways. I'm sure Alex is not a fan of such actions because it defeats the idea behind FSMs somewhat, but like he said once.. better have them than not have them ;) Use it with discretion, be concious of the fact that long lists of actions means you should split it in multiple states, and you'll be fine.

So, how to use "ConvertIntPairs"? Basically "Input Variable" is the "SWITCH" variable, the one with the integer values you want to test against. The "Output Variable" is the integer variable which will hold the "result" once the "key" condition is matched. "Count" informs how many value pairs you're going to define. From there on it's a simple case of feeding the Key and Results values, for the example above you'd do:

Code: [Select]
Key | Result
----+-------
  1 |   2
  2 |   4

Play with it a bit and you'll soon get the hang of how it works. Hopefully it will save you plenty of time, enjoy! :)

Code: [Select]
// (c) Copyright HutongGames, LLC 2010-2011. All rights reserved.

using UnityEngine;

namespace HutongGames.PlayMaker.Actions
{
    [ActionCategory(ActionCategory.Convert)]
    [Tooltip("Converts an Integer variable value to another value and assigns the result to a second variable. Works as a case-switch structure.")]
    public class ConvertIntPairs : FsmStateAction
    {
        [RequiredField]
        [UIHint(UIHint.Variable)]
        public FsmInt inputVariable;
        [RequiredField]
        [UIHint(UIHint.Variable)]
        public FsmInt outputVariable;

        [CompoundArray("Count", "Key", "Result")]
        [Tooltip("Input and Output Values")]
        public FsmInt[] keys;
        public FsmInt[] results;

        public override void Reset()
        {
            keys = new FsmInt[0];
            results = new FsmInt[0];
        }

        public override void OnEnter()
        {
            for (int i = 0; i < keys.Length; i++)
            {
                if (!keys[i].IsNone && inputVariable.Value.Equals(keys[i].Value))
                {
                    outputVariable.Value = results[i].Value;
                    Finish();
                }
            }
            Finish();
        }
    }
}
« Last Edit: April 09, 2011, 01:58:43 PM by MaDDoX »
--
Breno "MaDDoX" Azevedo
@brenoazevedo

tobbeo

  • 1.2 Beta
  • Full Member
  • *
  • Posts: 186
Re: ConvertIntPairs - Finally a case-switch action for Playmaker!
« Reply #1 on: April 09, 2011, 02:23:02 AM »
I tried it a bit and it's awesome. Very handy! Thank you for sharing!

Alex Chouls

  • Administrator
  • Hero Member
  • *****
  • Posts: 3987
  • Official Playmaker Support
    • LinkedIn
Re: ConvertIntPairs - Finally a case-switch action for Playmaker!
« Reply #2 on: April 12, 2011, 03:32:15 AM »
Very nice! I have no objection to this ;) and I think you summed up the pros/cons quite well!

MaDDoX

  • 1.2 Beta
  • Full Member
  • *
  • Posts: 165
    • FluidPlay Studios
Re: ConvertIntPairs - Finally a case-switch action for Playmaker!
« Reply #3 on: April 13, 2011, 05:35:55 PM »
Very nice! I have no objection to this ;) and I think you summed up the pros/cons quite well!

Thanks Alex, glad to know :) So, I'll keep pushing the "algorithmic boundaries" of PlayMaker, whenever you realize I'm finally crossing the line feel free to warn people in the action thread heh :D Seriously, I'm more and more used to breaking the states into multiple states, it does help a lot especially with debugging, but in some cases - especially larger FSMs which I can't find a logical reason to break down into more FSMs - having some algorithmic power within the same state clears up a lot of mini-states and makes things easier to create, understand and support - which, at the end of the day, is what this is all about, right?

Peace!
--
Breno "MaDDoX" Azevedo
@brenoazevedo

jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 15500
  • Official Playmaker Support
Re: ConvertIntPairs - Finally a case-switch action for Playmaker!
« Reply #4 on: April 14, 2011, 02:18:38 AM »
Yes,

 I don't like as well when there is too much actions in a state, Very much like when a function does too much, you'll likely find down the road that you will anyway need to split it to accomodate a usage you haven't planned. Divide and conquer :)


 Bye,

 Jean