playMaker

Author Topic: Random Weighted int problem [SOLVED]  (Read 2224 times)

djaydino

  • Administrator
  • Hero Member
  • *****
  • Posts: 2280
    • View Profile
    • jinxtergames
Random Weighted int problem [SOLVED]
« on: October 26, 2014, 01:12:48 PM »
hi, i wanted to use the custom Action : Random Weighted int
But it is giving me the result 0,1,2,3,4,5
and not the numbers i placed in them.
i am using (testing) unity 4.6.0.b17

i have set it up like this :
« Last Edit: November 21, 2014, 07:50:52 AM by djaydino »

jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 12351
  • Official Playmaker Support
    • View Profile
Re: Random Weighted int problem
« Reply #1 on: October 27, 2014, 12:57:35 AM »
Hi,

oups.. Unfortunalty, there is a mis usage of the function, it actually returns the index of the int, not the int itself...

I have corrected the action, delete it from your project and redownload it from the ecosystem.


Bye,

 Jean


djaydino

  • Administrator
  • Hero Member
  • *****
  • Posts: 2280
    • View Profile
    • jinxtergames
Re: Random Weighted int problem
« Reply #2 on: October 27, 2014, 04:03:25 AM »
ah ok thx

djaydino

  • Administrator
  • Hero Member
  • *****
  • Posts: 2280
    • View Profile
    • jinxtergames
Re: Random Weighted int problem [SOLVED] but another problem found
« Reply #3 on: October 27, 2014, 02:54:39 PM »
hi, i was just testing the random weights and set it up like this :


free image uploading

The problem is that the result goes like this : 1 / random 2-6 / 1  / random 2-6 / 1  and so on

so when it hits 1 the next one can't be 1

the way thought it would be is that 1 would be the result  99/100 times.

is it suposed to be like this?

if so is there a different way to do the random?

thx for the help.
« Last Edit: October 27, 2014, 03:47:44 PM by djaydino »

jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 12351
  • Official Playmaker Support
    • View Profile
Re: Random Weighted int problem [SOLVED] but another problem found
« Reply #4 on: October 27, 2014, 10:18:49 PM »
Hi,

 This is not what I experience when every ints has the same weight. I guess that you are using random weighted Int because you are assigning various weights to the different values, so some values will be picked more than others.

 Can you show me your action and the number and their respective weight?

Bye,

 Jean

djaydino

  • Administrator
  • Hero Member
  • *****
  • Posts: 2280
    • View Profile
    • jinxtergames
Re: Random Weighted int problem [SOLVED] but another problem found
« Reply #5 on: October 28, 2014, 02:41:15 AM »
it is there in the picture :) click on it :)

the picture is not showing completely here :)

anyway i set the 1st weight to 1 and the 5 others on 0.01
« Last Edit: October 28, 2014, 02:46:03 AM by djaydino »

djaydino

  • Administrator
  • Hero Member
  • *****
  • Posts: 2280
    • View Profile
    • jinxtergames
Re: Random Weighted int problem [SOLVED] but another problem found
« Reply #6 on: October 31, 2014, 02:58:16 AM »
i have been testing with some different weight settings with 6 numbers) and i noticed that the random can't hit the same number 2x in a row.

i don't think the Random is supposed to be like that.
i tested also the random int action and that seems to be ok
and i tested the Random event and that does what i was expecting.

i will mess around with the script and let u know if i found the problem

djaydino

  • Administrator
  • Hero Member
  • *****
  • Posts: 2280
    • View Profile
    • jinxtergames
Re: Random Weighted int problem [SOLVED] but another problem found
« Reply #7 on: October 31, 2014, 03:19:21 AM »
i think i got it to work properly now, i commented out parts from your script and added some other, here is the script :

Code: [Select]
// (c) Copyright HutongGames, LLC 2010-2014. All rights reserved.
/*--- __ECO__ __ACTION__ ---*/

using UnityEngine;

