Hey guys! I have a quick question that I think is a simple mess up. I am trying to make a console word game in c#, and I have different classes, and a main class that calls those classes. I first have the Player class, and the CreateCharacter class. My player class houses all of the variables that pertain to the player of the class (level, stats etc.), and my CreateCharacter class is what calls on the startup of the game, which asks the name, race, class they want the character to be. So far that is well and good, but when I call the classes I know no other way than to say: Player player = new Player(); and I believe that takes the data I got from CreateCharacter, and erases it. So my actual engine calls CreateCharacter, gets all the information, then when I call all of the variables in the engine, they come back erased. Any help is appreciated! Thanks!

Could you sendin some code or some drawing of your classes structure.
It would make it much clearer.
O, and inheriting from multiple classes is not possible in C#.

In terms of architecture, a common solution is to have a master Game class that holds references to all the instances of all the components that make up the current state of the game, eg a list of Players etc. The Game class instance is shared by passing it to every other class's constructors. Thus every class has a reference to the Game, and can call its methods to get whetever info they need, eg getPlayers(), getCurrentTurnPlayer() etc

Could you sendin some code or some drawing of your classes structure. It would make it much clearer. O, and inheriting from multiple classes is not possible in C#.

I have put together a small sample that explains my problem and what I am trying to do.

In terms of architecture, a common solution is to have a master Game class that holds references to all the instances of all the components that make up the current state of the game, eg a list of Players etc. The Game class instance is shared by passing it to every other class's constructors. Thus every class has a reference to the Game, and can call its methods to get whetever info they need, eg getPlayers(), getCurrentTurnPlayer() etc

That makes sense, but when I call a new instance of my class in my engine, I do not know how to pass it on and have the other classes call the instance in the engine :(

I have been working on this on and off in c# console and c# windows application and this seems to be my only brick wall in actually learning more and creating things in this language.

Here is my sample problem, I have the main Engine, I have TestPlayer which holds the variables, then I have CreateCharacter. The Engine calls CreateCharacter, which gets all of the information from the user, then displays the info. Then it goes back to the main constructor of the engine, and it tries to display the info again, and it is blank.

Engine:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Test
{
    public class Engine
    {
        public static void Main(string[] args)
        {
            CreateCharacter cc = new CreateCharacter();
            TestPlayer player = new TestPlayer();

            cc.createCharacter();
            Console.ReadKey();
            Console.Clear();
            Console.WriteLine("The player name is " + player.name + " and their gender is " + player.gender);
            Console.ReadKey();

        }
    }
}

CreateCharacter:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Test
{
    public class CreateCharacter
    {
        public void createCharacter()
        {
            TestPlayer player = new TestPlayer();

            Console.WriteLine("What is your name?");
            player.name = Console.ReadLine();
            Console.WriteLine("\nWhat is your gender?");
            player.gender = Console.ReadLine();
            Console.WriteLine("Hello " + player.name + " who is a " + player.gender);
        }
    }


}

TestPlayer:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Test
{
    public class TestPlayer
    {
        public string name { get; set; }
        public string gender { get; set; }
    }
}

Thanks in advance guys!!

public void createCharacter() is just a method in your CreateCharacter class.
You create a player object in it you fill the fields with values and when createCharacter() ends, all this will return to oblivion.
In your engine, you create an empty player and after createCharacter() you try to show the players values, which are of course empty.

CreateCharacter is what we call a "Factory" - it makes TestPlayers.
Just constructing a CreateCharacter isn't enough - what it needs is a method that creates a TestPlayer and returns it, so the main Engine can save and share that TestPlayer.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.