Search Unity

2D - Instantiate gameobjects in tiles from a .txt?

Discussion in '2D' started by Gonzasmpl, Aug 5, 2018.

  1. Gonzasmpl

    Gonzasmpl

    Joined:
    Jul 30, 2018
    Posts:
    55
    I'm trying to make a "semi random" dungeon generator in which diferent levels layout randomly get instantiate when you go through "portals". The poblem is that, say i want to make 1000 diferent level layouts and i need all of them to be lightweight. So, i think a good idea is to use .txt files which contains words and make a script read them and depending the word it reads in the first line and column, instantiate a gameobject in the first tile of my map and so on and so forth. The thing is i have no clue in how to achieve this, so if someone can help me or guide me somehow i'd be highly grateful. Also i want to create or use some empty .txt to save the previous levels which the player go through, so he can return to a previous level if he wants to. Thanks in advance!

    Example
    ----
    AAA-AA--P--AA-AAA
    AA--------------------AA
    A-----BB------BB-----A
    -------B----------B-------
    ----------------------------
    -------B----------B-------
    A-----BB------BB-----A
    AA--------------------AA
    AAA-AA--P--AA-A A A
    ---
    A = rock / B = tree / P = portal / blank space = nothing
    ( I know the example is no well made but it's for you to understand me )
     
    Last edited: Aug 5, 2018
  2. Shack_Man

    Shack_Man

    Joined:
    Jun 7, 2017
    Posts:
    372
    you could use numbers instead of letters, break the string into single letters and cast the strings as int. then you can simply have a list with the according tiles and pull one depending via the int.
     
  3. knobblez

    knobblez

    Joined:
    Nov 26, 2017
    Posts:
    223
  4. Gonzasmpl

    Gonzasmpl

    Joined:
    Jul 30, 2018
    Posts:
    55
    Thanks. You both gave me an overall idea and looking cautiously the code i finally achieved it exactly the way i want it to be.