BGE/Tiled
Alias: BGE.Tiled
Static Methods
resolveGid(
map: BGE.Tiled.TiledMap,
gid: dynamic,
): BGE.Tiled.TiledGidInfo
Looks up what a GID (a tile layer cell, or a tile object's gid) refers to: its tileset, its local id there, its tile entry if it has one, and its flip flags.
Tiled stores flips in a GID's top bits, so a flipped GID is above 2^31. Accepts it however it arrives: a LongInteger (as ParseJson gives it), a negative Integer, or a Double. A Float only works below 16777216 (2^24), so never for a flipped GID; bigger Floats count as empty.
Parameters
map(BGE.Tiled.TiledMap)gid(dynamic)
Returns
BGE.Tiled.TiledGidInfo— invalid for an empty cell (0) or a GID no tileset has
toUnsignedGid(gid: dynamic): longinteger
Parameters
gid(dynamic)
Returns
longinteger
getLayer(
map: BGE.Tiled.TiledMap,
name: string,
): BGE.Tiled.TiledLayer
The first layer with this name (group layers' children included), or invalid.
Parameters
map(BGE.Tiled.TiledMap)name(string)
Returns
BGE.Tiled.TiledLayer
getTileAt(
map: BGE.Tiled.TiledMap,
layer: BGE.Tiled.TiledLayer,
col: integer,
row: integer,
): BGE.Tiled.TiledGidInfo
What's in one cell of a tile layer. Rows count down from the top, as in Tiled.
Parameters
map(BGE.Tiled.TiledMap)layer(BGE.Tiled.TiledLayer) — a tile layercol(integer)row(integer)
Returns
BGE.Tiled.TiledGidInfo— invalid for an empty cell or one outside the layer
getLayerCells(
map: BGE.Tiled.TiledMap,
layer: BGE.Tiled.TiledLayer,
): Array.<BGE.Tiled.TiledCell>
Every non-empty cell of a tile layer, row by row.
Parameters
map(BGE.Tiled.TiledMap)layer(BGE.Tiled.TiledLayer) — a tile layer; invalid gives no cells
Returns
Array.<BGE.Tiled.TiledCell>
toWorldX(
map: BGE.Tiled.TiledMap,
pixelX: float,
offsetX?: float,
): float
A Tiled pixel x as a world x.
Parameters
map(BGE.Tiled.TiledMap)pixelX(float)offsetX(float, optional, default: "0.0") — added to the result
Returns
float
toWorldY(
map: BGE.Tiled.TiledMap,
pixelY: float,
offsetY?: float,
): float
A Tiled pixel y (+y down, from the map's top) as a world y (+y up, the map's bottom edge at 0).
Parameters
map(BGE.Tiled.TiledMap)pixelY(float)offsetY(float, optional, default: "0.0") — added to the result, e.g. to put a different row on world y 0
Returns
float
layerToColliderCells(
map: BGE.Tiled.TiledMap,
layer: BGE.Tiled.TiledLayer,
propertyName?: string,
): Array.<BGE.TileMap.ColliderCell>
One BGE.TileMap.ColliderCell per cell whose tile has a non-empty string value for the property, tagged with that value - ready for BGE.TileMap.mergeTileColliderRuns(). Hidden layers count too, so collisions can live on a layer that isn't drawn.
Parameters
map(BGE.Tiled.TiledMap)layer(BGE.Tiled.TiledLayer) — a tile layerpropertyName(string, optional, default: "\"collision\"") — a custom property set on tiles in the tileset
Returns
Array.<BGE.TileMap.ColliderCell>
layerToTileSpecs(
map: BGE.Tiled.TiledMap,
layer: BGE.Tiled.TiledLayer,
options?: BGE.Tiled.TiledTileSpecOptions,
): Array.<BGE.TileMap.TileSpec>
One BGE.TileMap.TileSpec per non-empty cell of a tile layer, positioned the way Tiled draws it - ready for BGE.TileMap.bakeTileMapImages().
A tile bigger than a cell sits on the cell's bottom-left corner, unless its tileset's "Tile Render Size" is "Map Grid Size", which fits it to the cell (stretched, or scaled evenly and centered for the "Preserve Aspect Fit" fill mode). The tileset's tile offset and the layer's offset are applied. Flipped tiles set flipH/flipV; rotated tiles (diagonal flips) aren't supported and draw unrotated and unflipped.
A hidden layer (or one in a hidden group) gives no tiles. Opacity isn't baked in: set alpha (layer.opacity * 255) on the drawables made from them.
Tileset images are loaded here, once each per call, unless options.bitmaps (or, without it, the map's own bitmaps from AssetLoader.loadTiledMap()) has them.
Parameters
map(BGE.Tiled.TiledMap)layer(BGE.Tiled.TiledLayer) — a tile layeroptions(BGE.Tiled.TiledTileSpecOptions, optional, default: "{}")
Returns
Array.<BGE.TileMap.TileSpec>
makeTileRegion(
info: BGE.Tiled.TiledGidInfo,
bitmaps: roAssociativeArray,
): dynamic
The region of its tileset image a tile is drawn from; invalid if the image won't load.
Parameters
info(BGE.Tiled.TiledGidInfo)bitmaps(roAssociativeArray)
Returns
dynamic
loadTileBitmap(
path: string,
bitmaps: roAssociativeArray,
): dynamic
The image at path, from bitmaps or loaded into it; invalid if it won't load.
Parameters
path(string)bitmaps(roAssociativeArray)
Returns
dynamic
parseMapFile(filePath: string): BGE.Tiled.TiledMap
Loads a map saved in Tiled's JSON format (.tmj/.json), and any external .tsj tilesets it uses. Image and tileset paths are resolved relative to the file that names them.
Supported: orthogonal, finite maps; tile layers stored as an array, CSV or uncompressed base64; object, image and group layers; embedded and external JSON tilesets (spritesheets and image collections); custom properties.
Not supported (prints a [BGE.Tiled] message and returns invalid): infinite maps, isometric/staggered/hexagonal maps, compressed layer data, XML tilesets (.tsx). Object templates, animated tiles and Wang sets are ignored.
Parameters
filePath(string) — e.g. "pkg:/maps/level1.tmj"
Returns
BGE.Tiled.TiledMap— the map, or invalid
parseMapJson(
json: string,
baseDir: string,
tilesets?: roAssociativeArray,
): BGE.Tiled.TiledMap
Parses a Tiled JSON map that's already in memory - see parseMapFile().
Parameters
json(string) — the map file's contentsbaseDir(string) — folder (or URL) the map lives in, for resolving image and tileset paths; "" leaves them as writtentilesets(roAssociativeArray, optional, default: "invalid") — resolved .tsj path -> its parsed JSON (see listExternalTilesets()); when given, external tilesets come only from here instead of being read
Returns
BGE.Tiled.TiledMap— the map, or invalid
listExternalTilesets(
json: string,
baseDir: string,
): Array.<string>
The external .tsj tilesets a map names, resolved against baseDir, once each and in order - for loading them before parseMapJson(). XML (.tsx) tilesets are left out; parsing reports them.
Parameters
json(string) — the map file's contentsbaseDir(string) — folder (or URL) the map lives in
Returns
Array.<string>— paths or URLs
listTiledMapImages(map: BGE.Tiled.TiledMap): Array.<string>
Every image a map's tilesets use (spritesheet images and image-collection tile images), once each - for loading them before layerToTileSpecs().
Parameters
map(BGE.Tiled.TiledMap)
Returns
Array.<string>— paths or URLs, as resolved by the parser
insertByFirstGid(
tilesets: roArray,
tileset: BGE.Tiled.TiledTileset,
): void
Parameters
tilesets(roArray)tileset(BGE.Tiled.TiledTileset)
Returns
void
parseTilesetEntry(
raw: roAssociativeArray,
baseDir: string,
suppliedTilesets?: dynamic,
): BGE.Tiled.TiledTileset
A map's tilesets entry: either a whole tileset, or {firstgid, source} pointing at a file.
Parameters
raw(roAssociativeArray)baseDir(string)suppliedTilesets(dynamic, optional, default: "invalid")
Returns
BGE.Tiled.TiledTileset
parseTileset(
raw: roAssociativeArray,
firstGid: integer,
baseDir: string,
): BGE.Tiled.TiledTileset
Parameters
raw(roAssociativeArray)firstGid(integer)baseDir(string)
Returns
BGE.Tiled.TiledTileset
parseLayers(
rawLayers: roArray,
out: roArray,
baseDir: string,
offsetX: float,
offsetY: float,
visible: boolean,
opacity: float,
): boolean
Appends each layer in rawLayers to out, replacing groups with their own layers (the group's offset, visibility and opacity combined into each). Returns false if a layer can't be read.
Parameters
rawLayers(roArray)out(roArray)baseDir(string)offsetX(float)offsetY(float)visible(boolean)opacity(float)
Returns
boolean
parseLayerData(raw: roAssociativeArray): roArray
A tile layer's GIDs as an array, from either a JSON array or uncompressed base64. GIDs with flip flags set are LongIntegers, matching what ParseJson gives for them.
Parameters
raw(roAssociativeArray)
Returns
roArray
parseObject(raw: roAssociativeArray): BGE.Tiled.TiledObject
Parameters
raw(roAssociativeArray)
Returns
BGE.Tiled.TiledObject
parsePoints(rawPoints: roArray): roArray
Parameters
rawPoints(roArray)
Returns
roArray
parseProperties(rawProperties: dynamic): roAssociativeArray
Tiled's [{name, type, value}] property list as a name -> value lookup.
Parameters
rawProperties(dynamic)
Returns
roAssociativeArray
getClass(raw: roAssociativeArray): string
Tiled 1.9 renamed objects' and tiles' "type" to "class".
Parameters
raw(roAssociativeArray)
Returns
string
getInt(
raw: roAssociativeArray,
key: string,
default: integer,
): integer
Parameters
raw(roAssociativeArray)key(string)default(integer)
Returns
integer
getFloat(
raw: roAssociativeArray,
key: string,
default: float,
): float
Parameters
raw(roAssociativeArray)key(string)default(float)
Returns
float
getString(
raw: roAssociativeArray,
key: string,
default: string,
): string
Parameters
raw(roAssociativeArray)key(string)default(string)
Returns
string
getBool(
raw: roAssociativeArray,
key: string,
default: boolean,
): boolean
Parameters
raw(roAssociativeArray)key(string)default(boolean)
Returns
boolean
getArray(raw: roAssociativeArray, key: string): roArray
Parameters
raw(roAssociativeArray)key(string)
Returns
roArray
logError(message: string): void
Parameters
message(string)
Returns
void
logWarning(message: string): void
Parameters
message(string)
Returns
void
Other
TiledTile
Extends: roAssociativeArray
One tile's own data from a tileset: only tiles Tiled wrote an entry for (ones with a type, properties, or - in an image collection - their own image) have one.
Properties
id(integer) — Local id within its tileset (GID - the tileset's firstGid)type(string) — Tiled's "class" (1.9+) or "type" (older); "" when unsetproperties(roAssociativeArray) — Custom properties, name -> valueimage(string) — Image-collection tiles only: full path to this tile's own image; "" otherwiseimageWidth(integer)imageHeight(integer)
TiledTileset
Extends: roAssociativeArray
A tileset, embedded in the map or loaded from an external .tsj. Spritesheet tilesets have an image cut into a grid; image collections have no image and one image per tile.
Properties
firstGid(integer)name(string)tileCount(integer)columns(integer) — 0 for an image collectiontileWidth(integer)tileHeight(integer)margin(integer)spacing(integer)image(string) — Full path to a spritesheet's image; "" for an image collectionimageWidth(integer)imageHeight(integer)tileOffsetX(integer) — Pixel offset to draw every tile at (Tiled'stileoffset; +y is down, as in Tiled)tileOffsetY(integer)tileRenderSize(string) — "tile" draws each tile at its own size; "grid" stretches it to the map's cell sizefillMode(string) — How a "grid"-sized tile fills its cell: "stretch", or "preserve-aspect-fit" (scaledtiles(roAssociativeArray) — Local id (as a string) -> TiledTile, for tiles that have oneproperties(roAssociativeArray)
TiledObject
Extends: roAssociativeArray
One placed object from an object layer. x/y are Tiled pixel coordinates (+y down): the top-left of a rectangle/ellipse/text, the position of a point, the origin of a polygon/polyline's points, and the BOTTOM-left of a tile object.
Properties
id(integer)name(string)type(string)shape(string) — "rectangle", "point", "ellipse", "polygon", "polyline", "tile" or "text"x(float)y(float)width(float)height(float)rotation(float) — Degrees clockwisevisible(boolean)points(roArray) — polygon/polyline only: points relative to x/y, as {x, y} AAsgid(dynamic) — tile objects only: the raw GID, flip flags included (see resolveGid()); 0 otherwiseproperties(roAssociativeArray)
TiledLayer
Extends: roAssociativeArray
A layer. Group layers aren't kept: their children are listed in order with the group's offset added and its visibility combined in, so every layer is found by name.
Properties
id(integer)type(string) — "tilelayer", "objectgroup" or "imagelayer"name(string)visible(boolean)opacity(float)offsetX(float) — Pixel offset (+y down, as in Tiled)offsetY(float)properties(roAssociativeArray)width(integer) — tilelayer only: width x height raw GIDs, row by row (0 = empty)height(integer)data(roArray)objects(roArray) — objectgroup onlyimage(string) — imagelayer only: full path to the image
TiledMap
Extends: roAssociativeArray
A parsed Tiled map - see BGE.Tiled.parseMapFile().
Properties
width(integer) — Size in cellsheight(integer)tileWidth(integer) — Cell size in pixelstileHeight(integer)backgroundColor(string) — "#rrggbb" / "#aarrggbb" as Tiled wrote it; "" when unsetproperties(roAssociativeArray)tilesets(roArray) — Sorted by firstGidlayers(roArray)name(string) — The name it was loaded under, set by AssetLoader.loadTiledMap()bitmaps(roAssociativeArray) — Image path -> roBitmap for every tileset image, set by AssetLoader.loadTiledMap();
TiledGidInfo
Extends: roAssociativeArray
What a GID refers to - see BGE.Tiled.resolveGid().
Properties
gid(integer) — The GID with its flip flags removedtileset(BGE.Tiled.TiledTileset)localId(integer)tile(dynamic) — invalid when the tileset has no entry for this tileflipH(boolean)flipV(boolean)flipD(boolean) — Diagonal flip (Tiled's 90-degree rotations combine this with flipH/flipV)
TiledCell
Extends: roAssociativeArray
One non-empty cell of a tile layer - see BGE.Tiled.getLayerCells().
Properties
row(integer)col(integer)info(BGE.Tiled.TiledGidInfo)
TiledTileSpecOptions
Extends: roAssociativeArray
Options for BGE.Tiled.layerToTileSpecs().
Properties
offsetX(float) — World units added to every tile's xoffsetY(float) — World units added to every tile's ybitmaps(roAssociativeArray) — Image path -> roBitmap. Images already in it aren't loaded again, and the ones that get