Y
Y
Yugg02021-09-05 15:52:32
C++ / C#
Yugg0, 2021-09-05 15:52:32

How can I implement this in a List?

My project has: (This is a test code)

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

[System.Serializable]
public class TestEvent
{
    public int b;
}

public class TestScript : MonoBehaviour
{
    public List<TestEvent> testEvents;

    private int a = 3;

    private void Start()
    {
        
    }
}


In "Start()" I need the List to look for the value b equal to the value a.
And if it doesn't, then nothing happens.

Inspector:
6134bd0a048a8484532925.jpeg

Maybe someone knows a way, or an article \ manual that will help me.
If something is not clear, write, I will immediately answer.
Thank you.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
B
Boris the Animal, 2021-09-05
@YugGO

Only here it is necessary to use LINQ with care. Personally, I would not use it at all in the same Update or any other place where the code is often called.

using System;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;

[System.Serializable]
public class TestEvent
{
    [SerializeField] private int _b;

    public int B => _b;
}

public class TestScript : MonoBehaviour
{
    [SerializeField] private List<TestEvent> _testEvents;

    private int a = 3;

    void Start()
    {
        var events = _testEvents
            .Where(item => item.B == a)
            .ToArray();
    }
}

Same thing, but without LINQ. Purely show the principle, since this is an elementary task, and you did not do this, then you need to learn. If the number of elements in List _testEvents is always small, I would also create var results = new List(); with capacity set to _testEvents.Count. That is, so var results = new List(_testEvents.Count);
public class TestScript : MonoBehaviour
{
    [SerializeField] private List<TestEvent> _testEvents;

    private int a = 3;

    void Start()
    {
        var events = GetEvents(_testEvents, item => item.B == a);
    }

    private static List<TestEvent> GetEvents(
        IEnumerable<TestEvent> testEvents, Func<TestEvent, bool> predicate)
    {
        var results = new List<TestEvent>();
        foreach (var item in testEvents)
        {
            if (predicate(item))
            {
                results.Add(item);
            }
        }
        return results;
    }
}

I'll duplicate the solution from the comments:
public class TestScript : MonoBehaviour
{
    [SerializeField] private List<TestEvent> _testEvents;

    private int a = 3;

    void Start()
    {
        var element = GetFirstOrDefault(_testEvents, item => item.B == a);
        if (element is not null)
        {
            c = element.c;
            r = element.r;
            u = element.u;
        }
    }

    // Если совпадение не найдено, то вернуть значение по умолчанию.
    private static T GetFirstOrDefault<T>(IEnumerable<T> items, Func<T, bool> predicate)
    {
        foreach (var item in items)
        {
            if (predicate(item))
            {
                return item;
            }
        }

        return default;
    }
}

D
Denis Zagaevsky, 2021-09-05
@zagayevskiy

https://docs.microsoft.com/ru-ru/dotnet/api/system...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question