JavaScript question re: syntax

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.

Or you can try:

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

Cheers.

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

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 while defining these fields as text - I could concatenate them and get:

but:

might be more efficient…

In FileMaker Pro fwiw:

gives:

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…

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:

sword,knife,gun
healing,cure poison,invisibility
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:

var dataFile : TextAsset;
var weapons : String[];
var potions : String[];
var armor : String[];

function Start () {
	var myData = dataFile.text.Split("\n"[0]);
	weapons = myData[0].Split(","[0]);
	potions = myData[1].Split(","[0]);
	armor = myData[2].Split(","[0]);
}

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

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

^[ ]+([0-9]+) ([a-zA-Z]+)$

…as your Find string and

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.