playMaker

Author Topic: Random Int "Fix" (inclusive switch added)  (Read 6264 times)

MaDDoX

  • 1.2 Beta
  • Full Member
  • *
  • Posts: 165
    • FluidPlay Studios
Random Int "Fix" (inclusive switch added)
« on: April 20, 2011, 04:58:10 PM »
[Edited to reflect discussion below]
The Current (1.1) Playmaker's Random Int works just like Random Range is designed to do in C# - excluding the last element. Nevertheless, at least for integer variables this is not a "designer friendly" behavior and might get people wondering why one of the values in the range is never reached. You can read more about the "issue" here.

To install it, you can simply open/edit the original randomint.cs from playmaker/actions folder and paste the following code. To use the original 'Max Value Exclusive' behavior simply uncheck the 'Inclusive Max' checkbox.

Code: [Select]
// (c) Copyright HutongGames, LLC 2010-2011. All rights reserved.
// 'inclusiveMax' option added by MaDDoX (@brenoazevedo)

using UnityEngine;

namespace HutongGames.PlayMaker.Actions
{
[ActionCategory(ActionCategory.Math)]
[Tooltip("Sets an Integer Variable to a random value between Min/Max.")]
public class RandomInt : FsmStateAction
{
[RequiredField]
public FsmInt min;
[RequiredField]
public FsmInt max;
[RequiredField]
[UIHint(UIHint.Variable)]
public FsmInt storeResult;
        [Tooltip("Should the Max value be included in the possible results?")]
        public bool inclusiveMax;

public override void Reset()
{
min = 0;
max = 100;
storeResult = null;
   inclusiveMax = true;
}

public override void OnEnter()
{
            storeResult.Value = (inclusiveMax) ?
                Random.Range(min.Value, max.Value + 1) :
                Random.Range(min.Value, max.Value);
   
            Finish();
}
}
}
« Last Edit: April 28, 2011, 12:10:07 PM by MaDDoX »
--
Breno "MaDDoX" Azevedo
@brenoazevedo

Alex Chouls

  • Administrator
  • Hero Member
  • *****
  • Posts: 3987
  • Official Playmaker Support
    • LinkedIn
Re: Random Int "Fix"
« Reply #1 on: April 20, 2011, 06:23:02 PM »
Maybe this should be an option on the action (E.g., an Inclusive check box).

The original behavior is useful when the int is a zero based index. E.g., Random Int using Get Child Count and then Get Child Num. With the new behavior, you'd have to make sure to subtract 1 from the child count...

jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 15500
  • Official Playmaker Support
Re: Random Int "Fix"
« Reply #2 on: April 21, 2011, 12:54:50 AM »
Hi,

 clear cut case of adding a check box to toggle this inclusive edit.

 Bye,

 Jean

MaDDoX

  • 1.2 Beta
  • Full Member
  • *
  • Posts: 165
    • FluidPlay Studios
Re: Random Int "Fix"
« Reply #3 on: April 25, 2011, 03:12:23 PM »
You're both right (as usual heh). I'll get to it, I've just returned from easter break - marvelous trip along the Brazilian northern coast beaches, whenever you guys need a host around here talk to me I'll gladly be your guide :)
--
Breno "MaDDoX" Azevedo
@brenoazevedo