x


How to return an array on-the-fly

Hi!

Here's how my code works:

public class myClass : MonoBehaviour{
  public static int[] myMethod(){
    List<int> aList = new List<int>();
    // ...
    // Filling the list
    int[] anArray = aList.ToArray();
    return anArray;
  }
}

and :

public class myMainClass : MonoBehaviour{
  void OnGUI() {
    Event e = Event.current;
    if (e.button == 0 && e.isMouse){
        int[] myArray = myClass.myMethod();
    }
  }
}

Now, the problem is that I need to return the array from myClass to myMainClass "on-the-fly", that is, even if the array is not completely filed, it already has to return its first values.

Thank you!

Note: I use a list at first (and eventually convert it to an array) because of its dynamic size property.

more ▼

asked Apr 04 '12 at 10:01 AM

Jodll gravatar image

Jodll
34 3 3 3

I don't know what your purposes are. It sounds like you're trying to do something asynchronously. Maybe you can explain a bit more what you are trying to achieve? Consider using enumerables or queues depending on your needs.

Apr 04 '12 at 10:46 AM Statement ♦♦

I agree with statement... and: You should probably be doing the filling on a different function and the extracting of the array only when needed. OnGUI is called several times in one frame and you do not need updates so often.

Apr 04 '12 at 02:13 PM GuyTidhar
(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first

Well It seems you just want a reference to the array in your main class. Just have the array as a public class level variable in your sub class and call the ref. But beware if the array is not instantiated it will throw an exception. Or another function or getter.

more ▼

answered Apr 13 '12 at 11:26 AM

dulan gravatar image

dulan
16

(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:

x4372
x1396

asked: Apr 04 '12 at 10:01 AM

Seen: 588 times

Last Updated: Apr 13 '12 at 11:26 AM