x


Multidimensional arrays - editing one element within one of the arrays

I have a multidimentional array set like the following

// 0 = G#, 1 = F#, 2 = E, 3 = D, 4 = C
// Second element is the time variable
songValues[0]  = [0,2.05];
songValues[1]  = [4,5.93];
songValues[2]  = [3,9.80];

I would like to be able to access the first element in one of the arrays (say songValues[0]) and change its value.

I already have a code which sends a message and variable to activate the following function (this function is within the same javascript as the above code)

function EditNoteType(noteValue : int){
    Debug.Log("edit note type worked! " + noteValue);
}

This works. Now I want to change the first variable in songValues[0] without changing the time variable.

(I'm not sure whether I can do this with some sort of "Shift/Pop" or not. My first attempt at this failed when I wrote:

songValues[0].Shift;

)

more ▼

asked Nov 25 '11 at 01:16 AM

littlemegzz gravatar image

littlemegzz
16 6 6 7

Multidimensional arrays are a bad choice. You should create a class or struct, instead.

Nov 25 '11 at 01:50 AM Jessy

what is a struct? Do you have a reference for creating a class or struct in javascript?

Nov 25 '11 at 02:03 AM littlemegzz

I provided a suggestion below. Let me know if you have any questions about it.

Nov 25 '11 at 02:39 AM Jessy

What is the multidimensional array?Why is it a bad choice?

Nov 25 '11 at 04:29 AM sokmeng
(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first

If you search around, you'll find that Array is generally a terrible thing; use List instead: http://msdn.microsoft.com/en-us/library/6sh2ey19.aspx

#pragma strict
import System.Collections.Generic;

enum Note {c, d, e, fSharp, gSharp}

class SongData {
    var note : Note;
    var time : float;

    function SongData(note : Note, time : float) {
       this.note = note;
       this.time = time;
    }
} var songValues : List.<SongData>;

function Reset() {
    songValues = new List.<SongData>([
       new SongData(Note.gSharp, 2.05),
       new SongData(Note.c, 5.93),
       new SongData(Note.d, 9.80)
    ]);
}
more ▼

answered Nov 25 '11 at 02:38 AM

Jessy gravatar image

Jessy
15.6k 72 95 196

Using a class to represent song values is definitely the way to go...

Nov 25 '11 at 04:00 AM jahroy

thanks for your help. I will use lists next time, but since my current assignment is due on Tuesday, I will manage for the time being.

Nov 27 '11 at 05:46 AM littlemegzz

are there limitations to lists? ie things you can usually do with arrays that can't be done with lists?

Dec 18 '11 at 03:33 AM ina

mmm i get this error: Can't add script behavior SongData. The script needs to derive from MonoBehavior!

Dec 18 '11 at 03:39 AM ina

@Jessy - also, there is a bit about how you should not use constructors to initialize values. http://unity3d.com/support/documentation/ScriptReference/index.Writing_Scripts_in_Csharp.html

Dec 18 '11 at 03:42 AM ina
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x1363
x356
x40
x37
x18

asked: Nov 25 '11 at 01:16 AM

Seen: 1288 times

Last Updated: Dec 18 '11 at 06:28 AM