Hello! Please choose your
desired language:
Dismiss

User data

Track user position and rotation

The Camera object exposes information about the user’s point of view in your scene.

  • camera.position returns a 3D vector with the user’s coordinates relative to the scene.
  • camera.rotation returns a quaternion with the user’s rotation.

Tip: You can also obtain the user’s rotation expressed in Euler angles (as an x, y and z vector) by writing camera.rotation.eulerAngles.

const camera = Camera.instance

class CameraTrackSystem {
  update() {
    log(camera.position)
    log(camera.rotation.eulerAngles)
  }
}

The example above logs the user’s position and rotation on each frame.

const camera = Camera.instance

class SomeSystem implements ISystem {
  entity: Entity
  constructor(entity: Entity) {
    this.entity = entity
  }

  update() {
    const transform = this.entity.getComponent(Transform)
    transform.rotation = camera.rotation
  }
}

const cube = new Entity()
const shape = new BoxShape()
const transform = new Transform()
transform.position.set(5, 1, 5)
cube.addComponent(shape)
cube.addComponent(transform)

engine.addEntity(cube)
engine.addSystem(new SomeSystem(cube))

The example above uses the user’s rotation to set that of a cube in the scene.

Get player’s public Ethereum key

You can obtain a user’s public Ethereum key by using getUserPublicKey(). You can then use this information to send payments to the user, or as a way to recognize users.

The example below imports the Identity library and runs getUserPublicKey() to get the public key of the user’s Ethereum account and log it to console. The user must be logged into their Metamask account on their browser for this to work.

import { getUserPublicKey } from "@decentraland/Identity"

const publicKeyRequest = executeTask(async () => {
  const publicKey = await getUserPublicKey()
  log(publicKey)
  return publicKey
})

class IDSystem implements ISystem {
  update() {
    if (publicKeyRequest.didFail) {
      log("error fetching id" + publicKeyRequest.error)
    } else {
      log("id:" + publicKeyRequest.result)
    }
  }
}

Note that we’re using an async function to run the getUserPublicKey() function, as it might take some time to retrieve this data. We’re then handling the data in a system, to be able to use it whenever it’s ready.

Get all user data

You can obtain a user’s display name and public Ethereum key by using getUserData().

The example below imports the Identity library and runs getUserData().

import { getUserData } from "@decentraland/Identity"

const userData = executeTask(async () => {
  const data = await getUserData()
  log(data.displayName)
  return data.displayName
})

NOTE: This won’t work when running a local preview, since you’re not authenticated while running a preview. But once the scene is deployed to Decentraland, the user’s data will be readable by this code.

Here we’re using an async function to run the getUserData() function, as it might take some time to retrieve this data. We’re then handling the data in a system, to be able to use it whenever it’s ready.

The getUserData() function returns the following information:

  • displayName: (string) The player’s user name, as others see in-world
  • userId: (string) A UUID string that identifies the player. If the player has a public key, this field will have the same value as the public key.
  • publicKey: (string) The public key of the player’s Ethereum wallet. If the player has no linked wallet, this field will be null.
  • hasConnectedWeb3: (boolean) Indicates if the player has a public key. True if the player has one.

Note: For any Ethereum transactions with the player, always use the publicKey field, instead of the userId.

Get user realm data

Players in decentraland exist in many separate realms. Players in different relms cant see each other, interact or chat with each other, even if they’re standing on the same parcels. Dividing players like this allows Decentraland to handle an unlimited ammount of players without running into any limitations. It also pairs players that are in close regions, to ensure that ping times between players that interact are acceptable.

If your scene sends data to a 3rd party server to sync changes between players in real time, then it’s important that changes are only synced between players that are on the same realm. You should handle all changes that belong to one realm as separate from those on a different realm. Otherwise, players will see things change in a spooky way, without anyone making the change.

import { getCurrentRealm } from '@decentraland/EnvironmentAPI'

executeTask(async () => {
  const currentRealm = await getCurrentRealm()
  text.value = `You are in the realm: ${JSON.stringify(currentRealm)}`
})

Decentraland handles its communications between players (including player positions, chat, messageBus messages and smart item state changes) through a decentralized network of communication servers. Each one of these servers can support multiple separate layers, each with a different set of players. Each one of these layers in a server is a separate realm.

The getCurrentRealm() function returns the following information:

  • displayName: (string) The full address of the relm, composed of the server + the layer
  • domain: (string) The URL of the server
  • serverName: (string) The name of the server
  • layer: (string) The name of the layer