# Get() Methods

{% hint style="info" %}
Example code here is for a `GameStatList`, though similar methods are on other list classes as well.

Methods for specific list types are below.
{% endhint %}

{% hint style="info" %}
Methods which return multiple objects will return an `IEnumerator<T>` (type depenent on the list you're dealing with), and you can convert that to a list or an array.

<pre class="language-csharp"><code class="lang-csharp"><strong>// List
</strong><strong>List&#x3C;GameStat> duplicateGameStats = gameStatList
</strong><strong>    .GetDuplicatesOfType("someType")
</strong><strong>    .ToList();
</strong>
// Array
GameStat[] duplicateGameStats = gameStatList
    .GetDuplicatesOfType("someType")
    .ToArray();
    
// Additional work before making it an Array
GameStat[] duplicateSwords = gameStatList
    .GetDuplicatesOfType("Weapon")
    .Where(x => x.ObjectName()
    .Contains("Sword"))
    .ToArray();
</code></pre>

{% endhint %}

{% hint style="info" %}
The common methods can be found in `GameModuleListExtensions.cs`
{% endhint %}

## `Get()`

Each list will have similar `Get()` methods. Dig into the `Game[OBJECT]List.cs` code to learn more.

The `Get()` methods have the additional `addIfNull` option. If true, the a new Game Module object will be created and returned, if it does not already exist.

```csharp
// Get a stat, and add if not already there
GameStat gameStat = gameStatList.Get(stat, true); // Pass in a "Stat" object
GameStat gameStat = gameStatList.Get(gameStat, true); // Pass in a GameStat object
GameStat gameStat = gameStatList.Get(uid, true); // Pass in the uid of a Stat

// Get by an ObjectType()
GameStat gameStat = gameStatList.GetByObjectType(objectType, true); // Reuturns just the first object of this type
```

## `TryGet()`

The `TryGet()` methods will return `true` or `false` if the item exists, and return the item to an `out` variable if it does. These methods will NOT add the Game Modules object to the list if it does not already exist.

```csharp
// TryGet -- multiple versions accept a Stat, GameStat, or Uid
if (gameStatList.TryGet(stat, out var foundGameStat))
    Debug.Log($"Got {foundGameStat.objectName}");

if (gameStatList.TryGet(gameStatt, out var foundGameStat))
    Debug.Log($"Got {foundGameStat.objectName}");

if (gameStatList.TryGet(uid, out var foundGameStat))
    Debug.Log($"Got {foundGameStat.objectName}");
    
// TryGet by ObjectType()
if (gameStatList.TryGetByObjectType(objectType, out var foundGameStat))
    Debug.Log($"Got {foundGameStat.objectName} which is a {objectType}");
```

## `GetByGameId()` & `TryGetByGameId()`

The runtime Game Module objects, such as `GameStat`, `GameCondition`, and `GameItemObject`, each have a unique `GameId()` which is created at runtime, and will stay the same even as the object is transferred.

This unique string identifies the specific instanace of the object in the game, opposed to the `Uid()` which is a unique string which identifies the Scriptable Object object, such as `Stat`, `Condition`, `ItemObject`.

In situations where a list may have multiple instances of a the same type of object, you may wish to `Get()` a specific instance, rather than obtaining the first found.

```csharp
var specificGameStat = gameStatList.GetByGameId(gameId);

if (gameStatList.TryGetGameId(gameId, out var specificGameStat))
    Debug.Log($"Found the specific gameStat with gameId {specificGameStat.GameId()});
```

## `First()` & `Last()`

While it's not difficult to get the first or last item in a list, we have helper methods for that as well.

```csharp
Debug.Log($"The last GameStat added is {gameStatList.Last().objectName}");
Debug.Log($"The first GameStat in the list is {gameStatList.First().objectName}");
```

## `GetAll()`

These will return an array of the type being handled. The array may be empty if there are none to return.

```csharp
GameStat[] GameStatsOf(string uid) => gameStatList.GetAll(uid);
GameStat[] GameStatsOfType(string objectType) => gameStatList.GetAllOfType(objectType);
```

## Additional Methods

```csharp
// Get array of ObjectTypes represented in the list
var allObjectTypes = gameStatList.GetObjectTypes.ToArray();

// Get list of all items that are duplicates, ignoring the first of each
var duplicateStats = gameStatList.GetDuplicates.ToList();
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://infinitypbr.gitbook.io/magic-pig-games/game-modules-4/module-documentation/game-module-lists/get-methods.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
