I think this is Unity so another reason is that this kind of state machine stuff is better done in Mecanim with the random variable set directly and not hardcoded, so that artists can mess with the animations without having to change code. Of course this can be taken to the other extreme where you have state machine hell where a 10-line method would be more effective but a simple idle animation switch definitely falls more into the “do it in Mecanim” side. More procedural than any technical reason why but it’s still a reason.
(Not as important if you’re doing it alone, but it’s good to get good habits going)
Is it possible to have a weighted random animation trigger solely through Mecanim? I have a main idle state where as long as the character is in that state, a random idle animation will play and then transition back to the main state. (In this case "blink" is the most common random idle animation to play)
What I would do is simply have a Random variable in the state machine that gets populated every update or whatever with a new random number. Add a component to inject it or something. Of course you’ll have to juggle slicing up the range but it’s a lot simpler than changing the code, in that the artists won’t need to lock code files.
Edit: also the timer portion could be abstracted out as a component, but that’s a little more arguable.
from my googling, C# supports the ability for ranges in switches, but that's the only thing I can see that's bad here? looks fine implementation other than that
The method name does not reflect the functionality of the method. I'd expect a method called 'AdvanceTimer' to advance a timer, not also set an animation trigger off some random value.
It's an interesting approach but hard to read and hard to maintain. If you want to add more cases you have to start calculating fractions so the chances stay the same.
A better approach would probably be e.g. a map of some sort or a simple if-else
They have all of those empty cases that should be deleted. However, it does seem that they don’t want all the numbers to run the default. I would personally do
if (timer <= 0) {
r = Random.Range(0,11);
if (r<3){
controller.animator.SetTrigger((“worm”, “peck”, “blink”)[r]);
}
timer = Random.Range(0f, 2f);
}
Note: might not run because I did this on mobile
Edit: It seems I can’t get the format right. If someone could reply with the proper format I would greatly appreciate it.
You're all missing that the max value passed to Random.Range(int, int) is exclusive, assuming this is the Unity implementation. The result will never be 10.
The fact that the float overload is an inclusive max is interesting too... The true horror is always in the docs.
I mean it makes sense imo. With ints is easy to exclude a value but with floats it's much harder. What would be the last value before upper limit? Also from the experience it's just much more useful this way.
For sure, I don't doubt the use cases, but functions with the same name, especially overloaded only on type, should have the same semantics.
A better nterface design would pick either inclusive or exclusive as the default and implement RangeExlusive or RangeInclusive for the other, and just not implement RangeExlusive for floats, unless there's a use case for it.
In most languages and frameworks, the upper bounds are exclusive for both integers and floating point types. For ints in particular, exclusive upper bounds are way easier to work with.
So unity is being kind of 'special' here. The documentation is also a bit horror-like. Consider this:
Important: Both the lower and upper bounds are inclusive. Any given float value between them, including both minInclusive and maxInclusive, will appear on average approximately once every ten million random samples.
Given a sufficiently large range of values, there are way more than ten million possible floating point values between them. So it's mathematically impossible for all of those values to appear on average every ten million samples. Moreover, the floating point values in any given range will generally not be uniformly distributed (because of the whole floating point thing, floating point values with smaller magnitude are distributed more densely). So if every possible floating point value is equally likely, then the resulting probability distribution is not uniform. Which I'm pretty sure is not what they mean.
Honestly, I wonder if being upper limit exclusive affects the performance, though I've no idea how it works so maybe not. Anyway thanks for the examples, still imo inclusive upper limit for floats is much more useful, though I guess Unity could give both options if they wanted to.
I've never used Unity in my life so the only thing that made me check the docs was my nervousness at seeing an expectation of an inclusive range.
I'll distill my point from later on in the comment chain: you can either be consistent (with yourself, the rest of the language, or domain) or explicit (and usually it's best to be both). Inconsistent and implicit is how you end up in r/badcode.
Also from the experience it's just much more useful this way.
I'm curious in what situations you have found inclusive floating point ranges for random variables to be more useful. Unity is the only framework I know of that uses this convention. All languages that I know of use a half open range [min, max) in their standard libraries for both random integers and random doubles.
private void AdvanceTimer(PlayerController controller) {
timer -= Time.deltaTime;
if(timer <= 0) {
switch(Random.Range(0, 10)) {
case 0:
case 1:
case 2:
case 3:
case 4:
case 5:
controller.animator.SetTrigger("blink");
break;
case 6:
case 7:
case 8:
case 9:
controller.animator.SetTrigger("peck");
break;
case 10:
controller.animator.SetTrigger("worm");
break;
default: break;
}
timer = Random.Range(2.0f, 5.0f);
}
}
57
u/1N07 Apr 07 '22
Okay I'll bite. Why is this bad?
I mean I can come up with some critiques and ways to improve this, but it doesn't seem that bad.
86
u/kiwitims Apr 07 '22
You're all missing that the max value passed to Random.Range(int, int) is exclusive, assuming this is the Unity implementation. The result will never be 10.
The fact that the float overload is an inclusive max is interesting too... The true horror is always in the docs.
12
u/seeroflights Apr 07 '22
Image Transcription: Code
I'm a human volunteer content transcriber and you could be too! If you'd like more information on what we do and why we do it, click here!
19
u/knoam Apr 07 '22
I like it. I like when the structure of code explains itself visually. There's an intuitiveness to it.
2
u/iHappyTurtle Apr 07 '22
RIP to the worm!
2
u/shizzy0 Apr 07 '22
I don’t hate it.
1
u/bigbobclemson May 07 '22
Does python handle cases like that? That’s interesting
0
u/[deleted] Apr 07 '22
[deleted] — view deleted comment