Advertisement

Basic Game questions from (learning C# by Programming Games) book

Started by January 05, 2015 08:22 AM
7 comments, last by Khaiy 10 years ago

Hi,

I am attempting to teach myself C# Game Basics from book (Learning C# by Programming Games)

In Chapter 5 we are introduced to a simple Game where the cursor is replaced by a picture of a balloon. This is about where Objects, Classes, Methods and Properties are explained. I am now looking at the code for this game and I am confused about a few things I was hoping someone could help me understand. So here is the code: (my question is inside // // )

using Microsoft.Xna.Framework;

using Microsoft.Xna.Framework.Graphics;

using Microsoft.Xna.Framework.Input;

class Balloon : Game

{

GraphicsDeviceManager graphics;

SpriteBatch spriteBatch;

Texture2D balloon, background;

Vector2 balloonPosition;

static void Main()

{

Balloon game = new Balloon();

game.Run();

}

public Balloon()

{

Content.RootDirectory = "Content";

graphics = new GraphicsDeviceManager(this);

}

//

1. Content is a property of Game which is essentially a location on the pc ? Then RootDirectory is a Method that specifies where all the assets are located? so Content.RootDirectory gives the property content a location.

Why can't you just write Content = "Content"

2. GraphicsDeviceManager is a class with a parameter the object we created called game ? we have created a new object in this line: graphics ?

3. what is difference between a parameter and a property of an object ?

//

protected override void LoadContent()

{

spriteBatch = new SpriteBatch(GraphicsDevice);

balloon = Content.Load<Texture2D>("spr_lives");

background = Content.Load<Texture2D>("spr_background");

}

protected override void Update(GameTime gameTime)

{

MouseState currentMouseState = Mouse.GetState();

balloonPosition = new Vector2(currentMouseState.X, currentMouseState.Y);

}

protected override void Draw(GameTime gameTime)

{

GraphicsDevice.Clear(Color.White);

spriteBatch.Begin();

spriteBatch.Draw(background, Vector2.Zero, Color.White);

spriteBatch.Draw(balloon, balloonPosition, Color.White);

spriteBatch.End();

}

}

1) RootDirectory is a Property, functions/methods are normally: Content.RootDirectory("Content") with a parameter. I would guess that Content is a variable/field of a class and RootDirectory is a property of the class.

2) Yes GraphicsDeviceManager is a class that's being instantied (creating an object from) to the variable/field called graphics. In this place we are calling the GraphicsDeviceManager's constructor.

3) A property is a member (or field) of the class e.g. "GraphicsDeviceManager graphics;" where as a parameter is passed in to functions/methods.
Generally properties within C# are public e.g.


//FIELD mUserID
private int mUserID
 
// PROPERTY USERID
public int USERID
{
get { return mUserID; }
set { mUserID = value; }
}
Advertisement

1) "content" is just a string, while Content property of game is a much more complex thing with many fields, one of which is a string pointing at the place in your hard drive where the content actually is

2) we created above with:

GraphicsDeviceManager graphics;

later on we assign graphics to the actual instance of the object

3)parementers are like "information", "data" we pass to the method or like in this case the constructor, which will use such things for their purpose

A property instead is usually a way to access a field of the actual object

a better example, lets assume there is a class Human

with the classic fields like height, haircolor etc etc

a typical constructor could be like Human joe =new Human(BasketPlayer);

here the constructor of the human class would assign each field a specific value for the kind of human we are gonna create, our parameter doesnt go directly into the object joe, but hepl create it

while doing:

joe.Height =2.15;

we would just access the field of height and change just that

In the future, try to use code blocks for questions/answers in code.


public Balloon()
{
    Content.RootDirectory = "Content";
    graphics = new GraphicsDeviceManager(this);
}


1. Content is a property of Game which is essentially a location on the pc ? Then RootDirectory is a Method that specifies where all the assets are located? so Content.RootDirectory gives the property content a location.
Why can't you just write Content = "Content"

Content is a property of the Game class and its type is ContentManager. Its purpose is to manage loading/accessing the assets such as sound, textures, models, shaders, etc. Whereas "Content" (notice the quotes) is a string. You can't write Content = "Content" because you'd be telling the compiler to assign a string type to a ContentManager type.


2. GraphicsDeviceManager is a class with a parameter the object we created called game ? we have created a new object in this line: graphics ?

Yes, you have created a new instance (instantiated) a new GraphicsDeviceManager class. You use it for specifying screen resolution, fullscreen mode, etc. I don't like how XNA examples name this property graphics when the Game class has a GraphicsDevice property. I wish they would have named it graphicsDeviceManager.


3. what is difference between a parameter and a property of an object ?

Parameters are variables passed into functions. Properties are public data members on objects.


public Balloon() // Your Balloon constructor, takes no PARAMETERS
{
    Content.RootDirectory = "Content"; // RootDirectory is a string PROPERTY
    graphics = new GraphicsDeviceManager(this); // graphics is a private MEMBER, and we pass (this) as a parameter to the GraphicsDeviceManager Constructor
}

...

protected override void Draw(GameTime gameTime)// gameTime is a GameTime PARAMETER for the Draw function.
{
    GraphicsDevice.Clear(Color.White); // The Clear function takes a color as a PARAMETER.
    spriteBatch.Begin(); // Begin is a function with no PARAMETER.
    spriteBatch.Draw(background, Vector2.Zero, Color.White);
    spriteBatch.Draw(balloon, balloonPosition, Color.White);
    spriteBatch.End();
}

EckTech Games - Games and Unity Assets I'm working on
Still Flying - My GameDev journal
The Shilwulf Dynasty - Campaign notes for my Rogue Trader RPG


Content is a property of the Game class and its type is ContentManager. Its purpose is to manage loading/accessing the assets such as sound, textures, models, shaders, etc. Whereas "Content" (notice the quotes) is a string. You can't write Content = "Content" because you'd be telling the compiler to assign a string type to a ContentManager type.

So is ContentManager a class within Object game ? im confused..

The Game class declares a variable of type ContentManager, named "Content", in the same way that the Balloon class in the code you posted declares a variable of type SpriteBatch named "spriteBatch". You are correct that a Game object will have a ContentManager object, and the name of the ContentManager object will be "Content". The ContentManager object has a property called "RootDirectory", which is type String.

The fully qualified variable would be: Balloon.Content.RootDirectory. This accesses the ContentManager object of Balloon, and the RootDirectory property of Content.

For future reference the word "class" is usually used to refer to a class definition and "object" is usually used to refer to an instance of a class. The code in "Game.cs" defines the what a Game object is, while something like "Game game1 = new Game()" is creating a specific Game object called "game1".

-------R.I.P.-------

Selective Quote

~Too Late - Too Soon~

Advertisement

ok so an object can have and manipulate a sub-object essentially right?

You should reallly read a book imo...

but yeah an object can have multiple fields which can be other objects on their own and so on

in a game scenario is pretty common, think of a character with a bag with a scroll of enchant etc etc

ok so an object can have and manipulate a sub-object essentially right?

That's broadly correct. The terminology is more along the lines of "an object can have a field which is another object", but you've got the idea.

-------R.I.P.-------

Selective Quote

~Too Late - Too Soon~

This topic is closed to new replies.

Advertisement