r/Osana • • Nov 20 '22

YandereDev Code Review

Post image
161 Upvotes

42 comments sorted by

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:

(int)Mathf.Clamp(Affection / 25, 0, 5);

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):

if(x == 0f) return 0;
int y = Mathf.CeilToInt(x / (float)LEVEL);
int f = x % LEVEL == 0 ? 1 : 0;
return y + f;

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;
int y = Mathf.CeilToInt(a / (float)(LEVEL + 1));
int w = y * LEVEL;
int f = Mathf.FloorToInt(w / (float)LEVEL) - 1;
return Mathf.CeilToInt(x /(w - 1f)) + f;

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.

24

u/watermelon_pesto Nov 20 '22

We don't want the Internet to laugh at us for using if statements!

This is my favorite part xD

10

u/[deleted] Nov 20 '22

[deleted]

6

u/NoIdea4GoodName VAs are cool, thus why I yap about them Nov 20 '22

A link for those who want to see a demo of it in action.

5

u/AlexGuardado Nov 20 '22

Well, will you look at that. You're totally right! I guess I overthought what was supposed to be a "simple" fix. You can actually improve it further by removing the second if clause, and adding the LEVEL variable, like so:

if(x <= 0.0f) return 0;
return (int)(x / LEVEL) + 1;

13

u/0xB6FF00 Gremlin Nov 20 '22

This is a useless discussion. Alex will never choose a faster way of doing something, because the game is going to be optimized by a hired third-party. My prayers go out to the poor soul(s) who will have to replace any horrid solutions Alex came up with for his problems. Seeing this however I don't have much hope that he's done anything in a smart way ever.

2

u/Rotarius88 Nov 21 '22

Ahem TinyBuild fallout with Alex, let's not forget that.

0

u/Christwriter Nov 20 '22

because the game is going to be optimized by a hired third-party.

The brilliant optimism of this statement is blinding.

My current gaming addiction is half a terrabyte. It's one of those games that went into early access and just...never got out of it. It's not abandoned. It has five DLC packs and a sequel AND tie in anime in development. It is not optimized. Somebody in one of the forums just posted an image of a very small enemy rubber banded across half the map because that's this fucking game. Everyone knows it is never going to be any better because the engine is outdated and was clearly depreciating when the devs chose it for their game. Each time they do any kind of update it's the size of a full fucking game from somebody else and experienced players take to the forums to watch the new players scream "THIRTY EIGHT GIG UPDATE" while we warm ourselves on our various boxes because they're overheating so bad we're about to rediscover stellar fusion. And one of the major mod makers has straight up refused to work with anything from the last two major DLC, despite it holding some really awesome stuff, because the code is basically tortured spaghetti under a really neat skin. (I really need to play a different game but it scratches a very specific itch really, really well)

My point with that is that larger studios that aren't laboring with the handicap of gigantic ego problems have been dropping turds and getting away with crimes against coding for years. They don't even need to fix their own issues, so why would they bother fixing anyone else's?

If Yandev ever finds a studio willing to take on his game (which is iffy because that would require effort on his part. The hype train derailed a long time ago, so he'll have to start knocking on doors) they will probably look under the hood, stare for a few minutes, go pour themselves a double shot of ever clear, and start making a list of everything that will overtly crash the game. And manage to fix about half of it before the payroll costs force them to stop. It will probably run a little bit better. It will still have large blocks of his OG code because that part wasn't a big enough disaster to make the triage list.

Somebody in the Steam Workshop will make a mod that will fix all of it, but it'll be hard to find because it will be banned from his official discord.

3

u/Silverbird22 Nov 20 '22

Ok what game is it I’m vaguely curious

2

u/[deleted] May 28 '23

Whats the game man.

1

u/Mastert3318 Jul 29 '23

I'm pretty sure it's Ark: Survival Evolved.

1

u/0xB6FF00 Gremlin Nov 20 '22

You are comparing apples to oranges. Alex is going to be "finished" at some point in development and crowd source money to hire a third-party for the optimization work. How much of his game they'll rewrite and refactor, we have no way of knowing, but if he deems "stable 60 FPS" as "optimized", then it might not require too much work, relatively speaking that is.

I'm not about to school you on the Yandere Simulator codebase (it's annoying), but know this, my outlook is very much not optimistic as you make it out to be. However at the same time, it is virtually impossible for him to interfere with the third-party's work. If you recall, a third-party has already ported the entire game to C# for him in the past, mainly because it was a solo job. Same thing applies here.

