playMaker

Author Topic: float round  (Read 7425 times)

jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 15500
  • Official Playmaker Support
float round
« on: August 07, 2012, 09: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
Re: float round
« Reply #1 on: August 07, 2012, 12:41:57 PM »
Perfect timing! I can see many uses for this in my game! Thank you!

Martin-Vaupell

  • Junior Playmaker
  • **
  • Posts: 70
  • Creating CarbonDiOxide
    • Evisystems
Re: float round
« Reply #2 on: December 28, 2013, 11: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
Re: float round
« Reply #3 on: May 22, 2015, 09: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
Re: float round
« Reply #4 on: May 23, 2015, 08:40:05 AM »
did you try the action: Float Round To Nearest ??