#Implementations of Func and generics

1 messages · Page 1 of 1 (latest)

coral matrix
#

Hello! Me again.

I really need advice on using Func and generics.

I know generics are most often used when working with files. But is there a way to implement them in game logic? I'm just interested in different examples of their use, as I'm trying to improve my code.

Also about Func. Where is the best place to use them?

I know what these keywords are, but I can't find a good use for them. (I've only used them a couple of times in reloading a player's attack, and Func is more of a code readability simplification.)

mortal talon
#

generics can be real powerful

coral matrix
#

I said "most", not only.

mortal talon
#

Think about a singleton system, where you only need to write the functionality once

#

Multiple different objects can be a singleton but you only need to write it once and use generic to pass through the object

#

or Pooling system etc.

#

a state machine system? maybe multiple objects share the same behaviour you can easily pass them in with generic

coral matrix
#

Shit, I forgot about Singletones...
My statemachine is suffering because of not using it...

Maybe there I can experiment with it.

mortal talon
#

Damage system ?

public interface IDamageable{
    void TakeDamage(int amount);
}

public class DamageSystem<T> where T : IDamageable{
    public void DealDamage(T target, int amount){
        target.TakeDamage(amount);
    }
}```
player, enemy, obstacle etc..
#

its kinda hard to narrow down cause they can be used in a variety of situations

#

if you use a List (most collections) you're working with Generics.
GetComponent etc. they're pretty much everywhere, they allow you to keep systems modular

coral matrix
#

Yes, I know it but yeah...

I understand its kinda hard to explain

mortal talon
#

I think with time you just start to use them more and see their true potential and usecases. Start small

coral matrix
#

It is almost impossible to understand programming without practice

Exactly

mortal talon
#

yup. Its mostly on a usecase basis.
until you need to use something and a lot you won't really know it well

#

try small systems with them.
saving is a good start too

#

a generic that can write data to file as you said earlier, its a good usecase

coral matrix
#

Okay, maybe I will try to think about it.

And maybe can you show some examples with Func, please?

mortal talon
#

a very basic example, what if you want to perform an action at the end of the timer ?

#

instead of being constrained to 1 specific function you can pass a function in

#

I usually use an Action, unless you need to return a value

#
  IEnumerator TimerCoroutine(float duration, Action onComplete){
        yield return new WaitForSeconds(duration);
        onComplete?.Invoke();
    }```
```cs
StartCoroutine(TimerCoroutine(2f, () => player.TakeDamage(10)));```
#
IEnumerator TimerCoroutine(float duration, Func<bool> condition){
    yield return new WaitForSeconds(duration);
    if (condition()) // only runs if Func returns true
        Debug.Log("condition met after timer");
}

StartCoroutine(TimerCoroutine(3f, () => player.Health < 50));```
coral matrix
#

Excellent

Now I understand how I could try using something like this in a project.

I thank you for helping with examples :)

mortal talon
#

small use cases and build up
you got this

coral matrix
# mortal talon small use cases and build up you got this

public static T Give_Randomnized_Object<T>(Func<float> total_count, params (float chance, T data)[] chances)
        {
            float total_weight = total_count();
            float random_param = UnityEngine.Random.Range(0, total_weight);

            float current_value = 0;
            foreach (var (chance, data) in chances)
            {
                current_value += chance;
                if (random_param <= current_value)
                {
                    return data;
                }
            }
            return default;
        }

I did something like this(2 weeks ago)
Just wanted to share what its actually cool thing. Really helps in singletons and generic scripts.

Thanks :3