playMaker

Author Topic: random int no repeat[SOLVED]  (Read 3770 times)

djaydino

  • Administrator
  • Hero Member
  • *****
  • Posts: 7616
    • jinxtergames
random int no repeat[SOLVED]
« on: January 26, 2015, 07:18:46 PM »
hi,
i'm making an action request but having a problem.
i have no errors on my action and with the debug log i get the desired result.

BUT when i play i can see that the "Store Result" is changed to none.
I think i'm just to tired atm to see whats wrong but i thought i drop it here :)

here's my code :

Code: [Select]
// (c) Copyright HutongGames, LLC 2010-2015. All rights reserved.
// 'inclusiveMax' option added by MaDDoX (@brenoazevedo)
// no repeat option added by DjayDino
/*--- __ECO__ __ACTION__ ---*/

using UnityEngine;

namespace HutongGames.PlayMaker.Actions
{
[ActionCategory(ActionCategory.Math)]
[Tooltip("Sets an Integer Variable to a random value between Min/Max.")]
public class RandomIntNoRepeat : 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 FsmBool noRepeat;

private int randomIndex;
private int lastIndex = -1;

public override void Reset()
{
min = 0;
max = 100;
storeResult = null;
// make default false to not break old behavior.
    inclusiveMax = false;
noRepeat = false;
}

public override void OnEnter()
{
PickRandom();
            Finish();
}

void PickRandom()
{
if (noRepeat.Value)
{
do
{
randomIndex = (inclusiveMax) ?
Random.Range(min.Value, max.Value + 1) :
Random.Range(min.Value, max.Value);
} while ( randomIndex == lastIndex);

lastIndex = randomIndex;
storeResult = randomIndex;

}else
{
randomIndex = (inclusiveMax) ?
Random.Range(min.Value, max.Value + 1) :
Random.Range(min.Value, max.Value);
storeResult = randomIndex;
}
Debug.Log("" +storeResult);
Debug.Log("" +randomIndex);
}
}
}


friendly greetings,

Dino
« Last Edit: January 27, 2015, 07:26:08 AM by djaydino »

Sly

  • Full Member
  • ***
  • Posts: 123
Re: random int no repeat
« Reply #1 on: January 26, 2015, 07:47:52 PM »
Hello,

I just checked quickly your code and it seem your storeResult(FsmInt) is egual to inclusiveMax. The problem is inclusiveMax is a boolean not an FsmInt. So probably it convert to none cause you're trying to assign a boolean value to an Int.

djaydino

  • Administrator
  • Hero Member
  • *****
  • Posts: 7616
    • jinxtergames
Re: random int no repeat
« Reply #2 on: January 26, 2015, 10:05:46 PM »
hmm i think your wrong there, i added debug.log for randomIndex and its also giving a random value as it is supposed to.
and this is working in random int action
« Last Edit: January 26, 2015, 10:09:08 PM by djaydino »

djaydino

  • Administrator
  • Hero Member
  • *****
  • Posts: 7616
    • jinxtergames
Re: random int no repeat
« Reply #3 on: January 26, 2015, 10:20:01 PM »
and tried it now without inclusive max :)

still the same...

if inclusiveMax is true it will do the Random.Range max.Value + 1

if false it will do the line without +1

and store the result to RandomIndex

it is just a shorter way to code it then using if true do this, else do this.

friendly greetings,

Dino

djaydino

  • Administrator
  • Hero Member
  • *****
  • Posts: 7616
    • jinxtergames
Re: random int no repeat
« Reply #4 on: January 27, 2015, 07:07:20 AM »
Lol, i had a good sleep now, found it i forgot to add .value to storeResult  ::)

action can be found here now :)

friendly greetings,

Dino