playMaker

Author Topic: float round  (Read 4502 times)

jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 14373
  • Official Playmaker Support
    • View Profile
float round
« on: August 07, 2012, 06:27:34 AM »
Hi,

 following a request, please find a action to round a float.

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

using UnityEngine;

namespace HutongGames.PlayMaker.Actions
{
[ActionCategory(ActionCategory.Math)]
[Tooltip("Sets a Float Variable to its round value. You can store the round value as a float or as an int")]
public class FloatRound : FsmStateAction
{
[RequiredField]
[UIHint(UIHint.Variable)]
public FsmFloat floatVariable;


[UIHint(UIHint.Variable)]
public FsmInt resultAsInt;


[UIHint(UIHint.Variable)]
public FsmFloat resultAsFloat;

public bool everyFrame;

public override void Reset()
{
floatVariable = null;
resultAsInt = null;
resultAsFloat = null;

everyFrame = false;
}

public override void OnEnter()
{
DoFloatRound();

if (!everyFrame)
Finish();
}

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

void DoFloatRound()
{
if (!resultAsInt.IsNone)
{
resultAsInt.Value = Mathf.RoundToInt(floatVariable.Value);
}
if (!resultAsFloat.IsNone)
{
resultAsFloat.Value = Mathf.Round(floatVariable.Value);
}
}
}
}

bye,

 Jean

amaranth

  • Full Member
  • ***
  • Posts: 172
    • View Profile
Re: float round
« Reply #1 on: August 07, 2012, 09:41:57 AM »
Perfect timing! I can see many uses for this in my game! Thank you!

Martin-Vaupell

  • Junior Playmaker
  • **
  • Posts: 69
  • Creating CarbonDiOxide
    • View Profile
    • Evisystems
Re: float round
« Reply #2 on: December 28, 2013, 09:25:34 AM »

Thank you, need this and found it VERY usefull, so it deserved a bump.

wonder why this old thing isen't in by default ;)

90degreedesigns

  • Playmaker Newbie
  • *
  • Posts: 13
    • View Profile
Re: float round
« Reply #3 on: May 22, 2015, 06:28:52 PM »
Is there a way to modify it to round it to nearest hundredth?
For example, 30.4512345 would be rounded to 30.45.

dudebxl

  • Hero Member
  • *****
  • Posts: 602
    • View Profile
Re: float round
« Reply #4 on: May 23, 2015, 05:40:05 AM »
did you try the action: Float Round To Nearest ??