While this logic is unbelievably fucked, if this is specifically what they want to do, is there a better way to do this? Enumerating all the possible options doesn't feel especially elegant either, even if it would be somewhat easier to read.
Then again, I guess easier to read is what matters most.
int random = rng.nextInt(3);
switch (random) {
case 0:
components.add(Grasscliff(...));
break;
case 1:
components.add(MediumStoneWtf(...));
//components.add(MediumStoneWtf(...)); // The code is badder than I first thought; this should only be called once apparently
break;
case 2:
components.add(MediumStone(...));
//components.add(MediumStone(...));
//components.add(MediumStone(...)); // EDIT: the last case breaks the for loop. This is *really* bad code, and I don't deserve any of my upvotes
break;
}
Actually it's even simpler than that, the original code only executes one of the statements once (you see the if i==0 and the break), so you don't even need the repetitions.
I realized after commenting but not faster than you :P and yeah, that's something obfuscated by this terrible way of doing it. So yeah, its clear that if you want job security for loops are the way to go
It looks like the first block, where random == 0 will execute three times for each value of i. Then again, I could be losing my mind from reading this atrocity. EDIT: I am wrong.
if you wanted a single random element called from a list, you'd have a list, array, dict - whatever the collection of possible elements was... and call a random element from it.
How am I possibly supposed to get 1 Grasscliff, 1 MediumStoneWtf, or 3 MediumStones with this method? Also this isn't really easier to write, and is harder on the garbage collector if your language has one, and I'm not sure I would trust the compiler to optimize it if it doesn't either, since you would be creating each object before you add any of them to the list. You also wouldn't be able to vary the argument in the constructor as this code does for the MediumStones
So I probably wouldn't do it this way, though that would be possible for some tasks.
EDIT: I'm an idiot and thought that the break in OP's code was in a switch statement. That one is only getting run once. I still don't think I would do this, but it is a valid solution. Not sure why I got so many upvotes without seeing that derp
I was presuming that the grasscliff and mediumstone objects were scenery thats cached and just displayed on call.
Like you'd load in all the elements you're going to use in the scene and then display them?
If you really needed to create, render and destroy the elements on the fly, wouldn't those be part of the elements' class, though? So you'd have grasscliff.render() grasscliff.destroy() and grasscliff is a
New SceneryElement
?
Or if not you'd have a separate thread that's checking for elements that are out of view and removing it- but then the Scenery would change and you turned away and looked back
Again, I've never made a video game - just guessing from how I'd expect it to work.
Game dev involves a lot of, "I really need this to just work, right now. I'll fix it later." Except you never get a chance to fix it.
I can honestly see this happening through a series of changes to what they were trying to do, until it's gotten so far from the original purpose that it should be entirely rewritten.
Supervision is nice but a single person can get by as long as they know what they're doing. This is just made by a clueless person. I refuse to believe experienced programmers would ever produce this.
This code does look pretty bad but procedural generation is nothing but trial and error. Even if the loop is pointless now, it probably wasn't always the case. It looks like it was set up to place up to 4 random objects. It could easily be that StoneTwo was initially two runs of the loop spawning MediumStone for example before being replaced by a single StoneTwo, and that up to 4 MediumStones could be placed before and that was disabled with the break for tuning reasons. It's also possible that they may have considered reverting changes and so left the loop code intact so it could be easily reverted, it doesn't technically make it perform poorly, it's just hard to decipher. A comment or two of reasoning would be enough to make this ok in my mind. (ie: this loop is here so I can easily add more rocks by commenting out the break)
When it's 0: for loop will loop 1 (1+0) times. Code in the first if gets executed once and that's it.
When it's 1: for loop will loop 2 (1+1) times. Code in the second if gets executed twice. However, the if checking for i == 0 makes sure the code within only gets executed on the first pass.
When it's 2: for loop will loop 3 (1+2) times. Code in the else statement gets executed once and then breaks on the first pass.
Not a single line actually needs to be repeated, this for-loop is entirely useless. At first the contents of the second and third if statement seem the same but they actually call different functions, on calls MediumStoneTwo(...) and the other calls MediumStone(...)
So, the most easy fix is this:
int random = rng.nextInt(3);
if (random == 0) {
components.add(Grasscliff(gameRef, Vector2(j + (i * 20), baseHeight + i), 60));
} else if (random == 1) {
components.add(MediumStoneTwo(gameRef, Vector2(j + (i * 30), baseHeight + i), Vector2(50, 25)));
} else { // (random == 2)
components.add(MediumStone(gameRef, Vector2(j + (i * 30), baseHeight + i), Vector2(50, 25)));
}
Could be made into a switch. Also, all lines call components.add(...), so you could extract a function that returns the different arguments to pass that to components.add() once.
Component getComponent(int componentNr) { // gonna assume type is Component, since we're adding to components
switch (componentNr) {
case 0: return Grasscliff(gameRef, Vector2(j + (i * 20), baseHeight + i), 60);
case 1: return MediumStoneTwo(gameRef, Vector2(j + (i * 30), baseHeight + i), Vector2(50, 25));
case 2: return MediumStone(gameRef, Vector2(j + (i * 30), baseHeight + i), Vector2(50, 25));
default: return null; // or throw an exception idfk
}
}
components.add(getComponent(rng.nextInt(3)));
Edit: Also, as u/TinyBreadBigMouth pointed out, the functions do some multiplication and addition with the i variable within the for loop, but since every line only runs on the first iteration (i = 0) those can be completely left out. That should leave you with the following:
Component getComponent(int componentNr) { // gonna assume type is Component, since we're adding to components
switch (componentNr) {
case 0: return Grasscliff(gameRef, Vector2(j, baseHeight), 60);
case 1: return MediumStoneTwo(gameRef, Vector2(j, baseHeight), Vector2(50, 25));
case 2: return MediumStone(gameRef, Vector2(j, baseHeight), Vector2(50, 25));
default: return null; // or throw an exception idfk
}
}
components.add(getComponent(rng.nextInt(3)));
I don't know what the j variable is though, if it's not global/static then that might have to actually be a parameter of the function we extracted also.
Also note that the i in there is the loop counter, and since code is only ever run on the first iteration, Vector2(j + (i * 30), baseHeight + i) can be simplified to Vector2(j, baseHeight).
I'm really missing a try catch except inside the loop so that even on exception we force this shitcode to be executed a few more times in hope that it finally works. Also, the random value seems too small.
Had a teacher in uni that did something similar, once. She was a wonderful person, but good god damn. We were doing a Z-grade Zork knock-off as a class project and our save and level files had to be a specific plaintext format; no XML or any of Java's serialization bits. In the loading loop...she used exceptions for control flow. As in, each section of a map would get loaded in, line-by-line, until it reached a character sequence (I think it was "----")...where it threw an exception so it could go back to the main level loading method. It was bonkers.
It's very bad code. The loop is hiding the logic and is completely unnecessary; in some cases the code will run through the loop multiple times while doing nothing. The whole thing is equivalent to a single if-elseif-else block:
Vector2 pos = Vector2(j, baseHeight);
int random = rng.nextInt(3);
if (random == 0) {
components.add(Grasscliff(gameRef, pos, 60));
} else if (random == 1) {
components.add(MediumStoneTwo(gameRef, pos, Vector2(50, 25)));
} else {
components.add(MediumStone(gameRef, pos, Vector2(50, 25)));
}
Note that the Vector2(j + (i * 30), baseHeight + i) being used all over the place simplifies to Vector2(j, baseHeight), since components will only be added when i is 0.
44
u/Magnus_Tesshu Aug 10 '21 edited Aug 10 '21
While this logic is unbelievably fucked, if this is specifically what they want to do, is there a better way to do this? Enumerating all the possible options doesn't feel especially elegant either, even if it would be somewhat easier to read.
Then again, I guess easier to read is what matters most.
96
u/Leif_Erickson23 Aug 10 '21
Is it just me or are game codebases exceptionally shitty
35
u/MurdoMaclachlan public boolean isInt(int i) { return true; } Aug 10 '21
Image Transcription: Code
I'm a human volunteer content transcriber for Reddit and you could be too! If you'd like more information on what we do and why we do it, click here!
6
u/Tall-Wrongdoer3997 Aug 11 '21
This deserves the medal “made at 2am”
4
u/rpmerf Aug 10 '21
But what is J? Don't tell me it's another random sized loop...
3
u/TheDoddler Aug 10 '21
This code does look pretty bad but procedural generation is nothing but trial and error. Even if the loop is pointless now, it probably wasn't always the case. It looks like it was set up to place up to 4 random objects. It could easily be that StoneTwo was initially two runs of the loop spawning MediumStone for example before being replaced by a single StoneTwo, and that up to 4 MediumStones could be placed before and that was disabled with the break for tuning reasons. It's also possible that they may have considered reverting changes and so left the loop code intact so it could be easily reverted, it doesn't technically make it perform poorly, it's just hard to decipher. A comment or two of reasoning would be enough to make this ok in my mind. (ie: this loop is here so I can easily add more rocks by commenting out the break)
4
u/SanianCreations Aug 10 '21 edited Aug 11 '21
Let's unpack this:
nextInt(3)can either be 0, 1 or 2.When it's 0: for loop will loop 1 (1+0) times. Code in the first if gets executed once and that's it.
When it's 1: for loop will loop 2 (1+1) times. Code in the second if gets executed twice. However, the if checking for
i == 0makes sure the code within only gets executed on the first pass.When it's 2: for loop will loop 3 (1+2) times. Code in the else statement gets executed once and then breaks on the first pass.
Not a single line actually needs to be repeated, this for-loop is entirely useless. At first the contents of the second and third if statement seem the same but they actually call different functions, on calls
MediumStoneTwo(...)and the other callsMediumStone(...)So, the most easy fix is this:
Could be made into a switch. Also, all lines call
components.add(...), so you could extract a function that returns the different arguments to pass that tocomponents.add()once.Edit: Also, as u/TinyBreadBigMouth pointed out, the functions do some multiplication and addition with the i variable within the for loop, but since every line only runs on the first iteration (i = 0) those can be completely left out. That should leave you with the following:
I don't know what the j variable is though, if it's not global/static then that might have to actually be a parameter of the function we extracted also.
2
u/stahkh Aug 10 '21
I'm really missing a try
catchexcept inside the loop so that even on exception we force this shitcode to be executed a few more times in hope that it finally works. Also, the random value seems too small.4
u/AlwaysNinjaBusiness Aug 10 '21
It's trippy code, but I don't know if it's bad code.
1
u/StochasticTinkr Aug 10 '21
Let's see if I understand what happens:
If random is 0, add a single `Grasscliff`. If random is 1, add a single MediumStoneTwo. If random is 2, add MediumStone three times.
1
u/LucasCarioca Aug 11 '21
Definitely something they thought was clever until they had to show or explain it.
1
u/Not_Sugden Aug 11 '21 edited Aug 11 '21
so this loop only iterates once
* well it iterates 2 times if random is 1 but it does nothing after the last iteration
why the fuck is he using a for loop lmfao
on every case:
(i * x),x + iwhere i is always 0, great job19
u/[deleted] Aug 10 '21
[removed] — view removed comment