playMaker

Author Topic: a simple Float question - how to exclude some range from random float  (Read 4478 times)

tyrot

  • Playmaker Newbie
  • *
  • Posts: 17
I want to create random trees along the road.
road starts X:-5 and ends X:5

How i can create float numbers between X -10 -5  and 5-10  but not in between -5 and 5

thank you 

tcmeric

  • Beta Group
  • Hero Member
  • *
  • Posts: 768
Re: a simple Float question - how to exclude some range from random float
« Reply #1 on: March 28, 2017, 12:30:37 AM »
As far as I can see. Either you need to make two different random floats (you can run them in the same state). One for each side of the road. This would be easiest.

The second is write a custom action using some examples from here: http://stackoverflow.com/questions/25891033/get-a-random-float-within-range-excluding-sub-range

The second would necessary if you need it to be truely evenly disturbed random number between your two ranges. Maybe for trees, that is not necessary?

tyrot

  • Playmaker Newbie
  • *
  • Posts: 17
Re: a simple Float question - how to exclude some range from random float
« Reply #2 on: March 28, 2017, 09:01:15 PM »
wow thanks tcmeric. I will check that script.

so far my workaround is .. making two floats as you mentioned and feeding them into 2 different Vector 3 and selecting random vector3 until array is completed .

thanks for your time! ..

tcmeric

  • Beta Group
  • Hero Member
  • *
  • Posts: 768
Re: a simple Float question - how to exclude some range from random float
« Reply #3 on: March 29, 2017, 01:51:19 AM »
Hi, so I wrote an action for it, essentially using the example script from stackoverflow.

You can download it from my github account here: https://github.com/dumbgamedev/general-playmaker/tree/master/Math

Called "randomFloatExclusion"

However, I am not sure it is working the way it should (The action works fine, but the math involved. Which is not my strong suit at all). Basically if you set the min excluded value as 3, sometimes it will return a value of 3.8, etc. (Not 4, but over 3).

Maybe I am misunderstanding something. Or maybe the script provided was not accurate.

How is your c#?

Maybe Jean can have a look.

Here is the code below (I wrote all of mine long hand (except their part). Note, i used playmaker variables and non playmaker variables, because playmaker variables cannot be used in some operations it seems).


Code: [Select]

public class  randomFloatExlusion : FsmStateAction
{

[RequiredField]
[TitleAttribute("Float Min")]
public FsmFloat minPlaymaker;

[RequiredField]
[TitleAttribute("Float Max")]
public FsmFloat maxPlaymaker;

[RequiredField]
[TitleAttribute("Float Min Exclusive")]
public FsmFloat minExclusivePlaymaker;

[RequiredField]
[TitleAttribute("Float Max Exclusive")]
public FsmFloat maxExclusivePlaymaker;

[TitleAttribute("Returned Value")]
public FsmFloat outcome;

[Tooltip("Repeat every frame")]
public bool everyFrame;

private float min;
private float max;
private float maxExclusive;
private float minExclusive;

public override void Reset()
{

everyFrame = false;
min = 0;
max = 0;
minExclusive = 0;
maxExclusive = 0;
outcome = null;

minPlaymaker = null;
maxPlaymaker = null;
minExclusivePlaymaker = null;
maxExclusivePlaymaker = null;
}


public override void OnEnter()
{

DoMath();

if (!everyFrame)
Finish();
}


public override void OnUpdate()
{
DoMath();
}


float DoMath()
{
min = minPlaymaker.Value;
max = maxPlaymaker.Value;
minExclusive = minExclusivePlaymaker.Value;
maxExclusivePlaymaker = maxExclusivePlaymaker.Value;

var excluded = maxExclusive - minExclusive;
var newMax = max - excluded;
outcome.Value = Random.Range (min, newMax);

return outcome.Value > minExclusive ? outcome.Value + excluded : outcome.Value;
}

}
}





jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 15500
  • Official Playmaker Support
Re: a simple Float question - how to exclude some range from random float
« Reply #4 on: March 29, 2017, 04:49:59 AM »
Hi,

 I'll have a look, but it seems to be good code wise at a glance.

 Bye,

 Jean

tyrot

  • Playmaker Newbie
  • *
  • Posts: 17
Re: a simple Float question - how to exclude some range from random float
« Reply #5 on: March 29, 2017, 04:09:56 PM »
wow i will check right away! thanks for looking it again!

jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 15500
  • Official Playmaker Support
Re: a simple Float question - how to exclude some range from random float
« Reply #6 on: March 31, 2017, 05:29:22 AM »
Hi,

 ok, few things :

- there is a typo in the action name :) it's randomFloatExclusion

- Actions category shoudl reflect more what it does, using "Custom" isn't going to be helpfull when glancing through the action list, it's of course a suggestion, if that helps you separating from official actions, it's good too.

