PlayMaker Updates & Downloads > Share New Actions

New ConvertFloatToString with decimal places

(1/1)

Andre Khromov:
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: ---// (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);
}
}
}

--- End code ---

Andre Khromov:
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:
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

Andre Khromov:
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

Navigation

[0] Message Index

Go to full version