namespace HutongGames.PlayMaker.Actions
{
[ActionCategory(ActionCategory.Math)]
[Tooltip("Pick a random weighted Int picked from an array of Ints.")]
public class RandomWeightedInt : FsmStateAction
{
[CompoundArray("Ints", "Int", "Weight")]
public FsmInt[] ints;
[HasFloatSlider(0, 1)]
public FsmFloat[] weights;

[RequiredField]
[UIHint(UIHint.Variable)]
public FsmInt result;

// private int randomIndex;
//        private int lastIndex = -1;

public override void Reset()
{
ints = new FsmInt[3];
ints[0] = 0;
ints[1] = 2;
ints[2] = 3;
weights = new FsmFloat[] {1,1,1};
result = null;

}
public override void OnEnter()
{
if (ints.Length > 0)
{
int randomIndex = ActionHelpers.GetRandomWeightedIndex(weights);

if (randomIndex != -1)
{

result.Value = ints[randomIndex].Value;
Finish();


return;
}
}

Finish();
}
// public override void OnEnter()
// {
// if (ints.Length > 0)
// {
// do
            // {
                // randomIndex = ActionHelpers.GetRandomWeightedIndex(weights);
            // } while ( randomIndex == lastIndex);

// lastIndex = randomIndex;
// result.Value = ints[randomIndex].Value;
// }

// Finish();
// }

}
}

i think it has something to do with these lines :
Code: [Select]
// } while ( randomIndex == lastIndex);

// lastIndex = randomIndex;
// result.Value = ints[randomIndex].Value;

you can change your script if you want to.

i can see some usefulness the way your script works now tho (if you don't want to hit the same number 2x in a row or spawn an object)
maybe a good idea is to add a check-box that says "Can Hit twice in a row"
so both methods can be used. i will play around some more with your script (sounds kinda weird huh  :P ) and i will post it if i get it to work.

greeting,

Dino

djaydino

  • Administrator
  • Hero Member
  • *****
  • Posts: 2280
    • View Profile
    • jinxtergames
Re: Random Weighted int problem [SOLVED] but another problem found
« Reply #8 on: October 31, 2014, 04:10:55 AM »
Ok i think i got this to work now  :o

Code: [Select]
// (c) Copyright HutongGames, LLC 2010-2014. All rights reserved.
/*--- __ECO__ __ACTION__ ---*/

using UnityEngine;

namespace HutongGames.PlayMaker.Actions
{
[ActionCategory(ActionCategory.Math)]
[Tooltip("Pick a random weighted Int picked from an array of Ints.")]
public class RandomWeightedInt : FsmStateAction
{
[CompoundArray("Ints", "Int", "Weight")]
public FsmInt[] ints;
[HasFloatSlider(0, 1)]
public FsmFloat[] weights;

[RequiredField]
[UIHint(UIHint.Variable)]
public FsmInt result;
[Tooltip("Can hit the same number twice in a row")]
public FsmBool canHitTwiceInARow;

private int randomIndex;
private int lastIndex = -1;

public override void Reset()
{
ints = new FsmInt[3];
ints[0] = 0;
ints[1] = 2;
ints[2] = 3;
weights = new FsmFloat[] {1,1,1};
result = null;
canHitTwiceInARow = false;
}
public override void OnEnter()
{
if (ints.Length > 0)
{


if (randomIndex != -1)
{
if (canHitTwiceInARow.Value)
{
randomIndex = ActionHelpers.GetRandomWeightedIndex(weights);
result.Value = ints[randomIndex].Value;
Finish();
}
else
{

{
do
{
randomIndex = ActionHelpers.GetRandomWeightedIndex(weights);
} while ( randomIndex == lastIndex);

lastIndex = randomIndex;
result.Value = ints[randomIndex].Value;
Finish();
}
}
}
}
}
}
}

you can copy this to your script if you want.

greetings kurt

djaydino

  • Administrator
  • Hero Member
  • *****
  • Posts: 2280
    • View Profile
    • jinxtergames
Re: Random Weighted int problem [SOLVED] but another problem found
« Reply #9 on: November 03, 2014, 02:49:21 PM »
bumb:

i bumbed this because i did not see a reply yet and i think it is important that the Random Weighted int works correctly :)

jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 12351
  • Official Playmaker Support
    • View Profile
