dot big bang

Developer API
Menu

Class Game<Generics>

All
  • Public
  • Public/Protected
  • All

Type parameters

  • Generics: { saveData: SaveData<any, any> }

Hierarchy

  • Game

Index

Properties

Readonly audio

audio: Audio

Audio contains methods for manipulating the audio context of the game, and for playing back Sounds.

cameraEntity

cameraEntity: null | Entity

The Entity in the Game that is used as camera for rendering the scene. The Entity must have a CameraComponent. If it doesn't or if this property is set to null, the scene will be rendered as if seen from a camera at an arbitrary position near the origin of the world.

Readonly input

input: Input

The Input contains the current state of the input devices connected to the local machine: the keyboard, mouse, gamepads as well as a simplified generic controls object.

Readonly renderer

renderer: Renderer

The Renderer contains all the settings that controls how the Game is rendered in 3D: the lights, colors, post-process effects, etc...

Readonly session

session: Session

The Session contains information about the multiplayer/network aspect of the Game, like the list of connected Peers and their Users.

Readonly ui

ui: UI

The UI (i.e user interface), lets you create visual elements in screen-space, like a heads up display.

Accessors

entities

  • The list of all the Entities in the Game.

    Returns Entity[]

frameDeltaTime

  • get frameDeltaTime(): number
  • The time difference, in seconds, between the start of the current frame and the previous one. As an example, a game running at 60 fps has a typical frame delta time of 0.016s.

    Returns number

