J
J
justslipknot2018-05-26 23:30:11
.NET
justslipknot, 2018-05-26 23:30:11

How to implement this function in C#?

In general, my strength is no more, I don’t even know what to google already.
Essence of the question: there is a certain class Character, the class has 3 properties: Health, Stamina, Mana (there will be more of them in the future). There is a collection of objects of type Item and a collection of type Perks, they store items and character perks, respectively. I want to make it so that the user selects the amount of health, mana, etc. in the application window. Then he chose the items and perks that he wanted to add. The output should be a file with the console commands of the game. Commands like this: player.additem id count, player.modav health "count", player.addperk id. The output should be a .txt file with a list of commands. With all this, if the user, for example, did not change the values ​​​​of Health, Stamina, etc. in the application window. Then do not add a command that is responsible for this parameter, so that something like player.modav health 0 does not work out. It would be possible with ifs , but I know for sure that there will be more than 10 such parameters in the future. I tried to describe it as clearly as possible. Thanks

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Artem, 2018-05-26
@devspec

You can (and probably should) override the ToString of the Character class, serialize the object into json there, and save the result to a file.
https://www.newtonsoft.com/json/help/html/Serializ...
https://stackoverflow.com/questions/16921652/how-t...
etc.
PS If commands are needed, as you describe, then they can also be saved to a file in the ToString() function, using, for example, string format
System.IO.File.WriteAllText(@"D:\path.txt", $"player.additem {this.id} {this.count}...");
PPS If you need to keep track of what data has changed ... in the setter of any property, you can add a new property value to the private list corresponding to this property. If the previous element of the list is equal to the new one, add null. Then using thisoperator in string.format we determine which command to output to the file (if the current item of the private list is NOT null), and which one is not (if the current item of the private list is null).

N
nexus478, 2018-05-30
@nexus478

You need the Command pattern, it will allow you to avoid a large number of ifs and at the same time give you the flexibility to add new commands. Read about this pattern on the meta-thread, carefully study the diagrams and try to understand what role each participant in the pattern plays.
It might look something like this

public interface ICommand
{
    void Execute(CommandParameter parameter);
}

public class CommandParameter
{
    public int Id { get; set; }
    public int Count { get; set; }
    // сюда можно добавлять сколько угодно новых параметров, 
    // которые вам требуется записывать в файл
}

public class AddHealthCommand : ICommand
{
    public void Execute(CommandParameter parameter)
    {
        File.WriteAllText(@"D:\path.txt", $"player.modav health {parameter.Count}");
    }
}

public class AddItemCommand : ICommand
{
    public void Execute(CommandParameter parameter)
    {
        File.WriteAllText(@"D:\path.txt", $"player.additem {parameter.Id} {parameter.Count}");
    }
}

public class CommandWriter
{
    public void WriteCommand(ICommand command, CommandParameter parameter)
    {
        command.Execute(parameter);
    }
}

For example, the user decides to add health, it will look like this
var addHealthParameter = new CommandParameter {Count = value}; //value пришло откуда-то из интерфейса
commandWriter.WriteCommand(new AddHealthCommand(), addHealthParameter);

If the user wants to add an item, then the entry will look like this
var addItemParameter = new CommandParameter {Id = id, Count = count};
commandWriter.WriteCommand(new AddItemCommand(), addItemParameter);

And if you want to add a new command, you simply make an ICommand descendant and implement the addition logic there. And the CommandParameter class gives you the flexibility to add new parameters to commands.
You have a WPF or WinForms application, right?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question