playMaker

Author Topic: Translate To Bar Value (good for NGUI)  (Read 3016 times)

FRickReich

  • Playmaker Newbie
  • *
  • Posts: 21
Translate To Bar Value (good for NGUI)
« on: February 04, 2014, 08:13:15 AM »
Hey there,

i found it a bit annoying to always create 2 float operators to render out "width = hp / max_hp * max_width" to convert a value to the length of a bar in NGUI so i created this little playmaker action, hope you can make some good use of it!





Code: [Select]
// (c) 2014 F. Rick Reich.

using UnityEngine;

namespace HutongGames.PlayMaker.Actions
{
[ActionCategory("Custom")]
[Tooltip("Translates a float to the width of a Progressbar (Usable for eg. Healthbars).")]
public class TranslateToBarValue : FsmStateAction
{

[RequiredField]
public FsmFloat CurrentValue;

[RequiredField]
public FsmFloat MaxValue;

[RequiredField]
public FsmFloat MaxBarWidth;

public FsmFloat Result;

public bool everyFrame;

public override void Reset()
{
CurrentValue = null;
MaxValue = null;
MaxBarWidth = 1.0f;
Result = null;
everyFrame = false;
}

public override void OnEnter()
{

Result.Value = CurrentValue.Value / MaxValue.Value * MaxBarWidth.Value;

if (!everyFrame)
{
Finish();
}
}

public override void OnUpdate()
{
Result.Value = CurrentValue.Value / MaxValue.Value * MaxBarWidth.Value;
}
}
}
« Last Edit: February 04, 2014, 09:24:04 AM by FRickReich »

jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 15500
  • Official Playmaker Support
Re: Translate To Bar Value (good for NGUI)
« Reply #1 on: February 04, 2014, 12:46:28 PM »
Hi,

 Nice!  It's actually very similar to the floatRemap custom action ( or better known as cross multiplication)

http://hutonggames.com/playmakerforum/index.php?topic=5122.msg24311#msg24311


Your action is more obvious to use, and that's very good. tho, if you you find yourself having to remap floats in a more complex set of ranges, FloatRemap will be handy.

bye,

 Jean

FRickReich

  • Playmaker Newbie
  • *
  • Posts: 21
Re: Translate To Bar Value (good for NGUI)
« Reply #2 on: February 04, 2014, 06:14:30 PM »
Hey Jean, didn't know about your Action right there  :-\, but i can see the similarities.

I think yours can be used for anything around cross-multiplications while mine is, even if usable for the same thing, more specifically for things like a Healthbar.

- Rick.