Search Unity

Question How do I add more lines to this list for CSV-Exportation

Discussion in 'Scripting' started by dasruedi_unity, Feb 12, 2024.

  1. dasruedi_unity

    dasruedi_unity

    Joined:
    Jul 20, 2022
    Posts:
    13
    I was following this tutorial on exporting data from a list to a CSV-Excel-File.



    Everything works as intended by the tutorial. However in the video the creator adds information to the list through the inspector. I instead need to add the information on runtime, but I am not very experienced in the use of lists, so the answer to my question might be quite easy.

    I simply need the certain lines of code that allow me to add slots to the list length and to add in the needed parameters to the added slots.

    thanks in advance for any helpful advice.
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,726
    Fix that. First place to start would be with the docs for List<T>(). The .Add() method is pretty hard to get wrong. :)

    If that's not the collection in use, then go figure out what the collection in use is and look up how to add to it.
     
    bugfinders likes this.
  3. dasruedi_unity

    dasruedi_unity

    Joined:
    Jul 20, 2022
    Posts:
    13
    Yeah I know, I need to fix that, but I have issues grasping the workings of lists and other functions that do not return an immediate visible effect. But thats a topic for another day.

    For now could you maybe give me a code example for that?


    upload_2024-2-12_22-41-22.png


    in the tutorial they create the player class and and the attributes name, health, damage defense.

    What lines of code would one use in order to add one player to the list, give them a name and set all attributes to 50?
     
  4. bugfinders

    bugfinders

    Joined:
    Jul 5, 2018
    Posts:
    1,784
    well they use arrays, and arrays are arguably designed to be fixed.. So looking at this its designed to be entered in the inspector, so you could do that. Or convert to a list like Kurt mentioned, which frankly is designed to have additions and removals
     
    Bunny83 likes this.
  5. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,726
    Hm, sounds like you've been WATCHING the tutorial and not DOING the tutorial.

    Two steps to doing tutorials correctly:

    - do everything perfectly as outlined by the tutorial-maker
    - understand all parts of it enough to explain it to your dog (see my picture)

    This second step can take weeks, days, months, years, but is essential to the process.

    As bugfinders noted above, convert the array to a List, add the stuff you need, then convert it back.

    All parts of all games done anywhere are done one tiny step at a time, building upon all previous steps, like this fellow:

    Imphenzia: How Did I Learn To Make Games:

     
  6. dasruedi_unity

    dasruedi_unity

    Joined:
    Jul 20, 2022
    Posts:
    13
    So by now I have changed my Equivalent of the "public class PlayerList" into a List<T>.
    Instead of Players and RPG roles I have Beans with different attributes.

    I am now able to press "P" and add new Beans to the List in runtime and those are also present in the CSV I write by pressing "space"

    upload_2024-2-13_15-45-28.png upload_2024-2-13_15-46-15.png upload_2024-2-13_15-46-32.png

    however, I do not know how to adress the 16 attributes. As you can see, I tried two ways in the AddInfo() Method that didn't work.

    Once I got this to work I should be done with this issue and can move back to the stuff I know how to do.

    I am aware that this is something I should know how to use on a deeper level but I learn best through repetitive usage and adjusting of tools I am given. This is how I accumulated most of my coding knowledge and it worked out well so far.

    So please if you could just give me the answer, I will keep reusing and adjusting it for different situations until I have a grasp of it's workings.
     
  7. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,479
    This is a very fundamental C# question you're asking here disguised as some specific project problem. You're asking how to create a class instance (which you're doing when you add it to the list) and how to set its fields/properties. The reason I am stating this is that you should really follow some basic C# tutorials on this first. :)

    Code (CSharp):
    1. var myBean = new Beans();
    2. myBean.ID = 123;
    3. <etc>
    4. myBeansList.Add(myBean);
    https://www.w3schools.com/cs/cs_classes.php
    https://www.tutorialsteacher.com/csharp/csharp-class

    NOTE: When posting code, please post using code-tags and NOT using images so devs can refer to your code easier.

    Thanks.