- fsm variables that accepts results should always be forced to only be able to select an existing fsmvariable, as opposed to let the user input something inside the action interface and also should be mandatory. this is achieved with decorating the public property with

      
Code: [Select]
[RequiredField]
[UIHint(UIHint.Variable)]


Then, I am not sure what it does. For example I get this:



should it not be within the min max range?

so if I have this setup above, what values range should I expect from this action? something in between 0 and 2 or 9 and 10 right?

 Bye,

 Jean

tcmeric

  • Beta Group
  • Hero Member
  • *
  • Posts: 768
Re: a simple Float question - how to exclude some range from random float
« Reply #7 on: March 31, 2017, 12:00:09 PM »
Thanks for the tips. Yes, something is not quite right with the code.

it is based on this stackoverflow answer.

http://stackoverflow.com/questions/25891033/get-a-random-float-within-range-excluding-sub-range

I guess I couldnt quite figure out what was wrong.


jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 15500
  • Official Playmaker Support
Re: a simple Float question - how to exclude some range from random float
« Reply #8 on: April 04, 2017, 04:46:59 AM »
Hi,

 ok, I fixed it, basically the logic was right, but many issues and bug int he code where preventing it to work properly, let me explain each one of them:

Code: [Select]
maxExclusivePlaymaker = maxExclusivePlaymaker.Value;
here you assign the value of the property maxExclusivePlaymaker to itself, so the FsmFloat is now corrupted, you should never do that, but I think here it's a typo or bad copy paste.

Code: [Select]
outcome.Value = Random.Range (min, newMax);you assign an intermediate result to the FsmFloat accepting the final result, you should never do that, always store the result when you know this is the final one. because in your case, your function DoMath() returns the final result, but your code doesn't use it to store the DoMath() result in outcome so you never have the final result, only the intermediate one.

Please find the corrected version:

Bye,

 Jean

Code: [Select]
// Custom Action by DumbGameDev
// www.dumbgamedev.com

using UnityEngine;

namespace HutongGames.PlayMaker.Actions
{
[ActionCategory("Custom")]
[Tooltip("Find random float between two values but excluding a specific range")]
public class  randomFloatExclusion : FsmStateAction
{

[RequiredField]
[TitleAttribute("Float Min")]
public FsmFloat minPlaymaker;

[RequiredField]
[TitleAttribute("Float Max")]
public FsmFloat maxPlaymaker;

[RequiredField]
[TitleAttribute("Float Min Exclusive")]
public FsmFloat minExclusivePlaymaker;

[RequiredField]
[TitleAttribute("Float Max Exclusive")]
public FsmFloat maxExclusivePlaymaker;

[TitleAttribute("Returned Value")]
[RequiredField]
[UIHint(UIHint.Variable)]
public FsmFloat outcome;

[Tooltip("Repeat every frame")]
public bool everyFrame;

private float min;
private float max;
private float maxExclusive;
private float minExclusive;

float result;

public override void Reset()
{

everyFrame = false;
min = 0;
max = 0;
minExclusive = 0;
maxExclusive = 0;
outcome = null;

minPlaymaker = null;
maxPlaymaker = null;
minExclusivePlaymaker = null;
maxExclusivePlaymaker = null;
}


public override void OnEnter()
{

DoMath();

if (!everyFrame)
Finish();
}


public override void OnUpdate()
{
DoMath();
}


void DoMath()
{
min = minPlaymaker.Value;
max = maxPlaymaker.Value;
minExclusive = minExclusivePlaymaker.Value;
maxExclusive = maxExclusivePlaymaker.Value;

var excluded = maxExclusive - minExclusive;
var newMax = max - excluded;

result= Random.Range (min, newMax);

outcome.Value = result > minExclusive ? result + excluded : result;
}

}
}

tcmeric

  • Beta Group
  • Hero Member
  • *
  • Posts: 768
Re: a simple Float question - how to exclude some range from random float
« Reply #9 on: April 04, 2017, 08:27:57 AM »
Cool! First one was just a simple error on my end. The second one, I would not have figured out I think. I appreciate your help. I fixed my spelling, added my help video URL, added some extra comments at the top, changed it to the math category and added to my github: https://github.com/dumbgamedev/general-playmaker/tree/master/Math

I will move it over to the ecosystem later when I do my whole batch.

If anyone is interested, here is my tutorial video for this action (day 4 of 30), for randomly placing trees, excluding the path per the OPs comment. Mine also uses my loop counter (or you can use iterate) to determine how many trees to place  8)


jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 15500
  • Official Playmaker Support
Re: a simple Float question - how to exclude some range from random float
« Reply #10 on: April 04, 2017, 10:21:29 AM »
Hi,

 Excellent! Thanks for all this work you do for the community, it's very much appreciated!

let me know for the ecosystem move, We can have a call it will be quicker

Bye,

 Jean