Skip to content

Prefabs ​

A prefab is a named data object that describes a GameObject. You define prefabs in settingsData.json and turn them into GameObjects by name.

json
{
  "prefabs": [
    {
      "name": "ball",
      "texture": "ball",
      "scale": {
        "x": 2,
        "y": 2
      }
    }
  ]
}
ts
this.AddGameObject('ball');

The Chrome extension lets you view and edit prefabs, so you rarely need to edit the JSON by hand.

Example: the Platformer's settingsData.json defines the player, blocks, coins and flag as prefabs.

Entities ​

The same kind of data object, when it isn't defined in settingsData.json, is called an entity. You can pass one straight to AddGameObject, and levels are made of them:

ts
this.AddGameObject({ texture: 'ball', x: 10 });
// a prefab with overrides
this.AddGameObject({
    name: 'bigBall',
    extends: 'ball',
    scale: { x: 2, y: 2 },
});

In other words, prefabs are the definitions in settingsData.json, and entities are the data that describes individual objects, often by extending a prefab.

Extending ​

A prefab or entity can extend another prefab with extends. It inherits all properties and can override any of them. Prefabs can extend each other in a chain.

json
{
  "name": "enemy",
  "texture": "enemy",
  "components": {
    "health": {
      "max": 3
    }
  }
},
{
  "name": "bossEnemy",
  "extends": "enemy",
  "scale": {
    "x": 3,
    "y": 3
  }
}

Example: the Showcase's prefabs demo builds a chain of prefabs that extend each other.

Base prefabs ​

Base prefabs are defined under basePrefabs. They hold the same properties, but they are building blocks for other prefabs rather than things you add directly.

json
{
  "basePrefabs": [
    {
      "name": "block",
      "baseClass": "ArcadeObject",
      "hitbox": "FULL",
      "static": true
    }
  ],
  "prefabs": [
    {
      "name": "grassLeft",
      "extends": "block"
    },
    {
      "name": "grassMid",
      "extends": "block"
    }
  ]
}

ANY is a built-in base prefab that every prefab inherits from. Properties you give ANY apply to all prefabs, for example a default pivot:

json
{
  "name": "ANY",
  "pivot": {
    "x": 0.5,
    "y": 0.5
  }
}

To leave a prefab out of that, let it extend NONE, the other built-in base prefab. It ends the chain, so nothing from ANY is applied:

json
{
  "name": "background",
  "extends": "NONE",
  "texture": "sky"
}

Every prefab and entity without an extends extends ANY, including base prefabs, so NONE is the only way out.

Example: the Platformer's base prefabs block and collectable are extended by every block and coin.

Properties ​

Prefabs and entities can have these properties. Most can also be a preset name, like "pivot": "CENTER".

Identity ​

PropertyDescription
nameThe name. Find the object with bare.object.GetByName().
extendsThe prefab this one inherits from. Defaults to ANY.
baseClassGameObject class to create, for example ArcadeObject. Register it with bare.AddClass().
tagsA list of strings, to group and find objects.
customDataYour own named values, for your game code.

Transform ​

PropertyDescription
position, or x and yPosition. For children, relative to the parent. Usually set per object, in a level or in code, not in a prefab.
scaleScale as { x, y }.
rotationRotation in degrees.
flippedFlip horizontally.
pivotPivot point. { x: 0, y: 0 } is the top left; values above 0 and up to 1 are a fraction of the image size, larger values are pixels. On the GameObject it reads back in pixels. See Pivot.

Inner transform ​

An extra transform for the visual content only, on top of the object's own transform. Useful for a bounce or wobble that shouldn't move the object itself.

PropertyDescription
innerPositionOffset.
innerScaleScale.
innerRotationRotation in degrees.
innerPivotPivot for the inner transform. Defaults to pivot.
innerFlippedAn extra horizontal flip.
childrenInheritInnerTransformChildren follow the inner transform too. On by default.

Appearance ​

PropertyDescription
textureImage name. "NAME" uses the object's name, handy in ANY.
imagePathFolder for the image, relative to public/images/.
colorTint. White keeps the original colors.
alphaOpacity, from 0 to 1.
visibleWhether it's drawn.

Structure ​

PropertyDescription
componentsComponents to attach, with their field values.
childrenChild entities.
componentsRemovedNames of inherited components to leave out.
propertiesRemovedNames of inherited properties to leave out.

Behavior ​

PropertyDescription
tapHitboxArea for pointer input, as { x1, y1, x2, y2 }.
persistentKeep the object when the level is cleared. Persistent objects aren't saved with the level.
saveInclude the object when saving the level.
interactableInEditorWhether the object can be selected in the level editor.

Class-specific properties ​

A baseClass adds its own properties, such as velocity, static and hitbox for an ArcadeObject. See Physics and Text & UI.

Any field marked @InspectorField on a GameObject class can be set from a prefab.

Components in prefabs ​

The components map uses the component name without the Component suffix, starting lowercase. Values set the component's fields:

json
"components": {
  "player": {
    "moveSpeed": 600
  },
  "animation": {}
}

This attaches a PlayerComponent with moveSpeed = 600 and an AnimationComponent.

Example: the Platformer's player prefab attaches an animation and a player component.

Presets ​

presets holds reusable values. Use a preset's name as the value of a property, and it's replaced by the preset.

A preset says which property it's for: its keys are property names. The preset below only fills in a particle value:

json
"presets": {
  "PARTICLE_EFFECT": {
    "particle": {
      "life": 1,
      "emitRate": 5
    }
  }
},
"basePrefabs": [
  {
    "name": "collectable",
    "components": {
      "particle": "PARTICLE_EFFECT"
    }
  }
]

Presets can go anywhere in your settings, including inside other presets.

To make a preset that works for any property, use the key ANY:

json
"presets": {
  "HALF": {
    "ANY": {
      "x": 0.5,
      "y": 0.5
    }
  }
}

Built-in presets ​

PresetValue
CENTER{ "x": 0.5, "y": 0.5 } for any property, for example "pivot": "CENTER".
FULLA hitbox covering the whole image, for "hitbox": "FULL".

Example: the Platformer's coins use the PARTICLE_EFFECT preset for their sparkle.

Typed access ​

The generated Data object gives you autocomplete for prefab names:

ts
import { Data } from './generated/assets';

this.AddGameObject(Data.Prefab.PLAYER);

See Generated names for the rest of Data.

Bare Engine is open source under the MIT license.