passing a value from one class to another

Started by
8 comments, last by laztrezort 11 years, 9 months ago
Hello

I have 3 classes:-
1-Tile Class
2-Map Class which is inheriting from Tile Class
3-Enemy Class

Now I want to print out the variable DesertTileRec.X in Enemy class. How can I do that? when I try to print it my game crash and tells me "Object reference not set to an instance of an object". this is because I didn't call all the methods in the map class in my enemy class.

I don't want to call the methods of Map class in Enemy Class because I already did that in my Main class. I just want to print that variable without calling Map class again in my Enemy Class.

If I print DesertTileRec.X in my main class it works becuase I have called all the Map class methods.

Is there a way to pass that varible from Main class to Enemy Class?


public class Tiles
{
protected Vector2 TilePosition;
public Rectangle DesertTileRec;

public void CreateRectangleAroundTile()
{
DesertTileRec.X = (int)TilePosition.X;
DesertTileRec.Y = (int)TilePosition.Y;
DesertTileRec.Width = (int)DesertTile.Width;
DesertTileRec.Height = (int)DesertTile.Height;
}
}



class Map : Tiles
{
protected int MapX = 26, MapY = 19; // MapX =
public void Draw(SpriteBatch spriteBatch)
{
for (int x = 0; x < MapX; x++)
{
for (int y = 0; y < MapY; y++)
{
TilePosition.X = (float)x * TileX;
TilePosition.Y = (float)y * TileY;
}
}
CreateRectangleAroundTile();
}
}



class Enemy
{
Map MMap = new Map();
void Draw
{
//If I don't call Map.Draw method my game crash. I don't want to call the Map.Draw method again becuase I have alread called it in my Main class
MMap.CreateRectangleAroundTile();
spriteBatch.DrawString(Text, "X " + MMap.DesertTileRec.X + "\n" + "Y: " + MMap.DesertTileRec.Y + "\n" + "Width: " + MMap.DesertTileRec.Width + "\n" + "Height: " + MMap.DesertTileRec.Height, new Vector2(250, 100), Color.White);
}
}
Advertisement
hi,
i think if u use a static varible,your problem will be solved.

???? ?? ??? ????

Persian Gulf

Typically you pass the already constructed Map object in the Enemy constructor and store the reference in a private field.

On a side note, I'm not sure why Map class inherits from Tile - this looks like possibly a better fit for composition rather than inhertiance. Inheritance is often described as a "Is A" relationship, whereas composition is described as "Has A". In my mind, "A Map is a Tile" makes less sense than "A Map has Tiles".

Also, when designing a class, think about what it should "know" or express as data. Does each tile need to know it's position in the map? They might, but if we change around the relationship between maps and tiles as above, they may not:

(pseudo code)

class Tile
{
public int Width{get;}
public int Height{get;}
...
}

class Map
{
private Tile[,] tiles = new Tile[width, height];
...
public Rectangle GetTileRectangle(int posX, int posY)
{
Tile currentTile = tiles[posX, posY];

return new Rectangle(posX, posY, posX + currentTile.Width, posY + currentTile.Height);
}
}

class Enemy
{
private Map _map;

public Enemy(Map map)
{
_map = map;
...
}
...
}


I'm not sure if this is what you are intending (since I don't know the overall design of your game), but the above makes more immediate sense, IMO.

Typically you pass the already constructed Map object in the Enemy constructor and store the reference in a private field.

On a side note, I'm not sure why Map class inherits from Tile - this looks like possibly a better fit for composition rather than inhertiance. Inheritance is often described as a "Is A" relationship, whereas composition is described as "Has A". In my mind, "A Map is a Tile" makes less sense than "A Map has Tiles".

Also, when designing a class, think about what it should "know" or express as data. Does each tile need to know it's position in the map? They might, but if we change around the relationship between maps and tiles as above, they may not:

(pseudo code)

class Tile
{
public int Width{get;}
public int Height{get;}
...
}

class Map
{
private Tile[,] tiles = new Tile[width, height];
...
public Rectangle GetTileRectangle(int posX, int posY)
{
Tile currentTile = tiles[posX, posY];

return new Rectangle(posX, posY, posX + currentTile.Width, posY + currentTile.Height);
}
}

class Enemy
{
private Map _map;

public Enemy(Map map)
{
_map = map;
...
}
...
}


I'm not sure if this is what you are intending (since I don't know the overall design of your game), but the above makes more immediate sense, IMO.


