Can't use saved information to load into an array

Stack Overflow Asked by Danny on August 13, 2020

I’ve managed to save some information into a file. I want to be able to read that information and use it to create an item

public void Save()
   StreamWriter writer;
   writer = new StreamWriter("PlayerInventory.txt");
   for (int i = 0; i < playerInventory.Length; i++)
      if (playerInventory[i] != null)
         if (playerInventory[i] is Sheild sheild)
            writer.WriteLine("Sheild:" + sheild.Defence + "," + + "," + sheild.description + "," + sheild.cost);

I started using this code to load it but i don’t know how to finish the code. Right now its split but i don’t know how to assign it to anything.

For example sheild:20,Shield,this is a shield,100

Right now that’s split into

  1. 20,
  2. Shield,
  3. this is a shield,
  4. 100

But I don’t know who to assign those stuff to an array

public void Load()
   StreamReader reader;
   reader = new StreamReader("PlayerInventory.txt");
   string line;
   string[] currentLineData;

   while (true)
         line = reader.ReadLine();
         if (line == null)
         if (line.Contains("Sheild"))
            line.Replace("Sheild:", "");
            currentLineData = line.Split(',');

One Answer

it looks like you are designing a game.

If I were setting up an inventory that included a shield with stats, I'd look at creating a complex data type, using a "struct" for an inventory item.

Then I'd create an array of the inventory struct items, with one for each inventory slot - before loading my file in.

This could hold all the different info you need for any item that can be stored in inventory like Item Name, defense strength, attack strength etc.

Then, when you load your file in, all the right stuff will be in places with nice names etc. for you to easily find them later.

If you need an extra stat, add a row to your struct definition, then add a column to your file and you are ready to go.

Answered by trevor on August 13, 2020

Add your own answers!

Related Questions

Bluehost hosted React pages not found

0  Asked on November 10, 2021 by paul-lindberg


Change a view margins programmatically

4  Asked on November 10, 2021 by m-r_dino


React returning view from switch statement

1  Asked on November 10, 2021 by mattblack


Find string then return different row value Python

0  Asked on November 10, 2021 by newtothecrew


React JS – route does not pick the required Component

0  Asked on November 10, 2021 by nirjal-paudel


get character after space of array value on javascript

9  Asked on November 7, 2021 by riko-pramayudi


Ask a Question

Get help from others!

© 2022 All rights reserved. Sites we Love: PCI Database, MenuIva, UKBizDB, Menu Kuliner, Sharing RPP