+ 2
How to use objects?
I am really confused on how using objects can help you in making video games. I understand that they are very important but I just can't get it through my head why they're important. I would appreciate some examples đ thank you.
2 Answers
+ 2
For explain to your question, you need to understand first why code reuse is important in medium/large applications, and can be easly paralelized across multiple developers. If your work is linear without taking into account that, you could end in a very hard to debug application.
It is easier to have a model based thinking, it's natural, before starting a project, how your work will be planified over time, or you will be stuck in some points in your development lifecycle and unforunately project will stop somewhere without a final solution and scope.
Using object oriented (OOP) approach, you can plan and model a scene and future interaction inside this scene, for which their functionality will be easly extensible in future.
A class is a self explanatory definition for a model which contains attributes, methods and modifiers. In the other hand object is the instance of this defined model (class) when you will need to use this by model constraints which you have defined.
+ 2
Lets consider a Player model. You can start rising yourself questions, to create a detailed model.
What attributes a player can have? Name? Identifier? Relations with another Player? And so on...
What actions and interactions can this player have? You can describe as methods/operations.
Can this player move()? Can this player start a party()? Can this player die()? And so on...
Finally you will end with a player model description (class description).
What if you want to deliver this created model to others for reuse and make some constraints regarding how other models spawned from it will manifest?
Here you can think at interfaces. Making this Player to implement some interfaces. An interface (abstract class) will only contain actions/methods without have a concrete implementation on should player move and this is leaved for the pne which will use. Player can move depending on the context, an inch or a kilometer or one unit step. When interface implementation will be used you will be obligate to provide a implementation, you have ended with constraints applied to the original model.
Example:
interface IAction
{
void move();
void party();
void die();
}
class Player implements IAction
{
int identifier;
String name;
int xPosition;
If methods from interfaces are not added here and also implementing these you will fail to create Player object.
void move () { xPosition++ }
...
...
}
We instantiate our model
Player player = new Player ();
...
...
code
...
...
On some another actions events we want to move the player.
player.move ()
This is a very breaf explanation this can be extended in hundreads hours of explanarions if not more.
Check youtube channel CodeTrain, will make you to understand OOP with illustrative examples wirh real uses. (Sound spectrum analysers, Realtime image analysis, Algorithms visualizarion and so on.
Have a great day.