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:
If difficulty = 1
--- gamespeed = 2
If difficulty = 2
--- gamespeed = 4
(...)
In C#, that would be written as such:
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:
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!
// (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();
}
}
}