Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Change Value of Variable for duration of time.

Discussion in 'Scripting' started by rothpletz5, Jun 27, 2021.

  1. rothpletz5

    rothpletz5

    Joined:
    Dec 2, 2020
    Posts:
    27
    Hello,

    I am working on a potions system for my game. I am hoping to use a function to change a variable, wait a duration of time, then return that variable back to its original value. For example, the function could increase the Player speed by 25% (player speed*1.25), wait five seconds, then return to normal speed (player speed /1.25).

    Here's what this looks like in code:
    Code (CSharp):
    1. void applyEffect(float target, float value, float duration)
    2.     {
    3.         float TimeElapsed;
    4.  
    5.         target *= value;
    6.  
    7.         ///need this part to repeat
    8.         TimeElapsed += Time.deltaTime;
    9.         ////////
    10.         if (TimeElapsed > duration) {
    11.             target/= value;
    12.         }
    13.     }
    As You can see, this does not work, as applyEffect will only be called once, so TimeElapsed will only update once. I would need to get that middle chunk of code to update constantly for this to work.

    What is the best way to achieve this? I am considering coroutines, but am stumped, since you cannot pass variables in or out of them. Any working result would have to work for any
    float target
    (such as speed, damage, critRate,etc), and would have to allow for multiple effects to be run at the same time ( for example, if the player drinks both speed and strength potions).
    If there is any way to make this work, or a better way of going about this, please let me know!

    Thanks so much and have a fantastic day!
     
    Last edited: Jun 27, 2021
  2. Hikiko66

    Hikiko66

    Joined:
    May 5, 2013
    Posts:
    1,302
    You can pass delegates as arguments, and they can get and set your variable

    Code (CSharp):
    1. using System;
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5.  
    6. public class UpdateField : MonoBehaviour
    7. {
    8.     //public so that we can watch the effect the coroutine has on this variable in the inspector
    9.     public float value;
    10.  
    11.  
    12.     void Start()
    13.     {
    14.         StartCoroutine(GetSetOverTime(() => { return value; }, (val) => { value = val; }, 20f));
    15.     }
    16.  
    17.     private IEnumerator GetSetOverTime(Func<float> getValue, Action<float> setValue, float duration)
    18.     {
    19.         float timeElapsed = 0f;
    20.  
    21.         while (timeElapsed < duration)
    22.         {
    23.             timeElapsed += Time.deltaTime;
    24.  
    25.             setValue.Invoke(getValue.Invoke() + Time.deltaTime);
    26.  
    27.             yield return null;
    28.         }
    29.     }
    30. }
    31.  
    But let us know if you don't understand delegates, or anonymous function syntax, or coroutines. There are better ways of explaining them. This is short hand and has no comments
     
    Last edited: Jun 27, 2021
    rothpletz5 likes this.
  3. rothpletz5

    rothpletz5

    Joined:
    Dec 2, 2020
    Posts:
    27
    Thanks so much, I should be able to use something like this! However, I'm not familiar with that anonymous function syntax, and my knowledge of delegates is limited. Are there some tutorials you could link?
     
  4. Ryiah

    Ryiah

    Joined:
    Oct 11, 2012
    Posts:
    20,134
    Here's one aimed at Unity developers.

     
    rothpletz5 likes this.