c# method question

Started by
5 comments, last by Gaius Baltar 9 years, 7 months ago

Hello there, I am new to this website and this is my first forum post here.

I'm using c# because the FAQ on this site recommended it for beginners though I already know html, css, a little hlsl and very little java. My problem is getting values linked to a class from one method into another.

I just started learning c# yesterday so this may seem like a silly problem.

Here is the code so far.

I want to get all the "player.xxxx" values from the characterCreation method to the runGame method.


namespace TextBasedRPGv0._2
{
    class Game
    {
            public class Character
            {
                public float maxHealth;
                public float currentHealth;
                public float maxMana;
                public float currentMana;
                public float Damage;
                public float Defence;

                public int level;
                public int strength;
                public int stamina;
                public int agility;
                public int speed;
                public int intelligence;
                public int wisdom;

            }

            public class Player: Character
            {
                public string userName;
                public string userProf;
            
                public string Warrior;
                public string Rogue;
                public string Mage;

                public float exp;
                public float expUntilNextLevel;
                public float expNextLevel;

            }

            public static void characterCreation()
            {
                int newPlayerLoop = 1;
                while (newPlayerLoop != 0)
                {
                    Player player;
                    player = new Player();

                    Console.WriteLine("Name your character.");
                    player.userName = Console.ReadLine();
                    Console.WriteLine("");
                    int chooseProf = 1;
                    Console.WriteLine("Choose your character's profession. (Warrior, Rogue or Mage)");

                    while (chooseProf != 0)
                    {
                        player.userProf = Console.ReadLine();

                        switch (player.userProf)
                        {
                            case "Warrior":

                                player.level = 1;
                                player.exp = 0;
                                player.strength = 7;
                                player.stamina = 6;
                                player.agility = 4;
                                player.speed = 4;
                                player.intelligence = 3;
                                player.wisdom = 3;

                                player.maxHealth = ((player.strength + player.stamina) * (player.level * 2)) + ((player.agility + player.speed + player.intelligence + player.wisdom) / (player.level * 2));
                                player.currentHealth = player.maxHealth;
                                player.maxMana = ((player.stamina + player.wisdom) * (player.level * 2)) + ((player.agility + player.speed + player.strength + player.intelligence) / (player.level * 2));
                                player.currentMana = player.maxMana;
                                player.Damage = ((player.strength * player.agility) - (player.agility / (player.level * 2) + player.speed)) / 2;
                                player.Defence = 0.05f;

                                Console.WriteLine("You are a level " + player.level + " Warrior with:");
                                chooseProf = 0;
                                break;
                            case "Rogue":

                                player.level = 1;
                                player.exp = 0;
                                player.strength = 3;
                                player.stamina = 5;
                                player.agility = 7;
                                player.speed = 6;
                                player.intelligence = 4;
                                player.wisdom = 2;

                                player.maxHealth = ((player.strength + player.speed) * (player.level * 2)) + ((player.agility + player.stamina + player.intelligence + player.wisdom) / (player.level * 2));
                                player.currentHealth = player.maxHealth;
                                player.maxMana = ((player.speed + player.wisdom) * (player.level * 2)) + ((player.agility + player.intelligence + player.strength + player.stamina) / (player.level * 2));
                                player.currentMana = player.maxMana;
                                player.Damage = ((player.strength * player.agility) + (player.agility / (player.level * 2) + player.speed)) / 2;
                                player.Defence = 0.04f;

                                Console.WriteLine("You are a level " + player.level + " Rogue with:");
                                chooseProf = 0;
                                break;
                            case "Mage":

                                player.level = 1;
                                player.exp = 0;
                                player.strength = 2;
                                player.stamina = 3;
                                player.agility = 4;
                                player.speed = 5;
                                player.intelligence = 6;
                                player.wisdom = 7;

                                player.maxHealth = ((player.strength + player.wisdom) * (player.level * 2)) + ((player.agility + player.speed + player.intelligence + player.stamina) / (player.level * 2));
                                player.currentHealth = player.maxHealth;
                                player.maxMana = ((player.intelligence + player.wisdom) * (player.level * 2)) + ((player.agility + player.speed + player.strength + player.stamina) / (player.level * 2));
                                player.currentMana = player.maxMana;
                                player.Damage = ((player.intelligence * player.agility) - (player.agility / (player.level * 2)) + player.wisdom) / 2;
                                player.Defence = 0.03f;

                                Console.WriteLine("You are a level " + player.level + " Mage with:");
                                chooseProf = 0;
                                break;
                        }
                    }

                    Console.WriteLine(player.strength + " strength");
                    Console.WriteLine(player.stamina + " stamina");
                    Console.WriteLine(player.agility + " agility");
                    Console.WriteLine(player.speed + " speed");
                    Console.WriteLine(player.intelligence + " intelligence");
                    Console.WriteLine(player.wisdom + " wisdom");
                    Console.WriteLine("Your max health is " + player.maxHealth + ".");
                    Console.WriteLine("Your max mana is " + player.maxMana + ".");
                    Console.WriteLine("Your damage is " + player.Damage + ".");
                    Console.WriteLine("(press enter to continue)");
                    Console.ReadLine();
                    Console.Clear();

                    newPlayerLoop = 0;
                }

            }
            
            public static void runGame()
            {            
                Console.WriteLine(player.userName);
                  
            }

        public static void Main(string[] args)
        {          
            characterCreation();
            
            runGame();
           
            Console.ReadLine();
        }

        
    }
}

Like I said, I'm new to this so the answer is probably very obvious to someone with c# experience.

Thanks in advance.

Advertisement

Disregard this post

A global? Seriously? Don't suggest things that will worsen his code quality.


Return the player from the character creation function and then pass it to the runGame function:


namespace TextBasedRPGv0._2
{
    class Game
    {
            public class Character
            {
                public float maxHealth;
                public float currentHealth;
                public float maxMana;
                public float currentMana;
                public float Damage;
                public float Defence;

                public int level;
                public int strength;
                public int stamina;
                public int agility;
                public int speed;
                public int intelligence;
                public int wisdom;

            }

            public class Player: Character
            {
                public string userName;
                public string userProf;
            
                public string Warrior;
                public string Rogue;
                public string Mage;

                public float exp;
                public float expUntilNextLevel;
                public float expNextLevel;

            }

            public static Player characterCreation()
            {
                while (true)
                {
                    Player player;
                    player = new Player();

                    Console.WriteLine("Name your character.");
                    player.userName = Console.ReadLine();
                    Console.WriteLine("");
                    int chooseProf = 1;
                    Console.WriteLine("Choose your character's profession. (Warrior, Rogue or Mage)");

                    while (chooseProf != 0)
                    {
                        player.userProf = Console.ReadLine();

                        switch (player.userProf)
                        {
                            case "Warrior":

                                player.level = 1;
                                player.exp = 0;
                                player.strength = 7;
                                player.stamina = 6;
                                player.agility = 4;
                                player.speed = 4;
                                player.intelligence = 3;
                                player.wisdom = 3;

                                player.maxHealth = ((player.strength + player.stamina) * (player.level * 2)) + ((player.agility + player.speed + player.intelligence + player.wisdom) / (player.level * 2));
                                player.currentHealth = player.maxHealth;
                                player.maxMana = ((player.stamina + player.wisdom) * (player.level * 2)) + ((player.agility + player.speed + player.strength + player.intelligence) / (player.level * 2));
                                player.currentMana = player.maxMana;
                                player.Damage = ((player.strength * player.agility) - (player.agility / (player.level * 2) + player.speed)) / 2;
                                player.Defence = 0.05f;

                                Console.WriteLine("You are a level " + player.level + " Warrior with:");
                                chooseProf = 0;
                                break;
                            case "Rogue":

                                player.level = 1;
                                player.exp = 0;
                                player.strength = 3;
                                player.stamina = 5;
                                player.agility = 7;
                                player.speed = 6;
                                player.intelligence = 4;
                                player.wisdom = 2;

                                player.maxHealth = ((player.strength + player.speed) * (player.level * 2)) + ((player.agility + player.stamina + player.intelligence + player.wisdom) / (player.level * 2));
                                player.currentHealth = player.maxHealth;
                                player.maxMana = ((player.speed + player.wisdom) * (player.level * 2)) + ((player.agility + player.intelligence + player.strength + player.stamina) / (player.level * 2));
                                player.currentMana = player.maxMana;
                                player.Damage = ((player.strength * player.agility) + (player.agility / (player.level * 2) + player.speed)) / 2;
                                player.Defence = 0.04f;

                                Console.WriteLine("You are a level " + player.level + " Rogue with:");
                                chooseProf = 0;
                                break;
                            case "Mage":

                                player.level = 1;
                                player.exp = 0;
                                player.strength = 2;
                                player.stamina = 3;
                                player.agility = 4;
                                player.speed = 5;
                                player.intelligence = 6;
                                player.wisdom = 7;

                                player.maxHealth = ((player.strength + player.wisdom) * (player.level * 2)) + ((player.agility + player.speed + player.intelligence + player.stamina) / (player.level * 2));
                                player.currentHealth = player.maxHealth;
                                player.maxMana = ((player.intelligence + player.wisdom) * (player.level * 2)) + ((player.agility + player.speed + player.strength + player.stamina) / (player.level * 2));
                                player.currentMana = player.maxMana;
                                player.Damage = ((player.intelligence * player.agility) - (player.agility / (player.level * 2)) + player.wisdom) / 2;
                                player.Defence = 0.03f;

                                Console.WriteLine("You are a level " + player.level + " Mage with:");
                                chooseProf = 0;
                                break;
                        }
                    }

                    Console.WriteLine(player.strength + " strength");
                    Console.WriteLine(player.stamina + " stamina");
                    Console.WriteLine(player.agility + " agility");
                    Console.WriteLine(player.speed + " speed");
                    Console.WriteLine(player.intelligence + " intelligence");
                    Console.WriteLine(player.wisdom + " wisdom");
                    Console.WriteLine("Your max health is " + player.maxHealth + ".");
                    Console.WriteLine("Your max mana is " + player.maxMana + ".");
                    Console.WriteLine("Your damage is " + player.Damage + ".");
                    Console.WriteLine("(press enter to continue)");
                    Console.ReadLine();
                    Console.Clear();

                    return player;
                }
            }
            
            public static void runGame(Player player)
            {            
                Console.WriteLine(player.userName);
                  
            }

        public static void Main(string[] args)
        {          
            Player player = characterCreation();
            
            runGame(player);
           
            Console.ReadLine();
        }

        
    }
}

Disregard this post.

A global? Seriously? Don't suggest things that will worsen his code quality.


I don't see you problem, this is implemented for standard class bound communication.


Just because his code is starting out as static methods doesn't mean he should get used to writing his entire program that way.

It's almost always better to use parameters and local variables instead of globals, especially in cases like this where it's embarrassingly easy to use a return value and a parameter.

EVER suggesting globals when writing code the proper way is this easy is a crime against humanity.

Wow, thanks for the quick replies. Both of the suggestions above work and I will keep both in mind for the future.

Wow, thanks for the quick replies. Both of the suggestions above work and I will keep both in mind for the future.

It appears, I was wrong, go with Nypyren's suggestion, in this case it would be the better solution overall.

This topic is closed to new replies.

Advertisement