> For the complete documentation index, see [llms.txt](https://docs.flatredball.com/gum/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.flatredball.com/gum/code/standard-visuals/textruntime/getcharacterindexatposition.md).

# GetCharacterIndexAtPosition

## Introduction

The `GetCharacterIndexAtPosition` method returns the character index under the cursor.

The returned index is the index into a string obtained by combining all strings in `WrappedText`, so additional logic is needed to find the character if the text is not all displayed in one line.

This method can also be used on text which includes BBCode, returning an index after BBCode has been removed.

## Code Example: Printing Character at Position

The following code shows how to use the Gum cursor to hover over a TextRuntime and display the character under the cursor.

```csharp
// Class scope
TextRuntime textRuntime;

protected override void Initialize()
{
    GumUI.Initialize(this);

    textRuntime = new TextRuntime();
    textRuntime.AddToRoot();

    textRuntime.Text = 
        "I am a text runtime which should line wrap because my " +
        "width is set to an absolute 200 units";

    textRuntime.Width = 200;
    textRuntime.WidthUnits = Gum.DataTypes.DimensionUnitType.Absolute;

    textRuntime.Anchor(Anchor.Center);

    textRuntime.HorizontalAlignment = HorizontalAlignment.Left;
    textRuntime.VerticalAlignment = VerticalAlignment.Top;

    base.Initialize();
}


protected override void Update(GameTime gameTime)
{
    GumUI.Update(gameTime);

    var cursor = GumUI.Cursor;
    var x = cursor.XRespectingGumZoomAndBounds();
    var y = cursor.YRespectingGumZoomAndBounds();

    if(textRuntime.HasCursorOver(GumUI.Cursor))
    {
        var innerText = (RenderingLibrary.Graphics.Text)textRuntime.RenderableComponent;

        int characterIndex = innerText.GetCharacterIndexAtPosition(x, y);

        if(characterIndex != -1)
        {
            int indexLeft = characterIndex;

            // We need to loop through the wrapped text to see which line we're on:
            foreach(var line in textRuntime.WrappedText)
            {
                if(line.Length > indexLeft)
                {
                    Debug.WriteLine($"At character {characterIndex}");
                    Debug.WriteLine($"Character is {line[indexLeft]}");
                    break;
                }
                else
                {
                    indexLeft -= line.Length;
                }
            }
        }
    }
    base.Update(gameTime);
}
```

<figure><img src="/files/kx8C02cKX7fDBGE46gT8" alt=""><figcaption><p>Cursor hovering over characters, and the character is printed out to the debug window</p></figcaption></figure>


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## 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, and the optional `goal` query parameter:

```
GET https://docs.flatredball.com/gum/code/standard-visuals/textruntime/getcharacterindexatposition.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

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.