frameTime

  • get frameTime(): number
  • The time at which the current frame started. This is expressed in seconds since the game started the first time (this time origin isn't affected when restarting the game in edit mode)

    Returns number

numEntities

  • get numEntities(): number
  • The number of Entities in the Game.

    Returns number

playerEntity

  • get playerEntity(): null | Entity
  • The Entity in the Game that represents the local player, or null if there's none. This is typically the user avatar's SkeletalObject. Internally, the playerEntity is the first Entity found in the Game with the 'player' tag.

    Returns null | Entity

saveData

  • get saveData(): Generics["saveData"]
  • A game-wide save data storage bucket specific to the current user. You can use this to persist data from one game session across to another.

    Think of it like a save file.

    Example usage:

    // `data` can be any JSON-serializable value you like.
    let data = { checkpoint: "house1", unlocks: ["bat", "hatchet"] };
    
    // When you are ready to save the user's progress:
    this.game.saveData.write(data);
    
    // And then, to read the data back out later:
    data = this.game.saveData.read();
    

    For more information, see the documentation for SaveData.

    Returns Generics["saveData"]

Methods

addEntity

  • addEntity(entity: Entity, networked?: boolean, networkTransform?: boolean): void
  • Adds the given Entity to the Game. A typical use case is to add a new Entity that has just been created either by hand (using the Entity constructor) or from a Template resource. Another use case it to re-add an Entity that previously got removed.

    Parameters

    • entity: Entity

      The Entity to add

    • Optional networked: boolean

      If true, the Entity will be replicated over the network on all the other Peers in the Session. If false, the Entity will only exist on the local Peer.

    • Optional networkTransform: boolean

      If true, the transform of the Entity will automatically be replicated over the network to all the other Peers in the Session. This can be true only if the networked argument is true.

    Returns void

addEntityFromTemplate

  • Creates an Entity from the given Template and add it to the Game.

    Parameters

    • template: Template

      The Template from which to create the Entity that gets added to the Game.

    • Optional networked: boolean

      If true, the Entity will be replicated over the network on all the other Peers in the Session. If false, the Entity will only exist on the local Peer.

    • Optional networkTransform: boolean

      If true, the transform of the Entity will automatically be replicated over the network to all the other Peers in the Session. This can be true only if the networked argument is true.

    • Optional position: Vector3

      If provided, use this Vector3 as the position of the newly created Entity. If not, use the position from the Template.

    • Optional rotation: Euler

      If provided, use this Euler as the rotation of the newly created Entity. If not, use the rotation from the Template.

    • Optional scale: Vector3

      If provided, use this Vector3 as the scale of the newly created Entity. If not, use the scale from the Template.

    Returns Entity

canvasToWorld

  • Converts the point expressed in canvas space, i.e in CSS pixels from the top-left corner, into world space. The conversion uses the CameraComponent of this.game.cameraEntity. If this is null, or if the cameraEntity doesn't have a CameraComponent, the method returns null. Be aware that this method will still return a world point even if the point isn't visibile in the canvas/camera, that is to say if the given canvas point lies outside the canvas rectangle (this can happen with mouse coordinates when left-click dragging outside the window) or if the the provided NDC z coordinate is outside the [-1, 1] range. This is a helper method, internally it takes the canvas point, converts it to clip space using game.renderer.canvasToClipSpace(), and convert the clip space point to world space using CameraComponent.clipSpaceToWorld().

    ➡️ See usage examples in the API Demo: Game canvas functions world.

    Parameters

    • canvasPoint: Vector2

      The canvas point, in CSS pixels from the top-left corner, to convert to world space.

    • Optional ndcZ: number

      The optional Z coordinate to use for the intermediate NDC (normalized device coordinate) point. A value of -1 places it on the camera near plane, while a value of 1 places it on the far plane.

    • Optional optionalTarget: Vector3

      When provided, return the world point into this Vector3 instead of allocating one. Be aware that even if you provide this argument, you should still check the return value for null.

    Returns null | Vector3

createRayFromCanvasPoint

  • createRayFromCanvasPoint(canvasPoint: Vector2, optionalTarget?: Ray): Ray
  • Creates a Ray in world space which origin is the given canvas point (converted in world space on the near plane) and which direction is the vector going from the camera to that point, normalized. The CameraComponent used for the calculations is the one currently defined by this game.cameraEntity. If this is null, or if the cameraEntity doesn't have a CameraComponent, a default Ray is returned.

    ➡️ See usage examples in the API Demo: Game canvas functions world.

    Parameters

    • canvasPoint: Vector2

      The point in canvas space, in CSS pixels from the top-left corner, from which to create the Ray.

    • Optional optionalTarget: Ray

      When provided, the ray is returned in this Ray object instead of allocating a new one.

    Returns Ray

getEntitiesByTag

  • getEntitiesByTag(tag: string): Entity[]
  • Gets the Entities of the Game with the given tag.

    ➡️ See usage examples in the API Demo: Game tag functions world.

    Parameters

    • tag: string

      The tag the Entities to get should have.

    Returns Entity[]

getEntitiesWithColliderIntersectingCapsule

  • getEntitiesWithColliderIntersectingCapsule(capsuleAxis: Line3, capsuleRadius: number, collisionLayerMask?: number): ColliderIntersection[]
  • Gets the Entities of the Game which CollisionComponent's collider intersects with the given capsule volume (i.e. a cylinder with hemispherical ends). The result is provided as an array of ColliderIntersection objects. Each one tells which Entity has been hit and where exactly in the capsule it happened. The ColliderIntersection objects in the resulting array are sorted by increasing distance to the start point of the capsule axis. Use the Game.getEntitiesWithVoxelObject-flavored static methods to intersect against the visual VoxelObjects of Entities rather than their colliders.

    Parameters

    • capsuleAxis: Line3

      The line segment, expressed in world space, representing the cylindrical part of the capsule (i.e. the length of the capsule without the two hemispheres).

    • capsuleRadius: number

      The radius, expressed in world space, of both the cylinder and hemispherical parts of the capsule. This defines how thick the capsule is.

    • Optional collisionLayerMask: number

      When provided, only the colliders matching this collision layer(s) are be considered on the Entity. When not provided, all colliders are considered.

    Returns ColliderIntersection[]

getEntitiesWithColliderIntersectingLineSegment

  • Gets the Entities of the Game which CollisionComponent's collider intersects with the given line segment. The result is provided as an array of ColliderIntersection objects. Each one tells which Entity has been hit and where exactly on the ray it happened. The ColliderIntersection objects in the resulting array are sorted by increasing distance to the start point of the line segment. Use the Game.getEntitiesWithVoxelObject-flavored static methods to intersect against the visual VoxelObjects of Entities rather than their colliders.

    Parameters

    • lineSegment: Line3

      The line segment, expressed in world space, against which Entities are tested for intersection.

    • Optional collisionLayerMask: number

      When provided, only the colliders matching this collision layer(s) are be considered on the Entity. When not provided, all colliders are considered.

    Returns ColliderIntersection[]

getEntitiesWithColliderIntersectingRay

  • getEntitiesWithColliderIntersectingRay(ray: Ray, rayLength?: number, collisionLayerMask?: number): ColliderIntersection[]
  • Gets the Entities of the Game which CollisionComponent's collider intersects with the given ray. The result is provided as an array of ColliderIntersection objects. Each one tells which Entity has been hit and where exactly on the ray it happened. The ColliderIntersection objects in the resulting array are sorted by increasing distance to the origin of the ray. Use the Game.getEntitiesWithVoxelObject-flavored static methods to intersect against the visual VoxelObjects of Entities rather than their colliders.

    Parameters

    • ray: Ray

      The ray, expressed in world space, against which the Entities are tested for intersection.

    • Optional rayLength: number

      If provided, the ray effectively behaves as a line segment starting from its origin and ending at this rayLength world units along the direction. If not provided, the ray starts at its origin and has an infinite length.

    • Optional collisionLayerMask: number

      When provided, only the colliders matching this collision layer(s) are be considered on the Entity. When not provided, all colliders are considered.

    Returns ColliderIntersection[]

getEntitiesWithColliderIntersectingSphere

  • getEntitiesWithColliderIntersectingSphere(sphere: Sphere, collisionLayerMask?: number): Entity[]
  • Gets the Entities of the Game which CollisionComponent's collider intersects with the given sphere volume (i.e. are totally or partially overlapping it). The Entities in the resulting array are not provided in any particular order.

    Parameters

    • sphere: Sphere

      The sphere, expressed in world space, against which the Entities of the Game are tested for intersection.

    • Optional collisionLayerMask: number

      When provided, only the colliders matching this collision layer(s) are be considered on the Entity. When not provided, all colliders are considered.

    Returns Entity[]

getEntityById

  • getEntityById(id: string): null | Entity
  • Gets the Entity in the Game with the given id, or null if there's no Entity with such id. Entities are assigned a unique id at creation so there can't be multiple Entities with the same id.

    Parameters

    • id: string

      The id of the Entity to look for.

    Returns null | Entity

getEntityByIndex

  • getEntityByIndex(index: number): null | Entity
  • Gets the index-th Entity in the list of all the Entities of the Game (see the entities property).

    Parameters

    • index: number

      The index of the requested Entity.

    Returns null | Entity

redirectToGame

  • redirectToGame(id: string, multiplayerInviteCode?: string): void
  • Redirects the browser to the given game. Usage example:

     this.game.redirectToGame("ee6c009c342f4176a6debbd7b3d997a3", "escapethealiens")
    

    Parameters

    • id: string

      The unique id of the game you want to go to. You can find this in the game's URL, after "game/". For example, given the URL:

      • https://dotbigbang.com/game/ee6c009c342f4176a6debbd7b3d997a3?mp=escapethealiens
      • The game's ID is this part: "ee6c009c342f4176a6debbd7b3d997a3".
      • You can select it in most browsers by double-clicking it.
    • Optional multiplayerInviteCode: string

      (Optional) The name of the multiplayer session you want to join. For example, given the URL:

      • https://dotbigbang.com/game/ee6c009c342f4176a6debbd7b3d997a3?mp=escapethealiens
      • The multiplayer invite code is this part: "escapethealiens".
      • You can select it in most browsers by double-clicking it.

    Returns void

removeEntity

  • removeEntity(entity: Entity): void
  • Removes the given Entity from the Game. If the Entity is networked, it is also automatically removed from the other Peers in the Session.

    Parameters

    • entity: Entity

      The Entity to remove

    Returns void

sendEventToTag

  • sendEventToTag(tag: string, methodName: string, ...rest: any[]): void
  • Calls the method specified by its name on all UserScriptComponents of all the Entities of the Game with the given tag. If a UserScriptComponent doesn't implement a method with that name, it's just skipped. The extra arguments after the method name are used as the arguments of the method to be called on each UserScriptComponent.

    ➡️ See usage examples in the API Demo: Game tag functions world.

    Parameters

    • tag: string

      The tag used to select which Entities in the Game should receive the event.

    • methodName: string

      The name of the method to try to call on each UserScriptComponent of the Entities of interest, defined by the tag.

    • Rest ...rest: any[]

      The arguments to pass to the method to be called on the UserScriptComponents.

    Returns void

worldToCanvas

  • Converts the point expressed in world space into canvas space (i.e in CSS pixels from the top-left corner). The conversion uses the CameraComponent of this.game.cameraEntity. If this is null, or if the cameraEntity doesn't have a CameraComponent, the method returns null. If the world point isn't inside the camera frustum, i.e. can't be seen on the canvas, the method also returns null. This is a helper method, internally it takes the world point, converts it to clip space using CameraComponent.worldToClipSpace(), and convert the clip space point to canvas space using game.renderer.clipSpaceToCanvas().

    Parameters

    • worldPoint: Vector3

      The world point to convert to canvas space.

    • Optional optionalTarget: Vector2

      When provided, return the canvas point into this Vector2 instead of allocating one. Be aware that even if you provide this argument, you should still check the return value for null.

    Returns null | Vector2

Static getClosestPointOnColliderToPoint

  • getClosestPointOnColliderToPoint(entity: Entity, point: Vector3, collisionLayerMask?: number): null | Vector3
  • Returns the closest point on the surface of the specified Entity's collider to the given point. Returns null if the Entity doesn't have a CollisionComponent or if the CollisionComponent's collider is NONE or VOXELOBJECT (which isn't supported yet). The point is returned in world space.

    Parameters

    • entity: Entity
    • point: Vector3

      The point, in world space, from which to find the closest point.

    • Optional collisionLayerMask: number

      When provided, only the colliders matching this collision layer(s) are be considered on the Entity. When not provided, all colliders are considered.

    Returns null | Vector3

