Home > OS >  Adding a timer to the code and then looping it
Adding a timer to the code and then looping it

Time:05-20

Trying to find a way to add a timer to my code and then continuously loop it with the timer. e.g Trying to craft items with a button click and then waiting 5 seconds for it to craft before it automatically begins to craft again and so on as long as I have the materials. Ive looked around on tutorials but havent been able to find what ive been looking for. This is the code I want to loop:

    public double copper;
public double copperBar;
public double copperBarValue;
public double multiplier;

public void Start()
{
    copperBar = 0;
    copperBarValue = 5;
    copper = 0;
    multiplier = 1;
}

    **public void FurnaceInteraction()
{
    if (copper >= copperBarValue)
    {
            copper -= copperBarValue;
            copperBar  = 1 * multiplier;
    }
}**

CodePudding user response:

This will completely solve your problem. You must place only one condition in while.

private void Start() => StartCoroutine(Run());
public bool youHaveMaterials;
public IEnumerator Run()
{
    while (youHaveMaterials) // repeat time until your materials end
    {
        Debug.Log("Do Crafting..");
        yield return new WaitForSeconds(5);
    }
}

IEnumerator is a time-based function that can support wait times itself. While also returns the code as long as the condition is met. The combination of wait and `while causes the creator to create the item each time the condition is met and then wait 5 seconds. It rebuilds from where you still have the material.


For Example..

In the code below, with 2 irons, we can also make 2 swords. Just Run StartCoroutine when your character is going to the forge for e.g..

private void Start() => StartCoroutine(CraftSword());

public int Iron = 2;
public IEnumerator CraftSword()
{
    Debug.Log("Start Crafting..");
    while (Iron > 0)
    {
        Iron--;

        Debug.Log("Sword Created!!"   "Remaining Iron: "   Iron);

        if (Iron == 0) break;

        yield return new WaitForSeconds(.5f);
        
        Debug.Log("Wait 0.5 second.");
    }
    Debug.Log("My Irons End..");
}

enter image description here

CodePudding user response:

public void Start()
{
    StartCoroutine(Timer());
}

IEnumerator Timer(){
    print("timer started and will wait 5 seconds");
    yield return new WaitForSeconds(5);
    print("timer finished after 5 seconds");
}

CodePudding user response:

You can use WaitForSeconds like Daniel suggests, or you can create your own Timer method using the Update method and Time.deltaTime.

Here is an example.

//tracks if we are looping or not
bool isLooping=false;

//holds the number of times left to loop
int loopTimes;

//holds the current time left
float timeLeft;

//wait 5 seconds
static float waitTime = 5;

private void Update()
{
    if (isLooping)
    {
        if (timeLeft>0)
        {
            timeLeft -= Time.deltaTime;
            RunFunctions();
        }
        else
        {
            loopTimes--;
            if (loopTimes > 0)
            {
                //print("loop again");
                timeLeft = waitTime;
            }
            else
            {
                //print("finished");
                isLooping = false;
            }
        }
    }
}

void RunFunctions() { }

public void StartLooping()
{
    //print("start looping");
    isLooping = true;
    loopTimes = 5;
    timeLeft = waitTime;
}
  • Related