Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
162 views
in Technique[技术] by (71.8m points)

c# - How to store data in a FILE so the garbage collector don't delete them

I'm in a early stage of making my game and I'm facing a problem... the garbage collector. I have a factory that create all the pokemons information, for exemple, here is magikarp :

        private PokemonInfo Magikarp()
        {
            //General Information
            int pokedex = 052;
            string name = "Magikarp";
            string description = "It is said to be the world’s weakest Pokémon. No one knows why it has managed to survive.";
            int gender = (int)Enum.gender.Both;
            double height = 0.9;
            double weight = 10;

            //Typing
            int type = (int)Enum.type.Water;

            //Abilities
            List<Ability> abilitiesPokemon = new List<Ability>();
            abilitiesPokemon.Add(abilities[(int)Enum.ability.Swift_Swim]);

            //Stats
            int baseHP = 20;
            int baseAttack = 10;
            int baseDefense = 55;
            int baseSpecialAttack = 15;
            int baseSpecialDefense = 20;
            int baseSpeed = 80;

            //Battle
            int catchrate = 255;
            int experience = (int)Enum.experience.Slow;
            int experienceYield = 20;

            //Moves
            List<Learnset> learnset = new List<Learnset>();
            List<Move> tmPokemon = new List<Move>();
            //========================================================Leveling
            learnset.Add(new Learnset(moves[(int)Enum.move.Splash], 1));
            learnset.Add(new Learnset(moves[(int)Enum.move.Tackle], 15));
            learnset.Add(new Learnset(moves[(int)Enum.move.Flail], 30));
            //========================================================TM
            tmPokemon.Add(moves[(int)Enum.move.Bounce]);

            return new PokemonInfo(pokedex, name, description, gender, height, weight, type, abilitiesPokemon, catchrate, experience, experienceYield, baseHP, baseAttack, baseDefense, baseSpecialAttack, baseSpecialDefense, baseSpeed, learnset, tmPokemon, Gyarados(), 20);
        }

Is there a way I can create a file with all my PokemonInfo data ? Something I can use like :

Pokedex.GetInfo(FILE.Magikarp).description;

I traveled across the web, searching far and wide, but I couldn't find the solution >.<

question from:https://stackoverflow.com/questions/65647761/c-sharp-how-to-store-data-in-a-file-so-the-garbage-collector-dont-delete-them

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You can create a class (or a static class) and declare static public properties (or fields) to hold your Pokemons. In the static constructor, you can define (and store) all your Pokemons.

public class Pokemons
{
    static Pokemons()
    {
        // here you set variables for your Pokemon
        // Then you set the Pokemon to the static field
        Magikarp = new PokemonInfo(...);
    }

    public static readonly PokemonInfo Magikarp;
}

Somewhere in your code, you can access your Pokemons like that:

var myPokemon = Pokemons.Magikarp;

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...