> 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). Agents can also search and read this documentation through its [MCP server](https://docs.flatredball.com/gum/~gitbook/mcp).

# 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.

Every backend counts a CRLF line break as one character, so `WrappedText` never contains the `\r`. If your own string uses CRLF line breaks, the index drifts by one character per line break when mapped back into it.

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

The position is in the same units the text is drawn in, so the method accounts for `FontScale`.

To find which BBCode tag is under that index, pass it to `GetTagAtIndex`. See [Combining With GetTagAtIndex](#combining-with-gettagatindex).

## 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;

// Initialize
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;

// Update
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;
            }
        }
    }
}
```

<figure><img src="https://2695663588-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-M_fzQmxQ1VeUFHcoo2c%2Fuploads%2Fgit-blob-25f7ec9bb6df89ccaf5caa3f316c2410c8282783%2F25_07%2013%2042.gif?alt=media" alt=""><figcaption><p>Cursor hovering over characters, and the character is printed out to the debug window</p></figcaption></figure>

## Combining With GetTagAtIndex

`TextRuntime.GetTagAtIndex(tagName, index)` returns the BBCode tag with the given name that contains a character index, or `null` if there is none. It uses the same index `GetCharacterIndexAtPosition` returns, so the two combine to find the tag under the cursor. This is how a [Link tag](https://docs.flatredball.com/gum/code/standard-visuals/textruntime/bbcode.md#links) becomes clickable:

```csharp
// Update
var cursor = GumUI.Cursor;
if (textRuntime.HasCursorOver(cursor))
{
    int index = textRuntime.GetCharacterIndexAtPosition(
        cursor.XRespectingGumZoomAndBounds(),
        cursor.YRespectingGumZoomAndBounds());

    TextTagSpan? link = textRuntime.GetTagAtIndex("Link", index);
    if (link != null)
    {
        // link.Value.Argument is the id, such as "shop" for [Link=shop]
    }
}
```

`GetCharacterIndexAtPosition` returns the nearest caret position, not the character under the point. A point past the end of the text returns the text length, and a point on the right half of a character returns the index after it. For a tag, that means the right half of its last letter gives an index equal to `EndIndex`, and `GetTagAtIndex` returns `null` there because `EndIndex` is exclusive. Check that the cursor is over the text first, as the sample does, and expect a click on the right half of a link's last letter to miss.


---

# 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 by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

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

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

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.