Hmm, thank you very much.
Ok can you please give me an example where I should be using inhertance?
My professor explained it to me like this.
A car is a class, and it has many parts. A car can inherit from class wheel and class engine and class light etc...

is that right? guessing from what you just told me, then thats wrong.

thank you again

Ok can you please give me an example where I should be using inhertance?
My professor explained it to me like this.
A car is a class, and it has many parts. A car can inherit from class wheel and class engine and class light etc...

is that right? guessing from what you just told me, then thats wrong.


Far be it from to argue with a professor, but IMO I do not think this is the intention of inheritance. Using analogies to describe inheritance is always on shaky ground, but to use your example I would say:

A car IS A vehicle.
A bicycle IS A vehicle.
A vehicle HAS one or more wheels.
A car HAS an engine.

This would set up the hypothetical relationship as follows:

class Vehicle
{
Wheel[] wheels;
}

class Bicycle : Vehicle
{}

class Car : Vehicle
{
Engine engine;
}


I have a feeling that perhaps the professor was misunderstood, or that I am not getting the complete story, or perhaps he/she is simplifying things for now to clarify later. You could always ask your professor for clarification on the matter.

You can also read up the subject, searching for "inheritance vs. composition" will net many hits, for example this one: http://www.codeproje...-VS-Inheritance
thanks. that was really helpful.

just one last question.


void update()
{
//if enemy collide
if (EnemyRec.Intersects(DesertTileRec))
{
Alive = false;
}

if (Alive == false)
{
Player.EnemyKilled += 1;
}
}


if the enemy collide then the enemy should die which means Alive will be false. Now I want to keep track of how many enemies did the player kill. so I did if enemy is not alive then add one to variable EnemyKilled.

the problem is that after the enemy die, EnemyKilled variable keep going up. it should be equal to 1 when enemy die, but it goes 1, 2, 3, 10000 etc ...

how can i just make if the enemy die just add one and stop? The problem is with the loop.

the problem is that after the enemy die, EnemyKilled variable keep going up. it should be equal to 1 when enemy die, but it goes 1, 2, 3, 10000 etc ...


I have a feeling the answer is hidden right there. What is happening to make the killed counter keep increasing? When shouldn't the counter increase? Is there something else that can be checked, in addition to the collision itself, before increasing the counter?

This is what I think you are looking to do (spoiler tags in case you want to try and work through it yourself first):
[spoiler]
if (EnemyRec.Intersects(DesertTileRec) && Alive == true)
{
Alive = false;
Player.EnemyKilled += 1;
}
[/spoiler]

[quote name='FantasyVII' timestamp='1342639870' post='4960615']
the problem is that after the enemy die, EnemyKilled variable keep going up. it should be equal to 1 when enemy die, but it goes 1, 2, 3, 10000 etc ...


I have a feeling the answer is hidden right there. What is happening to make the killed counter keep increasing? When shouldn't the counter increase? Is there something else that can be checked, in addition to the collision itself, before increasing the counter?

This is what I think you are looking to do (spoiler tags in case you want to try and work through it yourself first):
[spoiler]
if (EnemyRec.Intersects(DesertTileRec) && Alive == true)
{
Alive = false;
Player.EnemyKilled += 1;
}
[/spoiler]
[/quote]

thanks, I figured it out eventually. I set a new boolean and check if its true or not and then I set it to false.

I looked at your solution and it look much much better than mine.

looks like my brain stopped working. its 5AM. I need to go to sleep. :D

thanks, you have been very very very helpful.

Hello

I have 3 classes:-
1-Tile Class
2-Map Class which is inheriting from Tile Class
3-Enemy Class

Now I want to print out the variable DesertTileRec.X in Enemy class. How can I do that? when I try to print it my game crash and tells me "Object reference not set to an instance of an object". this is because I didn't call all the methods in the map class in my enemy class.

I don't want to call the methods of Map class in Enemy Class because I already did that in my Main class. I just want to print that variable without calling Map class again in my Enemy Class.

If I print DesertTileRec.X in my main class it works becuase I have called all the Map class methods.

Is there a way to pass that varible from Main class to Enemy Class?



Why not declare the function that will use the member variable as a friend of that class or you could just have the enemy class as a friend of the main class?

Why not declare the function that will use the member variable as a friend of that class or you could just have the enemy class as a friend of the main class?


From his posted code, I am assuming he is using C#, and C# does not have the concept of C++ "friend"

Even if it is C++, I don't think friend is applicable here.

This topic is closed to new replies.

Advertisement