Static getEntitiesWithVoxelObjectIntersectingLineSegment

  • Gets the Entities of the given list which have their VoxelObjectComponent's VoxelObject intersected by the given line segment. The intersection is calculated between the line segment and the current VoxelObject frame of the first VoxelObjectRenderer component of each Entity. If an entity of the list is tagged with "noraycast", it is skipped and not considered for intersection. The VoxelObjectIntersection objects in the resulting array are sorted by increasing distance to the line segment start point. Use getEntitiesWithCollider-flavored methods for intersecting with the CollisionComponent's collider rather than the visual VoxelObject of the Entities.

    Parameters

    • entities: Entity[]

      The list of Entities against which to perform the intersection test. If possible, avoid testing against all the Entities of the Game as this can be costly. Instead, you might want to first use a broad volume intersection test (i.e. like getEntitiesWithColliderIntersectingSphere) to get a small set of Entities to test against.

    • lineSegment: Line3

      The line-segment, expressed in world space, against which Entities are tested for intersection.

    Returns VoxelObjectIntersection[]

Static getEntitiesWithVoxelObjectIntersectingRay

  • Gets the Entities of the given list which have their VoxelObjectComponent's VoxelObject intersected by the given ray. The intersection is calculated between the ray and the current VoxelObject frame of the first VoxelObjectRenderer component of each Entity. If an entity of the list is tagged with "noraycast", it is skipped and not considered for intersection. The VoxelObjectIntersection objects in the resulting array are sorted by increasing distance to the origin of the ray. Use getEntitiesWithCollider-flavored methods to intersect against the CollisionComponent's collider rather than the visual VoxelObject of the Entities.

    Parameters

    • entities: Entity[]

      The list of Entities against which to perform the intersection test. If possible, avoid testing against all the Entities of the Game as this can be costly. Instead, you might want to first use a broad volume intersection test (i.e. like getEntitiesWithColliderIntersectingSphere) to get a small set of Entities to test against.

    • ray: Ray

      The ray, expressed in world space, against which Entities are tested for intersection.

    • Optional rayLength: number

      If provided, the ray effectively behaves as a line segment starting from its origin and ending at this rayLength world units along the direction. If not provided, the ray starts at its origin and has an infinite length.

    Returns VoxelObjectIntersection[]