> For the complete documentation index, see [llms.txt](https://docs.decentraland.org/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.decentraland.org/creator/content-creator-es/scenes-sdk7/patrones-de-programacion/mutable-data.md). Agents can also search and read this documentation through its [MCP server](https://docs.decentraland.org/~gitbook/mcp).

# Datos mutables

Aprende cómo manejar datos de solo lectura y mutables de los componentes

Al hacer referencia a datos de un [component](https://docs.decentraland.org/creator/content-creator-es/scenes-sdk7/arquitectura/entities-components.md), puedes obtener la versión mutable o la de solo lectura (inmutable).

Siempre que sea posible, trabaja con las versiones de solo lectura de los datos. Esta práctica puede mejorar significativamente el rendimiento de tu escena en comparación con trabajar siempre con versiones mutables de esos mismos datos.

La función `.get()` de un componente devuelve una versión de solo lectura (inmutable) del componente. Solo puedes leer sus valores, pero no cambiar ninguna de sus propiedades.

La función `.getMutable()` devuelve una versión del componente que permite cambiar sus valores. Usa versiones mutables solo cuando tengas previsto modificar un componente; de lo contrario, usa siempre `get()`.

```ts
// obtener una versión de solo lectura (inmutable)
const immutableTransform = Transform.get(myEntity)

// lo siguiente NO funciona:
// immutableTransform.position.y = 2

const mutableTransform = Transform.getMutable(myEntity)

// la siguiente línea SÍ cambia la posición de la entidad
mutableTransform.position.y = 2
```

Una buena práctica consiste en recorrer los componentes de solo lectura para comprobar sus valores y obtener la versión mutable de un componente individual solo cuando sea necesario modificarlo.

```ts
// altura máxima codificada directamente
const MAX_HEIGHT = 10

// Definir el sistema
function HeightLimitSystem(dt: number) {
	// recorrer todas las entidades que tienen un componente Transform
	for (const [entity] of engine.getEntitiesWith(Transform)) {
		// obtener valores de solo lectura
		const currentHeight = Transform.get(entity).position.y

		// comparar valores
		if (currentHeight > MAX_HEIGHT) {
			// obtener la versión mutable para realizar un cambio
			const mutableTransform = Transform.getMutable(entity)

			// cambiar el Transform
			mutableTransform.position.y = MAX_HEIGHT
		}
	}
}

// Añadir el sistema al engine
engine.addSystem(HeightLimitSystem)
```

En el ejemplo anterior, un sistema comprueba los valores de solo lectura del componente `Transform` de una entidad. En cada tick, comprueba si la posición *y* supera una altura máxima codificada directamente. Solo si la altura del Transform supera este límite, obtenemos la versión mutable del Transform. Puede parecer que esto supone trabajo adicional para la escena, pero en una escena donde comprobamos valores en cada tick del bucle del juego y solo hacemos cambios de vez en cuando, se obtienen enormes mejoras de rendimiento.

Esta práctica sigue los principios de la [programación orientada a datos](https://docs.decentraland.org/creator/content-creator-es/scenes-sdk7/arquitectura/data-oriented-programming.md). Además, se está adoptando gradualmente como práctica estándar en la industria de los videojuegos, debido a la gran mejora que aporta.

{% hint style="warning" %}
**📔 Nota**: En las versiones anteriores del SDK (6.x o anteriores), los componentes siempre se trataban como mutables. Ese patrón puede ser algo más sencillo de aprender, pero era mucho menos eficiente en tiempo de ejecución.
{% endhint %}


---

# 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.decentraland.org/creator/content-creator-es/scenes-sdk7/patrones-de-programacion/mutable-data.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.
