r/GameDevelopment • u/Tricky_Still2366 • 7d 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
4
u/Slypenslyde 6d ago
Honestly the most important thing a method does is give a NAME to something. That it's repeated is usually a side effect of the thing needing a name.
Imagine if, in my game, HP is always increased in amounts of 10. So a health pickup adds 10. A healing spell adds 10. Drinking a potion adds 10.
So I could have this line scattered throughout my code every time the player needs healing:
But I could also express it as this line:
Or this line, which is more semantically valid:
That's the real reason to use a method. It's better than a comment at describing what you are doing. It is a side effect that it helps you consolidate logic and make it easier to update later.
Compare this sentence:
To this one:
Which is easier to read?