Hi,
following Mark_T requests
http://hutonggames.com/playmakerforum/index.php?topic=822.msg3974#msg3974Please find a string parser for vector3. It's actually quit flexible ( actually ultra flexible!!!):
You can define the format with simple placeholders for x y and z like so:
"[x] [y] [z]"
which means it will parse into a vector 3 something like "1 2 3"
bonus:
-- it trims whitespaces automatically
-- exponential notation parsed ( "1e-01" will work
)
the default format is the following "Vector3(x,y,z)". note that the ( and ) char are escaped, else the regex will not work, that's something to be careful with, I could automate these.... if you shout loud enough, maybe I will
so the corresponding format is
"Vector3\([x],[y],[z]\)"
Questions always welcome.
// (c) Copyright HutongGames, LLC 2010-2011. All rights reserved.
using UnityEngine;
using System.Text.RegularExpressions;
namespace HutongGames.PlayMaker.Actions
{
[ActionCategory(ActionCategory.String)]
[Tooltip("Parse a string into a vector3 variable. Format is flexible, [x] [y] and [z] are placeholders, escape special chars like \\( and \\)")]
public class StringToVector3 : FsmStateAction
{
[RequiredField]
[UIHint(UIHint.Variable)]
public FsmString source;
public FsmString format;
[RequiredField]
[UIHint(UIHint.Variable)]
public FsmVector3 storeResult;
public override void Reset()
{
source = null;
format = "Vector3\\([x],[y],[z]\\)";
storeResult = null;
}
public override void OnEnter()
{
DoParsing();
Finish();
}
void DoParsing()
{
if (source == null) return;
if (storeResult == null) return;
string floatregex = "[-+]?[0-9]*\\.?[0-9]+([eE][-+]?[0-9]+)?";
string fullregex = format.Value;
// could be improved with some more regex to match x y z and inject in the replace... but let's not go there...
fullregex = fullregex.Replace("[x]","(?<x>" + floatregex + ")");
fullregex = fullregex.Replace("[y]","(?<y>" + floatregex + ")");
fullregex = fullregex.Replace("[z]","(?<z>" + floatregex + ")");
fullregex = "^\\s*" + fullregex;
Regex r = new Regex(fullregex);
Match m = r.Match(source.Value);
if ( m.Groups["x"].Value!="" && m.Groups["y"].Value!="" && m.Groups["z"].Value!="" ){
storeResult.Value = new Vector3(float.Parse(m.Groups["x"].Value),float.Parse(m.Groups["y"].Value),float.Parse(m.Groups["z"].Value));
}
}
}
}
Bye,
Jean