playMaker

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

MaDDoX

  • 1.2 Beta
  • Full Member
  • *
  • Posts: 165
    • View Profile
    • FluidPlay Studios
Random Int "Fix" (inclusive switch added)
« on: April 20, 2011, 01: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, 09:10:07 AM by MaDDoX »
--
Breno "MaDDoX" Azevedo
@brenoazevedo

Alex Chouls

  • Administrator
  • Hero Member
  • *****
  • Posts: 3205
  • Official Playmaker Support
    • View Profile
    • LinkedIn
Re: Random Int "Fix"
« Reply #1 on: April 20, 2011, 03: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: 12344
  • Official Playmaker Support
    • View Profile
Re: Random Int "Fix"
« Reply #2 on: April 20, 2011, 09:54:50 PM »
Hi,

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

 Bye,

 Jean

MaDDoX

  • 1.2 Beta
  • Full Member
  • *
  • Posts: 165
    • View Profile
    • FluidPlay Studios
Re: Random Int "Fix"
« Reply #3 on: April 25, 2011, 12: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