Search Unity

JavaScript question re: syntax

Discussion in 'Scripting' started by Adam-Buckner, Mar 5, 2008.

  1. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,664
    In the example on

    http://www.unifycommunity.com/wiki/index.php?title=Programming_Chapter_2

    There is the following example for creating an array:

    I am assuming that these needs to lie on separate lines, and should not be written:

    The reason I ask, is that I need to open over 150 arrays containing about 30 variables each. The easiest way I could think of doing this data entry was to bring this data into a FileMaker Pro and run a calculation on the fields ("Sword", "Knife", etc..) I need and output one single bit of code per array. When I do this, it formats the text into one long line.
     
  2. thylaxene

    thylaxene

    Joined:
    Oct 10, 2005
    Posts:
    716
    Or you can try:

    var weapons : String[] = ["Sword", "Knife", "Gun"];

    Cheers.
     
  3. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    No, the only thing that matters is the semicolon, which separates lines. Everything else is just for your convenience, so you can use one line for your entire script if you really wanted....

    If you have a lot of data, you probably want to store it in some kind of database and parse that (a text file would be OK), rather than hard-coding everything.

    --Eric
     
  4. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,664
    Heh! Thanks!

    I put my thinking cap on, pulled out a bottle of Havana Club 7 for a snort, and realized that if I defined two global fields in FileMaker Pro - one as a <"> and the other as a <return> while defining these fields as text - I could concatenate them and get:
    but:

    might be more efficient...


    --

    In FileMaker Pro fwiw:

    gives:

     
  5. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,664
    Eric5h5 -

    Is there an easy way to input large volumes of data? Or store large amounts of data that Unity can use?

    I'll have to admit that I can script FileMaker Pro pretty well, but I'm only at the beginner phase with Java right now.

    I've picked a simple test game to try out my JavaScripting on. A turn based game, so there is one game state per player and each turn the game states of each player will be modified by player actions; next turn.

    Currently I'm using FMP to format the data in need, so I'll only have to input the main data bits, and for the arrays, leave FMP to actually write the code, as it's simple and repetitive...

    But I could imagine wanting to write/have some sort of game state database that Unity could look at. I was assuming that I'd have to start with arrays...
     
  6. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    Yep, a TextAsset would be one way. As a simple example, say you had 3 arrays for weapons, potions, and armor; you could use this:

    Code (csharp):
    1. sword,knife,gun
    2. healing,cure poison,invisibility
    3. chain mail,plate mail,jacket
    and save it as "data.txt" (Unity needs the .txt extension to recognize it as text) in your project folder. Then have:

    Code (csharp):
    1. var dataFile : TextAsset;
    2. var weapons : String[];
    3. var potions : String[];
    4. var armor : String[];
    5.  
    6. function Start () {
    7.     var myData = dataFile.text.Split("\n"[0]);
    8.     weapons = myData[0].Split(","[0]);
    9.     potions = myData[1].Split(","[0]);
    10.     armor = myData[2].Split(","[0]);
    11. }
    You'd drag the data.txt file onto the appropriate slot in the Inspector of course. Then the script just splits it on linefeed chars into an array of strings, which are then split on comma chars into the other string arrays.

    That's pretty simplistic though...if you have lots of data, it would be better to use a more robust format that doesn't rely on magic numbers like this example.

    --Eric
     
  7. andeeeee

    andeeeee

    Joined:
    Jul 19, 2005
    Posts:
    8,768
    The way I would go about something like this is to have the contents of the array as separate lines in a text file to begin with (you can just export this from FileMaker if you have already done a lot of typing into a FileMaker DB). Open the file with the free TextWrangler editor and use its Add Line Numbers command (on the Text menu).

    You can now generate an array definition by using a regular expression in Find/Replace and pasting into your source file. You would want something like

    Code (csharp):
    1. ^[ ]+([0-9]+) ([a-zA-Z]+)$
    ...as your Find string and

    Code (csharp):
    1. arrayName[\1] = "\2";
    ...as the Replace string.

    If you make a mistake or want to use another method of defining the array, you just go back to the original text file (just Undo) and change the regular expression slightly.