Initialising data whilst game running

Is this possible…

I have an animation that should start as soon as it can, then whilst it is running I want to load some data into an array and then start using it once it is ready. I thought the way to do this was using yield. However regardless of what I do the whole game stops whilst the initialising of the data takes place, this can take up to a several seconds. Is there a way around this?

So in more detail this is the setup

in one Update() function I play the animation - this works on its own fine.

Then in another script in Start() I call an init() function that yields whilst the data is loaded.

private var rbf : RBF;

function Start() {
	rbf = new RBF(transform.gameObject, aniName, useCaching, useBlending, color1, color2 );	
	init();
}

function init() {
   yield rbf.buildCache();
}

When I do this the entire game hangs until rbf.buildCache() returns.

Also, if I make the rbf.buildCache() function a void rather than returning a bool and then put another yield function inside it (as in the script help) the original animation starts playing correctly but the cache is never built.

Is there a way to set this up that will work?

Using yield doesn’t use threads, so if you have a function that takes several seconds to complete, it will halt everything for several seconds. Either use threads, or divide up the function into smaller bits, like

for (i = 0; i < 1000000; i++) {
  // do stuff
  if (i%1000 == 0) {yield;}
}

will run 1000 iterations in 1000 frames, instead of doing 1000000 iterations in 1 frame.

–Eric

I see. I did try threading it but that caused other weirdness. I’ll have to see if I can break it up as you suggest.

hmmmm…

So if i do this

function init() {
	yield test();
}

function test() {
	for (i = 0; i < 1000000; i++) { 
	  // do stuff 
	  if (i%1000 == 0) {Debug.Log("testing " + transform.name); yield;} 
	}
}

then all is good.

But if I move the test function inside my class then it never runs, this seems to match what I was seeing before, as soon as I put a yield in a class it won’t work. Can you not use yield in a class function?

[edit]
I’ve got the whole thing working outside of the class now, would still be interested to know if this can be made to work in a javascript class.