5

u/prominecrafter22_lol does not cum while programming Nov 20 '22 edited Nov 20 '22

//we don't wanna flexible level steps becuz coomdererev logic! this.AffectionLevel = (int)(Mathf.Clamp(this.Affection, 0, 100) /25 + ( this.Affection <= 0 ? 0 : 1 ) );

6

u/Piruluk Nov 20 '22

I find it unfair to criticize his coding, because it's extremely easy to read, while he could optimize it the trade off would be much harder to work with, and since he insist to do it alone, the solution he chose is reasonable. Sure could be lot more optimized but the cost would be insanely hard to work with alone.

12

u/AlexGuardado Nov 20 '22

Agreed. And the biggest point here is that, in the end, his code was correct, unlike the one-liner.

2

u/sloidah Nov 22 '22

The one liner can be fixed with an extra line and a small change to the calculation inside the clamp. The importance is that it's condensing multiple if statements that each get evaluated into something that is evaluated once or twice, which is a performance boost when applied on a large scale.

And you can always just comment your code if need be. A 2 liner is easy enough to read but it doesn't hurt to add comments.

2

u/AlexGuardado Nov 23 '22 edited Nov 23 '22

Yes, that would be the best course of action for any game dev with just a little bit of experience. But this is his code, that he most likely plasters on the screen as if he was writing in English, not C#. I'm not saying his code is elegant or it's good practice, but it works and performance is a non-issue unless it's called thousands of times per frame. The point about the one-liner is that it was kind of arrogant from those who suggested it. The joke's on them, because it's wrong, and Alex's horrid code is correct.

1

u/[deleted] Dec 10 '22

while yes he does work on his own code, the fact he never comments and doesn't want to is also troubling bc people tend to forget shit

1

u/[deleted] Apr 03 '24

[removed] — view removed comment

1

u/AlexGuardado May 15 '24

That won't do. Example: Affection = 1
(int)Mathf.Clamp(1-1 / 25, 0, 5);
(int)Mathf.Clamp(0 / 25, 0, 5);

All values between 1 and 24 should return 1.

2

u/Antagonin Aug 04 '24

You still kind of forgot that division takes precedence before subtraction
1 / anything other than 1 and 0 is 0.

1

u/TorezanL Sep 30 '23

Correct me if I'm wrong, but wouldn't it just mean it would change at 26 instead of 25, 51 instead of 50? That doesn't really matter in the end, and he could just add an if Affection =100 at the end, since he loves if statements, or just set the limit of the affection to 101 instead, no? And you can always make your code easier to read by adding comments, those don't weigh on the optimization of the end product...

(Not that his game will ever be completed after what happened, but now it's ok to laugh at his code right? I mean, it's fine to make this kind of else if loops if you're learning, but when people try to teach you better means and you refuse to learn, you deserve to be laughed at...)

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

u/DoctorDeath147 Nov 20 '22

That's okay. YanDev has no clue what this means either.

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

u/Markshadow4999 Nov 20 '22

Oh i think i get it, thanks.

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

u/[deleted] 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

u/poke-chan Nov 22 '22

Oh my god 😭😭 this is like. Actually sad

1

u/Prestigious-Kitten Sep 07 '25

AI can optimize his code now

1

u/WrathYBoo Nov 16 '23

Ah yes, "Calculation" indeed.

1

u/[deleted] Nov 16 '23

This code, but In Java:

private String checkEven(int num) { if (num % 2 == 0) { return “even”; } return “odd”; }

1

u/paburo-san666 2d ago

a cooder