Simple vs Complicated code

Hi there. I’m enjoying this tutorial a lot. Occasionally though it seems to me that a simple code solution gets replaced by a code solution that’s unnecessarily complicated. Yes, I know, the complicated code teaches a more sophisticated and perhaps flexible coding approach and introduces additional concepts that should be useful in the future. But, right now, I’m at a basic coding level and will not remember the complicated solution but will remember the simple one.
Case in point below.

   SceneManager.LoadScene(0);

versus

    int currentSceneIndex = SceneManager.GetActiveScene().buildIndex;
    SceneManager.LoadScene(currentSceneIndex);

I understand how the second solution works. But, again, I won’t remember it. It’s a bit demoralizing.
Just saying.

Jim

I can give my 2 cents here.

I’ve been playing with unity for the last year and a half, done the 2d, 3d and 4 RPG courses for gamedev tv. I’ve also had a few other courses, and lots of youtube videos on “one offs”

The thing that the game dev tv does is sometimes “over engineer” the solution. So that it gives you the ability to expand.

The first line of code WORKS, but if you tried to use it in the next scene, it would just send you back to the first, or change it with another hardcoded value.

Sometimes I get to the end of a section, or into the next course, I go back and try to figure out “what EXACTLY does that line do” so i can modify it. Usually it makes more more sense the second time around.

Compared to other projects where I can’t add functionality, they usually just doesn’t hold up as it gets more complex. But generally Game Dev stuff does(if you look at the enormous library of comments that Brian has added onto the RPG course)

The other part is being EXPOSED to more ways to do things, different problem solving.

I would try not to let it get you down, if your confused, it just means your learning. Try to make your way through the course, then review parts that were confusing again.

My prime example was the 3d tower defense section. by the end i had no idea how the dictionaries worked, or how the pathfinding was calculated. I felt like i got very little out of it. But after doing core combat, and seeing those same principles again, things started to click. (Note, i still think it was cruel and unusual torture to give us the double dictionary right off the bat),

Now i look back and wonder why i didn’t get it.

Hang in there, ask tons of questions and enjoy the journey :slight_smile:

5 Likes

Something to think about. You’re not going to remember everything. No one does. Well, at least not the normal people :wink: What’s important is to learn the general gist of things and also learn how to google the solution you need.

You’ll pick up a lot of familiarity with each bit of code with practice and experimentation. You’re not supposed to remember every method.

Last but not least, let the editor/autocomplete do a lot of the heavy lifting for you.

That’s one way to do it. My code is this line and it’s easier to read.

SceneManager.LoadScene(sceneBuildIndex: 0);

I think the difference is reusability.
The code with the hard coded 0 can only be used for the first scene. when you create another level, you have to get that level index and reload the scene, otherwise it will all go to the first level.

1 Like

Maybe this helps someone, this one is the same and a lot easier:

SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex);

It’s in the course tho
Then you just add 1 and that’s the next level, I think it’s easier:

SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex + 1);
1 Like

The first solution only works for one level, the second solution works for any level without requiring you to manually hard code it, which can lead to human errors that are more difficult to debug (since it won’t give you an error code, it will just behave in an unexpected way).

The goal is to think in terms that free you from hard coding values that you’ll have to manually change in multiple places if you ever want to change or extend your program.

Privacy & Terms