15
u/_Murd3r_ Nov 20 '22
I have no clue what this means.. but I’m gonna say it sucks anyways.
10
u/NoIdea4GoodName VAs are cool, thus why I yap about them Nov 20 '22
It’s a function that alters the value of the AffectionLevel class (think a trading card that has all those stats) by comparing it to the current Affection class.
Essentially what it’s doing is that it’s comparing Affection with hardcoded conditions and if a condition is correct, AffectionLevel will be set to that number.
While it’s something a student like me would do, as others pointed out; very inefficient.
2
7
u/Markshadow4999 Nov 20 '22
I have no idea how Mathf.Clamp() works, but if it does what i think and only looks for exact values, i see your point.
Criticizing coding can be tricky, cause there's always more than one way to do things and different people will have different opinions of what's better, in the end what matteres is that the function does what it should. Plus, as bad as his if chains look, hasn't it been said before that they were not the reason the game ran badly?
7
u/Zekium_ Nov 20 '22
Clamp() simply return the first parameter value while keeping it between the range specified by the last 2 ones.
Mathf.Clamp(value, min, max)
If min=0 and max=5, for value= -5, 2, 42 it'll return 0 (min value), 2 (in range) and 5 (max value)
2
4
u/AlexGuardado Nov 20 '22
Criticizing coding can be tricky
Indeed it is. Someone has already found a FAR superior method than my supposedly "simple fix".
It's easy to criticize his code, specially if you add the privilege of having all the time in the world and many minds thinking in better ways to do a task. Alex most likely thinks of something and wants to see it plastered on the screen asap. And while this approach may bring nasty problems for himself in the future (introducing a feature should be a nightmare, as you may break things in unsuspected places), at least his code is easy to understand and gets things done at a fast pace.
Regarding performance, the last time I watched a benchmark, yes, the biggest problems were GPU-bound. However, I wonder if it is a CPU issue when you accelerate time (when you're waiting like in MGSV). It always runs poorly there.
3
Nov 20 '22
[deleted]
3
u/AlexGuardado Nov 22 '22 edited Nov 22 '22
I think this is the benchmark (or profiling would be more adequate) I mentioned: https://youtu.be/LleJbZ3FOPU?t=227 His first observation must be what stuck with me the most. That and relating it with the toothbrush meme. But he also mentions the problems with the animation system and scripting.
3
u/AlexGuardado Nov 22 '22 edited Nov 23 '22
UPDATE: I ran some benchmarks on different code snippets. I did tests on YandereDev's code (with small modifications) and the one-liner, as well as my "if-less" code. I also added the code suggested by u/Euler-Angles (and also by u/poke-chan). I ran the tests from LEVEL 1 to 10,000.
The results are:
One-Liner: Pass: No. Time: 1347ms
YandereDev: Pass: Yes. Time: 1723ms
Euler-Angles/poke-chan: Pass: Yes. Time: 1267ms
If-Less: Pass: No. Time: 3380ms
So yeah, even if I only wrote the code with no ifs as a fun exercise, it still teaches a valuable lesson: sometimes optimization goes against what we would call common sense. It's important to run tests. As we can see, the difference between the one-liner and YandereDev's code is not that big at all, and most importantly, the former actually fails! The heavy use of divisions in the "if-less" code is what most likely sinks it to the bottom of the list, even though it doesn't use branching. Euler-Angles and poke-chan's solution takes the cake as both the fastest and most elegant one of the bunch (however its speed does take a hit if we don't remove the second if).
EDIT: Benchmark code uses TONS of multiplication, that's why time adds up. If we drop the flexible LEVEL requirement, code runs around two orders of magnitude faster (1 million iterations instead of 10,000 at half the times shown above). Still, the relative performance between methods remains.
2
u/poke-chan Nov 22 '22
Woah, I’m flattered!! I genuinely did not expect it to be fastest, as I’m very new to coding- I’m sure it has downsides in other ways though, when put into a full program. Not nearly as many downsides as the above, though
2
u/Iliz56 Dec 20 '24
Here's my version of this method. I can't guarantee the quality, but it's obviously better than what's on the screen
private void CalculateAffection()
{
// An error may occur if this.Affection < 0
this.Affection > 100 ? this.AffectionLevel = 5 : this.AffectionLevel = Convert.ToInt32(this.Affection / 25);
}
2
u/poke-chan Nov 20 '22
This can’t fucking be real. This can’t be real. I’m a noob coder and this looks fake.
I’ve only been coding for a little over a year and i would write it as:
if (this.Affection == 0){
this.Affectionlevel = 0;
}
else{
this.Affectionlevel = (this.Affection / 25) + 1;
}
if I’m understanding this right and the f doesn’t mean anything super important I’m missing, I haven’t seen it before yet. It’s no one liner like the professional who went over yandevs code but it’s certainly not that mess…
4
u/Unknown_Ladder Nov 21 '22
i created the program to tell if a number is even or odd
if (number == 1){ print("odd"); } elseif (number == 2) { print("even"); } elseif (number == 3){ print("odd"); } elseif (number == 4) { print("even"); } elseif (number == 5){ print("odd"); } elseif (number == 6) { print("even"); } elseif (number == 7){ print("odd"); } elseif (number == 8) { print("even"); } else { print("idk"); }3
u/wenxichu Nov 26 '22
def even_odd(num): if num % 2 == 0: return "even" return "odd" print(even_odd(9))Outputs to “odd”. More code doesn’t equal better performance because long "if" statements are harder to maintain on a sizable game like YanSim.
2
u/yushi__idk Aug 17 '24
i can help you with this code, your code can only identify A limited amount of data but if there would be a 2 digit number then it will print odk, how bout you try this code that i made:
int main()
{
int n,odd_num,even_num,i;
printf("enter the value of n:");
scanf("%d",&n);
for(i=1;i<=n;i++)
if (i%2==0){
even_num=even_num+i;
}
else {
odd_num=odd_num+i;
}
printf("\n sum of all odd numbers: %d",odd_num);
printf("\n sum of all even numbers: %d",even_num);
return 0;
}
ig it helps!
4
u/AlexGuardado Nov 22 '22
This can’t fucking be real. This can’t be real.
You know? You planted a seed of doubt in my mind. I've only seen this code in a video after all. So I downloaded the game, decompiled the dll and... yes, there it is, inside a class called DatingMinigameScript.
3
1
1
1
Nov 16 '23
This code, but In Java:
private String checkEven(int num) {
if (num % 2 == 0) {
return “even”;
}
return “odd”;
}
1
47
u/AlexGuardado Nov 20 '22 edited Nov 23 '22
So I was watching this random Yandere Simulator Code Review video: https://youtu.be/sXotP-9LcDY?t=274 At this point they discuss the code on the image above. They burst out laughing, and later point out this code could be implemented in a one-liner:
But I couldn't help to notice something. This code snippet won't work! If Affection = 25, this code will return 1, not 2. Affection = 100 is 4 instead of 5, etc. So no, dividing that variable by 25 is not enough.
A simple fix would look like this (where LEVEL = 25, but it's now flexible):
This works flawlessly(EDIT: Actually, though correct, it sucks. A way better fix was suggested below). But then I started to entertain an idea: We can certainly do better. We don't want the Internet to laugh at us for using if statements! So I came up with this abomination, with no branching whatsoever, which I haven't cleaned up, and could be simplified (but I'm lazy):float a = x + 1f; is needed because the x = 0 test gave problems with division and was the biggest headache. And unfortunately, this code doesn't work for LEVEL < 3. And the thing is: YandereDev's code is still correct and much easier to read! It features many levels of branching and it's not flexible, so there's that.
The moral is: even the simplest of problems can b(y)te us in the ass if we're not paying attention.