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

Automating My Script

Discussion in 'Scripting' started by apkimmerling, Jul 25, 2022.

  1. apkimmerling

    apkimmerling

    Joined:
    Sep 29, 2020
    Posts:
    5
    Hi! I have made a Tile Automator script(from a YouTube tutorial but heavily modified) as well as a save system, so I can save and load a "world" for the player, using Unity's TileMap system. I have a piece of code that handles the actual spawning of the procedurally generated tile "chunk". There's a start position, which I am having trouble automating. For the actual if statements, I could use a for loop, but how would I go about automating the start position. Eventually, the world will be "infinite" with loading and unloading tile "chunks", which is why I need to automate this script. Thanks in advance!
    Code (CSharp):
    1.  
    2.             /*
    3.             for (int i = startIntVar; i <= endIntVar; i++)
    4.             {
    5.  
    6.             }
    7.             */
    8.  
    9.             if (chunkCount == 1)
    10.             {
    11.                 startPosition = new Vector3Int(width, height, 0);
    12.                 DoSim(numR);
    13.                 StartCoroutine(SaveEnum());
    14.                 isPreviousSimComplete = false;
    15.             }
    16.  
    17.             if (chunkCount == 2)
    18.             {
    19.                 startPosition = new Vector3Int(-width, -height, 0);
    20.                 DoSim(numR);
    21.                 StartCoroutine(SaveEnum());
    22.             }
    23.  
    24.             if (chunkCount == 3)
    25.             {
    26.                 startPosition = new Vector3Int(-width, height, 0);
    27.                 DoSim(numR);
    28.                 StartCoroutine(SaveEnum());
    29.             }
    30.  
    31.             if (chunkCount == 4)
    32.             {
    33.                 startPosition = new Vector3Int(width, -height, 0);
    34.                 DoSim(numR);
    35.                 StartCoroutine(SaveEnum());
    36.             }
    37.  
    38.             if (chunkCount == 5)
    39.             {
    40.                 startPosition = new Vector3Int(width, 0, 0);
    41.                 DoSim(numR);
    42.                 StartCoroutine(SaveEnum());
    43.             }
    44.  
    45.             if (chunkCount == 6)
    46.             {
    47.                 startPosition = new Vector3Int(-width, 0, 0);
    48.                 DoSim(numR);
    49.                 StartCoroutine(SaveEnum());
    50.             }
    51.  
    52.             if (chunkCount == 7)
    53.             {
    54.                 startPosition = new Vector3Int(0, height, 0);
    55.                 DoSim(numR);
    56.                 StartCoroutine(SaveEnum());
    57.             }
    58.  
    59.             if (chunkCount == 8)
    60.             {
    61.                 startPosition = new Vector3Int(0, -height, 0);
    62.                 DoSim(numR);
    63.                 StartCoroutine(SaveEnum());
    64.             }
    65.  
    66.             if (chunkCount == 9)
    67.             {
    68.                 startPosition = new Vector3Int(width * 2, height * 2, 0);
    69.                 DoSim(numR);
    70.                 StartCoroutine(SaveEnum());
    71.             }
    72.  
    73.             if (chunkCount == 10)
    74.             {
    75.                 startPosition = new Vector3Int(-width * 2, -height * 2, 0);
    76.                 DoSim(numR);
    77.                 StartCoroutine(SaveEnum());
    78.             }
    79.  
    80.             if (chunkCount == 11)
    81.             {
    82.                 startPosition = new Vector3Int(-width * 2, height * 2, 0);
    83.                 DoSim(numR);
    84.                 StartCoroutine(SaveEnum());
    85.             }
    86.  
    87.             if (chunkCount == 12)
    88.             {
    89.                 startPosition = new Vector3Int(width * 2, -height * 2, 0);
    90.                 DoSim(numR);
    91.                 StartCoroutine(SaveEnum());
    92.             }
    93.  
    94.             if (chunkCount == 13)
    95.             {
    96.                 startPosition = new Vector3Int(width * 2, 0 * 2, 0);
    97.                 DoSim(numR);
    98.                 StartCoroutine(SaveEnum());
    99.             }
    100.  
    101.             if (chunkCount == 14)
    102.             {
    103.                 startPosition = new Vector3Int(-width * 2, 0 * 2, 0);
    104.                 DoSim(numR);
    105.                 StartCoroutine(SaveEnum());
    106.             }
    107.  
    108.             if (chunkCount == 15)
    109.             {
    110.                 startPosition = new Vector3Int(0 * 2, height * 2, 0);
    111.                 DoSim(numR);
    112.                 StartCoroutine(SaveEnum());
    113.             }
    114.  
    115.             if (chunkCount == 16)
    116.             {
    117.                 startPosition = new Vector3Int(0 * 2, -height * 2, 0);
    118.                 DoSim(numR);
    119.                 StartCoroutine(SaveEnum());
    120.             }
    121.  
    122.             if (chunkCount == 17)
    123.             {
    124.                 startPosition = new Vector3Int(width * 2, height, 0);
    125.                 DoSim(numR);
    126.                 StartCoroutine(SaveEnum());
    127.             }
    128.  
    129.             if (chunkCount == 18)
    130.             {
    131.                 startPosition = new Vector3Int(width * 2, -height, 0);
    132.                 DoSim(numR);
    133.                 StartCoroutine(SaveEnum());
    134.             }
    135.  
    136.             if (chunkCount == 19)
    137.             {
    138.                 startPosition = new Vector3Int(-width * 2, height, 0);
    139.                 DoSim(numR);
    140.                 StartCoroutine(SaveEnum());
    141.             }
    142.  
    143.             if (chunkCount == 20)
    144.             {
    145.                 startPosition = new Vector3Int(-width * 2, -height, 0);
    146.                 DoSim(numR);
    147.                 StartCoroutine(SaveEnum());
    148.             }
    149.  
    150.             if (chunkCount == 21)
    151.             {
    152.                 startPosition = new Vector3Int(width, height * 2, 0);
    153.                 DoSim(numR);
    154.                 StartCoroutine(SaveEnum());
    155.             }
    156.  
    157.             if (chunkCount == 22)
    158.             {
    159.                 startPosition = new Vector3Int(-width, height * 2, 0);
    160.                 DoSim(numR);
    161.                 StartCoroutine(SaveEnum());
    162.             }
    163.  
    164.             if (chunkCount == 23)
    165.             {
    166.                 startPosition = new Vector3Int(width, -height * 2, 0);
    167.                 DoSim(numR);
    168.                 StartCoroutine(SaveEnum());
    169.             }
    170.  
    171.             if (chunkCount == 24)
    172.             {
    173.                 startPosition = new Vector3Int(-width, -height * 2, 0);
    174.                 DoSim(numR);
    175.                 StartCoroutine(SaveEnum());
    176.             }
     
    Last edited: Jul 25, 2022
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,945
    .
    .
    .

    Oh dear... You need to learn about collections and iterations, not writing 24 pieces of code for this. That'll never scale!!

    From there you need to learn about using indexing variables and producing coordinates based on them.

    For instance, this code iterates a 2-dimensional grid and produces positions based on the two index values.

    https://github.com/kurtdekker/makeg...makegridfrombitmap/Bitmap2Grid/Bitmap2Grid.cs

    Look around lines 60 and 62 for the iterators, and then find the
    ComputePosition()
    method for how it maps the
    i,j
    iterators into worldspace.

    Don't get ahead of yourself on features and complexity. Make sure you understand 100% of each thing you attempt as you are learning, otherwise you're just building on quicksand.

    Tutorials and example code are great, but keep this in mind to maximize your success and minimize your frustration:

    How to do tutorials properly, two (2) simple steps to success:

    Step 1. Follow the tutorial and do every single step of the tutorial 100% precisely the way it is shown. Even the slightest deviation (even a single character!) generally ends in disaster. That's how software engineering works. Every step must be taken, every single letter must be spelled, capitalized, punctuated and spaced (or not spaced) properly, literally NOTHING can be omitted or skipped.

    Fortunately this is the easiest part to get right: Be a robot. Don't make any mistakes.
    BE PERFECT IN EVERYTHING YOU DO HERE!!

    If you get any errors, learn how to read the error code and fix your error. Google is your friend here. Do NOT continue until you fix your error. Your error will probably be somewhere near the parenthesis numbers (line and character position) in the file. It is almost CERTAINLY your typo causing the error, so look again and fix it.

    Step 2. Go back and work through every part of the tutorial again, and this time explain it to your doggie. See how I am doing that in my avatar picture? If you have no dog, explain it to your house plant. If you are unable to explain any part of it, STOP. DO NOT PROCEED. Now go learn how that part works. Read the documentation on the functions involved. Go back to the tutorial and try to figure out WHY they did that. This is the part that takes a LOT of time when you are new. It might take days or weeks to work through a single 5-minute tutorial. Stick with it. You will learn.

    Step 2 is the part everybody seems to miss. Without Step 2 you are simply a code-typing monkey and outside of the specific tutorial you did, you will be completely lost. If you want to learn, you MUST do Step 2.

    Of course, all this presupposes no errors in the tutorial. For certain tutorial makers (like Unity, Brackeys, Imphenzia, Sebastian Lague) this is usually the case. For some other less-well-known content creators, this is less true. Read the comments on the video: did anyone have issues like you did? If there's an error, you will NEVER be the first guy to find it.

    Beyond that, Step 3, 4, 5 and 6 become easy because you already understand!
     
  3. apkimmerling

    apkimmerling

    Joined:
    Sep 29, 2020
    Posts:
    5
    Yeah, I know that creating if statements over and over are never going to work or scale, or are remotely efficient, this was just to test the other systems, such as saving and loading. As for the tutorial part of what you mentioned, the tutorial I followed was strictly procedural generation of tiles. It did not cover saving or loading, or loading in chunks of tiles. The tutorial had a different use case, I just adjusted it pretty heavily to fit my needs. I created the chunk system on my own, again, just to test out how saving/loading worked. The if statements wasn't going to be my final outcome, as I knew it was a bad system. Thank you for your response, it truly does mean a lot. I had extensively watched the tutorial and wrote it line by line, word for work, exactly the same, and examined it, to make sure I knew what each line of code was doing, and how it was changing how the code worked. This was so I could change it with relative ease, without having to mess around for hours trying to figure out how to do something.