Playmaker Forum

PlayMaker Updates & Downloads => Share New Actions => Topic started by: MaDDoX on April 20, 2011, 04:58:10 PM

Title: Random Int "Fix" (inclusive switch added)
Post by: MaDDoX 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 (http://answers.unity3d.com/questions/33371/random-range-if-conditions).

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();
}
}
}
Title: Re: Random Int "Fix"
Post by: Alex Chouls 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...
Title: Re: Random Int "Fix"
Post by: jeanfabre on April 21, 2011, 12:54:50 AM
Hi,

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

 Bye,

 Jean
Title: Re: Random Int "Fix"
Post by: MaDDoX 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 :)