Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Getting Information from Excel Spreadsheet?

Discussion in 'Scripting' started by DRRosen3, Oct 19, 2014.

  1. DRRosen3

    DRRosen3

    Joined:
    Jan 30, 2014
    Posts:
    683
    I have heard that it's possible to use Microsoft Excel and Access to make scripting easier/quicker/more efficient. However, I'm not quite sure how this would be accomplished (if it can be). What I'm trying to accomplish is this:

    I have creatures in my game that correspond to element types. The players also have element types, as well as both the creatures and players attacks. I need to basically have a way of determining (in code) what element type an attack is and then what element type the target is. Some creatures and players also have two element types. I started to script this using "if statements". However, I then realized this would lead to hundreds of lines of code (given all of the possible combinations of attack element type vs. target element type when the target has two element types because there are over 20 types).

    For example:
    -Creature attacks player with fireball.
    -Fireball elements type = fire.
    -Player's element types = earth & stone.
    -Both earth and stone take half damage from fire element.
    -This would result in the player taking 25% of the total damage.

    And that's just one example where the player has two types. Again given 20 different types, you could imagine how many "if ??? == ??? && ???==??? || ??? == ??? & ???=???" I would end up writing. Any help getting this onto an excel spreadsheet (or just simplifying this in general) would be greatly appreciated.

    P.S. - The attack's element type is stored on each attack's script as a variable of an enum list of elements (public enum ElementTypes) and the player's/creature's element is stored in the same way as two variables of type ElementTypes: playerType01 & playerType02 ...on their script.

    P.S.S. - Coding in C# by the way. And I'm not expecting someone to give me the step-by-step process. A point towards a good tutorial would be great.
     
  2. cl9-2

    cl9-2

    Joined:
    May 31, 2013
    Posts:
    417
  3. DRRosen3

    DRRosen3

    Joined:
    Jan 30, 2014
    Posts:
    683
    I read over that CSVReader wiki and somehow I don't quite thing that lines up with what I need to accomplish. Below is a screenshot of the table in excel.

    What I need to be able to do is plug values into the following equation based on the attack type (left column) when compared against the target's type (top row).

    Modifier = STAB * (Type1 * Type2) * Critcal * other * Random.Range(0.85, 1);

    Of course Type1 and Type2 are the values from the chart. I suppose (getting a little ahead of myself here) I'm also going to have to understand how to access the attack element type (from its script) and the target's types (from its script).

     
  4. cl9-2

    cl9-2

    Joined:
    May 31, 2013
    Posts:
    417
    In the equation Modifier = STAB * (Type1 * Type2) * Critcal * other * Random.Range(0.85, 1);

    Shouldn't (Type1 * Type2) be something like Coefficient(Type1, Type2) ?
    i.e. Coefficient(NORMAL, NORMAL) = 1

    In any event, you would use the CSVReader to read the exported CSV file into the array that you then would multiply values.
     
  5. DRRosen3

    DRRosen3

    Joined:
    Jan 30, 2014
    Posts:
    683
    So I would need to add another portion then. Type1 is equal to the attack's element (first column) vs the target's first element (top row). Type2 is equal to the attack's element (first column) vs the target's second element (top row).

    So to put a face on this. For example:
    -Creature attacks player with Toxic.
    -Toxic's elements type = POISON.
    -Player's element types = GROUND & ROCK.
    -Both GROUND and ROCK take half damage from POISON element.
    -This would result in the player taking 25% of the total damage.

    Then Type1 = 0.5 (POISON vs GROUND) ...and... Type2 = 0.5 (POISON vs ROCK) which makes the attacks total effectiveness (which is what I use the chart to solve in the first place) 0.25 (because 0.5 * 0.5 = 0.25).

    So I think what you're telling me is that I need another portion to this. A portion that gets the attackElement information from the attack script and compares it to the playerElement01 & playerElement02 information from their script.

    Sorry, I'm still a novice at programming.
     
  6. cl9-2

    cl9-2

    Joined:
    May 31, 2013
    Posts:
    417
    Actually no, I wasn't certain what Type1 and Type2 corresponded to, but I see now what they correspond to. You would still use CSVReader to read the exported CSV into an array, then calculate Type1 and Type2 based on the values in the array
     
  7. Deleted User

    Deleted User

    Guest

  8. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,773
    If you need to import Excel data, then you'll need to learn, at minimum, file input and parsing. These are not entirely trivial skills to learn, if you don't already have them.

    It might be drastically easier (if this is the one thing you need Excel input for) to write an inspector GUI with a grid of float-input fields.
     
  9. DRRosen3

    DRRosen3

    Joined:
    Jan 30, 2014
    Posts:
    683
    Well @supremegrandruler 's thread was actually a real eye-opener to another method. I'm going to do some testing in a bit to see if the answer he got in his thread will work for me. It's basically using a 2D Array.