playMaker

Poll

Helpful

yes
1 (100%)
no
0 (0%)

Total Members Voted: 1

Author Topic: New ConvertFloatToString with decimal places  (Read 1472 times)

Andre Khromov

  • 1.2 Beta
  • Playmaker Newbie
  • *
  • Posts: 12
    • View Profile
New ConvertFloatToString with decimal places
« on: June 13, 2011, 02:15:07 AM »
Just wanted to post a new version of ConvertFloatToString action with a decimal place cut off.

This code is directly from the Playmaker C# file with the truncation of the string at the end.

Enjoy

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

using UnityEngine;

namespace HutongGames.PlayMaker.Actions
{
[ActionCategory(ActionCategory.Convert)]
[Tooltip("Converts a Float value to a String value.")]
public class ConvertFloatToString : FsmStateAction
{
[RequiredField]
[UIHint(UIHint.Variable)]
public FsmFloat floatVariable;
[RequiredField]
[UIHint(UIHint.Variable)]
public FsmString stringVariable;
public FsmInt numberOfDecimalPlaces;
public bool everyFrame;

public override void Reset ()
{
floatVariable = null;
stringVariable = null;
numberOfDecimalPlaces = 2;
everyFrame = false;
}

public override void OnEnter()
{
DoConvertFloatToString();

if (!everyFrame)
Finish();
}

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

void DoConvertFloatToString ()
{
numberOfDecimalPlaces.Value = (numberOfDecimalPlaces.Value < 0) ? 0 : numberOfDecimalPlaces.Value; // doesn't allow negative numbers
stringVariable.Value = floatVariable.Value.ToString ("F" + numberOfDecimalPlaces.Value);
}
}
}

Andre Khromov

  • 1.2 Beta
  • Playmaker Newbie
  • *
  • Posts: 12
    • View Profile
Re: New ConvertFloatToString with decimal places
« Reply #1 on: June 13, 2011, 02:21:13 AM »
Also, I apologize that this post was a poll, but I couldn't see any other way to post a new message. Someone please clue me in for next time.

Thanks,

Andre

MaDDoX

  • 1.2 Beta
  • Full Member
  • *
  • Posts: 165
    • View Profile
    • FluidPlay Studios
Re: New ConvertFloatToString with decimal places
« Reply #2 on: June 13, 2011, 09:45:35 PM »
Nice. You can also check this action, to convert int to string using any format you want, you can adapt its code to, for instance, allow the end-user to define how many decimal places he wants for the float: http://hutonggames.com/playmakerforum/index.php?topic=267.0
--
Breno "MaDDoX" Azevedo
@brenoazevedo

Andre Khromov

  • 1.2 Beta
  • Playmaker Newbie
  • *
  • Posts: 12
    • View Profile
Re: New ConvertFloatToString with decimal places
« Reply #3 on: June 13, 2011, 10:27:59 PM »
Haha! I almost coded that! But it was late and I thought:

well... you know I could put in a format string text box, or I can cover what I want to do and probably 90% of the people like me.

So you totally did the right thing, and I cheesed out, but I thought I post it because hey... I saw someone post FloatSubtract when you can just use FloatAdd with a negative number :P