playMaker

Author Topic: Set Sub String  (Read 4187 times)

jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 15500
  • Official Playmaker Support
Set Sub String
« on: July 09, 2012, 05:09:19 AM »
Hi,

 following a request: http://hutonggames.com/playmakerforum/index.php?topic=1874.0

 please find an action that can replace a range from a string with another string. It's not a replace, as replacing a word by another, here you define the starting character and the length.

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

using UnityEngine;

namespace HutongGames.PlayMaker.Actions
{
[ActionCategory(ActionCategory.String)]
[Tooltip("Sets a sub-string from a String Variable.")]
public class SetSubstring : FsmStateAction
{
[RequiredField]
public FsmString stringVariable;

[RequiredField]
public FsmInt startIndex;
[RequiredField]
public FsmInt length;

public FsmString subString;


[RequiredField]
[UIHint(UIHint.Variable)]
public FsmString storeResult;
public bool everyFrame;

public override void Reset()
{
stringVariable = null;
startIndex = 0;
length = 1;
storeResult = null;
subString = null;

everyFrame = false;
}

public override void OnEnter()
{
DoSetSubstring();

if (!everyFrame)
Finish();
}

public override void OnUpdate()
{
DoSetSubstring();
}

void DoSetSubstring()
{
if (stringVariable == null)
{
return;
}
if (storeResult == null)
{
return;
}
if (subString == null)
{
return;
}



string _base = stringVariable.Value;

if (startIndex.Value >= _base.Length)
{
return;
}

int _length = Mathf.Min(_base.Length-startIndex.Value,length.Value);
_base = _base.Remove(startIndex.Value,_length);

_base = _base.Insert(startIndex.Value,subString.Value);

storeResult.Value = _base;
}

}
}

 Bye,

 Jean

Groo Gadgets

  • Beta Group
  • Full Member
  • *
  • Posts: 175
Re: Set Sub String
« Reply #1 on: July 10, 2012, 10:10:06 PM »
Hey Jean,

Thank you so much for this awesome new action, it will be invaluable for my word game project. I don't have time to test it out this week since I have all 3 of my kids during their school holiday but I will be sure to test it out this weekend and report my results.

Thanks again!

Simon

jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 15500
  • Official Playmaker Support
Re: Set Sub String
« Reply #2 on: July 11, 2012, 03:27:41 AM »
Hi,

 No rush, enjoy your holiday with your kids :)

Bye,

 Jean