# Basic Integration

As mentioned in the [setup](/overview/installation-and-setup.md#additional-files) section, game studios will be required to create 2 files in order for the NRN Agent to "understand" the game world, and for the game world to "understand" the NRN Agent output. The NRN team will provide partner game studios with template files for Javascript, Unity, and Unreal.

<figure><img src="/files/3zZNZs0ZkjJLnjWowAgZ" alt=""><figcaption><p>Red indicates what is understandable for the game, while blue indicates what is understandable for NRN Agent</p></figcaption></figure>

After instantiating the NRN Agent, the basic flow for integration is as follows:

* Convert the game world into an state space that the NRN model can use
* **Human Controlled**
  * Update human inputs
  * Convert executed inputs to an action that the NRN model can understand
  * Add the state and action pair to the dataset
* NRN **Agent Controlled**
  * Perform inference to select the action
  * Convert the selected action to an input that the game can understand
* Either send data to the trainer platform or train directly in-game

Below we show how to initialize the model and use it at the game manager level:

{% tabs %}
{% tab title="Javascript" %}

```javascript
class Game {
    // Instantiate agent
    constructor(agentInputs) {
        this.agent = AgentFactory.createAgent(...agentInputs);
    }
    
    // Game loop update
    update() {
        // Get the state space for the model
        const state = getStateSpace(this);
        
        if (this.human) {
            // If human is controlling, then update their inputs
            this.inputs.p1.update();
            
            // Convert the inputs into something the NRN agent can understand
            const action = getActionOneHot(this.inputs.p1);
            
            // Add the observed state and action to the dataset
            this.agent.collect({ state, action });
        }
        else {
            // If NRN agent is controlling, then perform inference
            const agentAction = this.agent.selectAction(state);
            
            // Execute the action in-game
            convertActionToGame(agentAction);
        }
    }
}
```

Then at the end of the game, you can send the data you collected to the trainer platform as follows:

```javascript
await agent.uploadData();
```

OR

If you have the API access to train in-game, then you can directly train within the game loop as follows:

```javascript
await agent.train(model.getTrainingData(), trainingConfiguration);
```

{% endtab %}

{% tab title="C# - Unity" %}

```csharp
public class Game : MonoBehaviour 
{
    // Define the agent that will be taking actions
    public IAgentRL Agent { get; set; }

    // Instantiate model
    private async void _loadAgent(modelInputs)
    {
        Agent = AgentFactory.CreateAgent<IAgentRL>("reinforcement", modelInputs);
    }
    
    // Game loop update
    private async void Update() {
        // Create the world representation which we will convert to the state space matrix
        var world = new NrnIntegration.NrnWorld 
        {
            // All world attributes/objects that are relevant for the AI
        };
        
        // Get the state space for the model
        Matrix state = NrnIntegration.NrnStateSpace.GetState(world);
        
        if (human) 
        {
            // Convert the inputs into something the NRN agent can understand
            Dictionary<string, bool> action = NrnActionSpace.ConvertInputToBools();
            
            // Add the observed state and action to the dataset
            Agent.Collect({ state, action });
        }
        else {
            // If NRN agent is controlling, then perform inference
            Dictionary<string, bool> agentAction = Agent.SelectAction(state);
            
            // Execute the action in-game
            NrnActionSpace.ConvertActions(agentAction);
        }
    }
}
```

Then at the end of the game, you can send the data you collected to the trainer platform as follows:

```csharp
await Agent.UploadData();
```

OR

If you have the API access to train in-game, then you can directly train within the game loop as follows:

```csharp
await AgentArc.Train();
```

{% endtab %}
{% endtabs %}

{% hint style="info" %}
The NRN team will work with each game studio on creating an optimal set of training configurations for their games
{% endhint %}


---

# 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://docs.nrnagents.ai/getting-started/basic-integration.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.
