I have a question about Invokerepeating.

I have a invoke thingy that repeates here it is:
InvokeRepeating(“Walk”, 1, Frequencywalk - 0.66);

Frequencywalk is set to 1 in a var, but since this invoke is in start, if I edit the variable, nothing happens at all. So I figured it will work under the function update. I put it there, and it either makes it sound once, or not at all…
How would I make it so if I update the variable, it will update?

2 Answers

2

Try using Invoke instead:

float FrequencyWalk = 5f;

void Start()
{
    Invoke("Walk", FrequencyWalk);
}

void Update()
{
    FrequencyWalk += someModifierValue;
}

void Walk()
{
    //do stuff
    if(stopInvoking) 
        return;
    Invoke("Walk", FrequencyWalk);
}

In this case, every time invoke gets called it will get set with a new time to delay. If you are doing this, you’ll need a way to stop the invoke loop from happening, so make some sort of case that will break the loop.

I do not get it... So.... Ugg I cant comprehend this. I know what update is doing, and start. But what does the stuff insald walk do? also I am using JavaScript.

inside the void Walk(), it simply invokes itself after 'FrequencyWalk' seconds unless stopInvoking is True

"Unless it is strue" What, It shows I it is true then do it. I still don't understand, so it invokes itself, then it loops?

Oh it works! thanks for help you two. also one more question, I have a line that does this: Frequencywalk = 0.8F; and the var doesn't change to that! I don't think is will go into decimals for some odd reason, also here is the var. var Frequencywalk : float = 1.1;

where does the line of code: FrequencyWalk = 0.8 reside? Are you sure it is getting called? Also, if the main question is answered, don't forget to mark it as solved.

Sounds like you are trying to do something that invoke repeating was probably not made for. Dynamic repeating durations. You could try out Coroutine and then use your FrequencyWalk variable inside of there. This should be able to accomplish what you want to do if I understand it correctly. If you are still having trouble or if I didn’t understand your problem correctly, let me know.

Here’s an example:

 public void WalkCoroutine()
{
    while (true)
    {
        float timer = 0.0f;
        while (timer < FrequencyWalk - .66)
        {
            timer += Time.deltaTime;
            yield return null;
        }
        Walk();
    }
}

Once this coroutine is started {StartCoroutine(“WalkCoroutine”)} it will only be able to be stopped with StopCoroutine(“WalkCoroutine”). This is a very messy implementation but it will allow you to edit the variable “FrequencyWalk” and have it change the duration in real time.