playMaker

Author Topic: stringtoVector3  (Read 2114 times)

jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 14237
  • Official Playmaker Support
    • View Profile
stringtoVector3
« on: December 29, 2011, 03:19:30 AM »
Hi,

 following Mark_T requests http://hutonggames.com/playmakerforum/index.php?topic=822.msg3974#msg3974

Please 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:

Code: [Select]
"[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

Code: [Select]
"Vector3\([x],[y],[z]\)"

Questions always welcome.


Code: [Select]
// (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

Mark_T

  • 1.2 Beta
  • Junior Playmaker
  • *
  • Posts: 72
    • View Profile
Re: stringtoVector3
« Reply #1 on: December 29, 2011, 05:42:14 PM »

Thank you so much for all your help. :)



Andrew.Lukasik

  • Full Member
  • ***
  • Posts: 134
    • View Profile
    • my twitter @andrewlukasik
Re: stringtoVector3
« Reply #2 on: January 09, 2012, 03:45:05 AM »
Good addition! I think it should be linked in "Share New Playmaker Actions" also