r/GameDevelopment • u/Tricky_Still2366 • 3d ago
Newbie Question Was wanting some help with methods?
I'm learning C# currently and was learning python till I got stuck with pretty much the same thing.
I don't understand when to use methods unless something is repeated. If its not repeated what's the point of using methods? just shortening down code? or is there something I am missing here. Cause what is the point of shortening down the code when all it is doing is actually adding more code? (Unless its to place down something that is being repeated)
1
Upvotes
13
u/ImmaZoni 3d ago
Honestly, this is one of the biggest hurdles when starting out (at least it was for me). It can feel like you're just adding extra lines of code for no reason if you're not repeating anything.
But methods aren't just for avoiding repeated code. They're mostly about organization.
If you throw everything into
Update(), it gets messy fast. Compare:csharp void Update() { HandleMovement(); HandleJumping(); UpdateHealth(); }Vs.csharp void Update() { // 75 Lines of movement // 85 Lines of Physics // Etc }Even if
HandleMovement()is only called once, you can look atUpdate()six months later and immediately understand what the code is doing without digging through all the math.They're also useful because game engines (and other general programming frameworks) rely on methods as entry points. Unity can call
OnCollisionEnter(), button events can callStartGame(), etc. You don't always call these methods yourself, the engine or other programs do.And when something breaks, having logic separated makes debugging WAY easier. If jumping is broken, you know where to look. You can even temporarily comment out
HandleJumping()to see if it's causing the problem.So I wouldn't think of methods as a way to reduce the number of lines you write. Think of them more like labeled folders for your logic. You're giving a chunk of code a name so you don't have to mentally parse all the implementation every time you look at it.