Re: Random Weighted int problem [SOLVED] but another problem found
« Reply #10 on: November 04, 2014, 12:38:39 AM »
Hi,

 Thanks for bumping, I need this :)

 I have implemented your system, I think it's good. I have pushed it on github yo you can get this version via the ecosystem now.

 Bye,

 Jean

djaydino

  • Administrator
  • Hero Member
  • *****
  • Posts: 2280
    • View Profile
    • jinxtergames
Re: Random Weighted int problem [SOLVED] but another problem found
« Reply #11 on: November 06, 2014, 08:06:11 AM »
i just noticed that in "Random event" No Repeat is used, i will edit this script and set it to No Repeat also for better clarity.

Code: [Select]
// (c) Copyright HutongGames, LLC 2010-2014. All rights reserved.
/*--- __ECO__ __ACTION__ ---*/
// http://hutonggames.com/playmakerforum/index.php?topic=8665.msg41731#msg41731

using UnityEngine;

namespace HutongGames.PlayMaker.Actions
{
[ActionCategory(ActionCategory.Math)]
[Tooltip("Pick a random weighted Int picked from an array of Ints.")]
public class RandomWeightedInt : FsmStateAction
{
[CompoundArray("Ints", "Int", "Weight")]
public FsmInt[] ints;
[HasFloatSlider(0, 1)]
public FsmFloat[] weights;

[RequiredField]
[UIHint(UIHint.Variable)]
public FsmInt result;
[Tooltip("Don't repeat the same Int twice in a row.")]
public FsmBool noRepeat;

private int randomIndex;
private int lastIndex = -1;

public override void Reset()
{
ints = new FsmInt[3];
ints[0] = 1;
ints[1] = 2;
ints[2] = 3;
weights = new FsmFloat[] {1,1,1};
result = null;
noRepeat = false;
}
public override void OnEnter()
{

PickRandom();
Finish ();
}

void PickRandom()
{
if (ints.Length ==  0)
{
return;
}

if (noRepeat.Value)
{
do
{
randomIndex = ActionHelpers.GetRandomWeightedIndex(weights);
} while ( randomIndex == lastIndex);

lastIndex = randomIndex;
result.Value = ints[randomIndex].Value;

}else
{
randomIndex = ActionHelpers.GetRandomWeightedIndex(weights);
result.Value = ints[randomIndex].Value;
}
}
}
}
when i have some extra time i will make a bundle from the other randoms and implement No Repeat to them.

jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 12351
  • Official Playmaker Support
    • View Profile
Re: Random Weighted int problem [SOLVED] but another problem found
« Reply #12 on: November 06, 2014, 09:26:54 AM »
Hi,

 cool! If you do that, can you name them differently then the official one, then we can put them directly on github ready for download :) otherwise they are not really usable since they would get overwritten everytime you update PlayMaker.

If you want to work directly on the ecosystem github rep, let me know, we can create branch and fork to merge and moderate.

 Bye,

 Jean

djaydino

  • Administrator
  • Hero Member
  • *****
  • Posts: 2280
    • View Profile
    • jinxtergames
Re: Random Weighted int problem [SOLVED] but another problem found
« Reply #13 on: November 20, 2014, 03:46:45 AM »
i was actually thinking that it would be nice if it would be added in the original Actions on a next update.
but it can be done in custom actions also,
any suggestion how to name them?
 
sorry for this late reaction, i went to Philippines and it took a while to get some decent internet connection.

i will try to get them done as soon as i can
what do i need to do to work directly on the ecosystem ?

greetings djaydino

jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 12351
  • Official Playmaker Support
    • View Profile
Re: Random Weighted int problem [SOLVED] but another problem found
« Reply #14 on: November 20, 2014, 10:58:18 PM »
Hi,

You can use "RandomWeightInt2" for example.

To put it directly into the ecosystem, you need to fork the github public repository and have some keywords in the action comments ( /*--- __ECO__ __ACTION__ ---*/), then the action will become visible once I merge your fork into the master.

the repo where this action is currently hosted is:

https://github.com/jeanfabre/PlayMakerCustomActions_U3

if you have unity 4 actions, then you have another rep:

https://github.com/jeanfabre/PlayMakerCustomActions_U4

shall I invite you on this repo?

Bye,

 Jean