Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Question Create a filtered list of World Names

Discussion in 'Scripting' started by apkimmerling, Oct 17, 2022.

  1. apkimmerling

    apkimmerling

    Joined:
    Sep 29, 2020
    Posts:
    5
    I want to create a dropdown with the world names the user selects, and I want it to work kinda like how Minecraft does, where the user creates a world, and adds it to the top of the World Selection "screen". Kinda like this:
    Code (CSharp):
    1. //A, B, C, D    <----Default array
    2. //D, C, B, A    <----New, formatted array
    3. //LOAD A
    4. //A, D, C, B    <----New, formatted array when 'A' loaded
    5. //LOAD C
    6. //C, A, D, B    <----New, formatted array when 'C' loaded
    7. //CREATE E
    8. //E, C, A, D, B <----New, formatted array when 'E' created
    I have the list(Array that is converted into a list) saved into a .json file, that when a new world is created, is saved to the end of that array. I also have a lastPlayedWorld integer that stores the index of the last played world in the array.
    Sorry if that's a lot of information, no idea if anything actually made sense lol. Just ask if you need any clarification.
    Thank you in advance!
     
  2. Nad_B

    Nad_B

    Joined:
    Aug 1, 2021
    Posts:
    373
    Well basically you have two solutions:
    - When creating a new world, add it at the top of the list by using
    WorldsList.InsertAt(0, newWorld);

    or
    - After loading the worlds from Json, order them in descending order by creation date (if you have this info) or id (if incremental):
    WorldList = loadedWorldsFromJson.OrderByDescending(x => x.CreationDate).ToList();
    (don't forget to add
    using System.Linq;
    )
     
    apkimmerling likes this.
  3. apkimmerling

    apkimmerling

    Joined:
    Sep 29, 2020
    Posts:
    5
    Adding a creation date doesn't sound like a bad idea...I'm also thinking about having an openedDate that'll change when the user opens/loads in the world, and sort by that. Thank you SOO much for your suggestion!
     
    Last edited: Oct 17, 2022
    Nad_B likes this.
  4. Munchy2007

    Munchy2007

    Joined:
    Jun 16, 2013
    Posts:
    1,732
    A standard unsorted list should be all you need for this.

    When a new world is added insert it at the beginning of the list as suggested by apkimmerling and when a world is loaded, remove it from the list and then re-insert it at the beginning of the list.
     
  5. Yoreki

    Yoreki

    Joined:
    Apr 10, 2019
    Posts:
    2,590
    https://learn.microsoft.com/en-us/dotnet/api/system.datetime.now?view=net-6.0

    You will find a lot of information and even examples on basic things like that super quickly!
    Simply searching exactly what you asked above: "date and time in c#", should yield the above result :)
     
    orionsyndrome likes this.