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

Question Parse string by X amount

Discussion in 'Scripting' started by Corva-Nocta, Sep 12, 2023.

  1. Corva-Nocta

    Corva-Nocta

    Joined:
    Feb 7, 2013
    Posts:
    801
    Hey all,
    I'm working on a system of generating a grid map of a fixed size based on a string. For simplicity, let's just say it's a 3x3 grid. So what I'm trying to make is a string like "FHFTFGFJHDFBAFAHAY" the idea being that every 2 characters make up a single reference that will spawn the according tile on thr grid. So in the above example: FH means it's a field that spawns at 1,1 FT means a mountain spawns at 1,2 and FG means a lake will spawn at 1,3 and so on.

    So my question is: how do I parse a string for every other character?
     
  2. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    6,121
    Strings are arrays of characters. They have an indexer, in fact, that returns the character at each index. So one way is to just treat them like arrays and iterate through them:

    Code (CSharp):
    1. string someString = "String";
    2. for (int i = 0; i < someString.Length; i++)
    3. {
    4.    char c = someString[i];
    5.    Debug.Log(c);
    6. }
    The string class also has a number of methods for getting smaller pieces of the string at the same time. Though do note that working with strings like this will allocate a ton.
     
  3. ijmmai

    ijmmai

    Joined:
    Jun 9, 2023
    Posts:
    188
    a way to get two characters in a string would be

    Code (CSharp):
    1. string someString = "String";
    2. for (int i = 0; i < someString.Length; i+2)
    3. {
    4.    string c = someString[i]+someString[i+1];
    5.    Debug.Log(c);
    6. }
    But is going with a string the best way? Where does the string come from, how is it generated?
     
  4. Corva-Nocta

    Corva-Nocta

    Joined:
    Feb 7, 2013
    Posts:
    801
    That makes sense, I'll likely need to use a more efficient system in the future. But that's a good starting place!
     
  5. Corva-Nocta

    Corva-Nocta

    Joined:
    Feb 7, 2013
    Posts:
    801
    Oh that makes sense! And would be pretty easy to implement. Thanks!

    As for it being the best way, probably not. I'll be generating the string a little later (pretty much just a for loop that generates 2 characters for every tile, based on what the tile should be) that will be used for the map.
     
  6. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    6,121
    An array of bytes could be more straight forward.

    Depends how many different types of tiles you have. I'm working on a tile-based game as well, and opted to use
    ushort
    as the numerical ID's for different tile types. And then a 16 * 16 (256) array of these can be used to generate the necessary data for a chunk.

    Then a singleton scriptable object with a lookup table (dictionary serialised with Odin) can be used to convert these ID's into the actual scriptable objects that define the tiles.
     
  7. Corva-Nocta

    Corva-Nocta

    Joined:
    Feb 7, 2013
    Posts:
    801
    Oh wow, that sounds way more efficient!

    I'm not 100% sure on how big the tile array will be, still toying around with that, but probably in the neighborhood of 50ish to 100ish.

    Maybe I'll look into ushorts instead. I'm not too familiar with them, so probably something I should learn about.
     
  8. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    6,121
    I more so mean how many different types of tiles there'll be. As you'll need a data type can represent enough different kinds of tiles. A byte will give you a capacity of 256 different types. A
    ushort
    - which is just halfway between a byte and a
    uint
    - gives you 65,535 different types, which is enough for most games.
     
    ijmmai likes this.