Hi all. I am relatively new to coding and game development, and i’m working on a little project that i’m letting grow small step by small step. It is a Tile-TurnBased chesslike game.
I am having some sort of “silly” problem with constructors and I really can’t get to decipher C# MSDN guides.
In particular i have two problems:
-
i have my Tile class which is:
public class Tile
{
public int dimensioneTile;public int xRel; public int yRel; public bool tileOccupata; public Tile(int _xRel, int _yRel, bool _occupata) { xRel = _xRel; yRel = _yRel; tileOccupata = _occupata; }
(tileOccupata is a “isOccupied” sort of bool)
it works actually, but since all of my tiles “are born” empty I would like to have somthing like:
public class Tile
{
public int dimensioneTile;
public int xRel;
public int yRel;
public bool tileOccupata;
public Tile(int _xRel, int _yRel)
{
xRel = _xRel;
yRel = _yRel;
tileOccupata = false;
}
}
Where the variable already stores the information of being “empty”, but if i code it like so it stores it as true, ignoring my line of code saying “tileOccupata = false”… this is not the behavior i was expecting.
My second problem is similar but slightly different
public class Eroe
{
public string nome;
public int Strength;
//i want health to be Strength * 2
public int Health;
//constructor
public Eroe(string _nome, int STR)
{
nome = _nome;
Strength = STR;
Health = Strength * 2;
//i also tried Health = STR * 2
}
}
in both cases Health turns out to be 0
So my actual question is:
do i need to have a parameter for each variable i have in my class for it to be actually initialized at a different value than 0 || true? How can I do it?
Thank you for your help