I’m trying to write a script that cycles through the values in an array every x number of seconds. I’ve tried it a couple dozen ways and nothing seems to work. Here’s one of my failed attempts to give you an idea what I’m trying to do. I’ve read and re-read the docs and still can’t see how to do this. Can anyone help?
var myArray : float[];
var secs : float = 2.0;
function Update () {
DelayAndDo ();
}
function DelayAndDo () {
for (var value in myArray) {
//Do my thing here
yield WaitForSeconds(secs);
}
}
var myArray : float[];
var secs : float = 2.0;
function Start () {
DelayAndDo ();
}
function DelayAndDo () {
while( true ){
for (var value in myArray) {
//Do my thing here
}
yield WaitForSeconds(secs);
}
}
Yeah, I ran into this recently and as UT’s resident artist that has very little business scripting stuff I was taught this technique…
Instead of:
var myArray : float[];
var secs : float = 2.0;
function Update () {
DelayAndDo ();
}
function DelayAndDo () {
for (var value in myArray) {
//Do my thing here
yield WaitForSeconds(secs);
}
}
try:
var myArray : float[];
var secs : float = 2.0;
while (true)
{
yield WaitForSeconds DelayAndDo ();
}
function DelayAndDo ()
{
for (var value in myArray)
{
//Do my thing here
yield WaitForSeconds(secs);
}
}
Don’t take this as a solid solution, but it solved something I was working on recently
var myArray : float[];
var secs : float = 2.0;
function Start () {
InvokeRepeating("DelayAndDo", .01, secs);
}
function DelayAndDo () {
for (var value in myArray) {
//Do my thing here
}
}
Then you can cancel it at any time with CancelInvoke. I remember having some problems with using 0 for the start time which is why I use .01, though I can’t remember what the problem was and anyway it might have been fixed by now, but normally it doesn’t matter and it’s a habit by now so hey.
I’d actually recommend staying away from Update() as much as possible. At first it seems like a natural place to put stuff, but I’d say only use it if you really do want something to run every single frame without exception. Otherwise you end up going through various logic contortions to get things to work right (I know, I’ve been there ). Stick with coroutines and yield, InvokeRepeating, etc. Usually that makes things simpler and easier to understand.
InvokeRepeating is working beautifully for me driving character animation sync’d with audio (see thread here) but I want to use it for random stuff too (i.e. blinks), and I can’t seem to get that to work without putting it in Update:
function Update() {
if (!kill)
delay -= Time.deltaTime;
if(delay < 0) {
delay = Random.Range(0, maxDelay);
animation.CrossFadeQueued("blink",.5,QueueMode.PlayNow);
}
}
which seems to have a performance hit, and InvokeRepeating(“Blink”,.01,Random.Range(0,8.0)) doesn’t work, I guess the time argument can’t change once it’s set. So I’m back to Kahuna’s original question, but with random instead of fixed time intervals…
There shouldn’t be any noticeable performance hit with code that like; that’s quite trivial and is a reasonable solution. However, for the sake of being somewhat cleaner, I’d do something like:
function RandomBlinks () {
while (true) {
yield WaitForSeconds (Random.Range(.5, maxDelay));
animation.CrossFadeQueued("blink", .5, QueueMode.PlayNow);
}
}
Also by doing this you can make the blinking start and stop if you want by calling the function with Invoke (not InvokeRepeating in this case) and then CancelInvoke when you want to turn it off. (I don’t think the minimum in Random.Range should be zero…I don’t know about you but I can’t blink infinitely fast. )