Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't figure what lambda expression does stand for

I'm learning OOP in Unity and i found game with code build with oop, the problem is that i can't figure out what does line "private bool IsInitialized => _bulletPool != null;" stand for.

I've read about lambda expression and as i understood its used like an UnityAction or for writing shorter functions.

using System;
using UnityEngine;
using UnityEngine.Events;

[RequireComponent(typeof(CapsuleCollider2D))]
[RequireComponent(typeof(ShipAudio))]
public abstract class Ship : MonoBehaviour
{
[SerializeField] private Vector2 _shootDirection;

private BulletPool _bulletPool;

protected CapsuleCollider2D Collider;

private bool IsInitialized => _bulletPool != null; //this line

public event UnityAction Shooted;
public event UnityAction TookDamage;
public event UnityAction Died;

private void Awake()
{
    if (_shootDirection == Vector2.zero)
        throw new ArgumentException(nameof(_shootDirection));

    Collider = GetComponent<CapsuleCollider2D>();
    Collider.isTrigger = true;
}

public void Init(BulletPool bulletPool)
{
    _bulletPool = bulletPool;
}

public void TakeDamage()
{
    OnTakeDamage();
    TookDamage?.Invoke();
}

public void Die()
{
    OnDie();
    Died?.Invoke();
}

public void Shoot()
{
    if (IsInitialized == false)
        throw new Exception("Impossible to shoot. Bullet pool is not stated");

    if (_bulletPool.TryGetObject(out Bullet bullet) == false)
        throw new Exception("Impossible to spawn bullet");

    bullet.Show();
    bullet.transform.position = transform.position;
    bullet.Init(_shootDirection);

    Shooted?.Invoke();
}

protected abstract void OnTakeDamage();

protected abstract void OnDie();
}

1 Answers

It's a shortcut syntax for a function or property getter.

private bool IsInitialized => _bulletPool != null;

Does exactly the same thing as this.

private bool IsInitialized
{
    get
    {
        return _bulletPool != null;
    }
}
like image 162
Jonathan Wood Avatar answered Aug 25 '26 08:08

Jonathan Wood



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!