HTML5 shell class reference

Projects exported for the Web expose the Engine() class to the JavaScript environment, that allows fine control over the engine's start-up process.

This API is built in an asynchronous manner and requires basic understanding of Promises.

Engine

Engine 类提供了在Web上加载和启动导出项目的方法。对于默认的导出设置,这已经是导出的HTML页面的一部分。要了解 Engine 类的实际使用,请参见 自定义Web导出的HTML页面

Static Methods

Promise

load ( string basePath )

void

unload ( )

boolean

isWebGLAvailable ( [ number majorVersion=1 ] )

实例方法

Promise

init ( [ string basePath ] )

Promise

preloadFile ( string|ArrayBuffer file [, string path ] )

Promise

start ( EngineConfig override )

Promise

startGame ( EngineConfig override )

void

copyToFS ( string path, ArrayBuffer buffer )

void

requestQuit ( )

class Engine(initConfig)

Create a new Engine instance with the given configuration.

参数
  • initConfig (EngineConfig) -- The initial config for this instance.

Static Methods

Engine.load(basePath)

Load the engine from the specified base path.

参数
  • basePath (string) -- 引擎加载的底层路径。

返回

A Promise that resolves once the engine is loaded.

返回类型

Promise

Engine.unload()

卸载引擎以释放内存。

This method will be called automatically depending on the configuration. See unloadAfterInit.

Engine.isWebGLAvailable([majorVersion=1])

检查 WebGL 是否可用。(可选)指定要检查的特定 WebGL 版本。

参数
  • majorVersion (number) -- The major WebGL version to check for.

返回

If the given major version of WebGL is available.

返回类型

boolean

Instance Methods

Engine.prototype.init([basePath])

初始化引擎实例。如果引擎还没有被加载,可以选择将基本路径传递给引擎来加载它。参见 Engine.load()

参数
  • basePath (string) -- 引擎加载的底层路径。

返回

A Promise that resolves once the engine is loaded and initialized.

返回类型

Promise

Engine.prototype.preloadFile(file[, path])

Load a file so it is available in the instance's file system once it runs. Must be called before starting the instance.

If not provided, the path is derived from the URL of the loaded file.

参数
  • file (string|ArrayBuffer) --

    The file to preload.

    If a string the file will be loaded from that path.

    If an ArrayBuffer or a view on one, the buffer will used as the content of the file.

  • path (string) -- Path by which the file will be accessible. Required, if file is not a string.

返回

A Promise that resolves once the file is loaded.

返回类型

Promise

Engine.prototype.start(override)

Start the engine instance using the given override configuration (if any). startGame can be used in typical cases instead.

This will initialize the instance if it is not initialized. For manual initialization, see init. The engine must be loaded beforehand.

Fails if a canvas cannot be found on the page, or not specified in the configuration.

参数
  • override (EngineConfig) -- An optional configuration override.

返回

引擎启动后解析的承诺。

返回类型

Promise

Engine.prototype.startGame(override)

Start the game instance using the given configuration override (if any).

This will initialize the instance if it is not initialized. For manual initialization, see init.

This will load the engine if it is not loaded, and preload the main pck.

This method expects the initial config (or the override) to have both the executable and mainPack properties set (normally done by the editor during export).

参数
  • override (EngineConfig) -- An optional configuration override.

返回

当游戏启动后承诺解析。

返回类型

Promise

Engine.prototype.copyToFS(path, buffer)

Create a file at the specified path with the passed as buffer in the instance's file system.

参数
  • path (string) -- The location where the file will be created.

  • buffer (ArrayBuffer) -- The content of the file.

Engine.prototype.requestQuit()

Request that the current instance quit.

This is akin the user pressing the close button in the window manager, and will have no effect if the engine has crashed, or is stuck in a loop.

Engine configuration

An object used to configure the Engine instance based on godot export options, and to override those in custom HTML templates if needed.

Properties

type

name

boolean

unloadAfterInit

HTMLCanvasElement

canvas

字符串

executable

字符串

mainPack

字符串

locale

number

canvasResizePolicy

Array.<string>

args

function

onExecute

function

onExit

function

onProgress

function

onPrint

function

onPrintError

EngineConfig

The Engine configuration object. This is just a typedef, create it like a regular object, e.g.:

const MyConfig = { executable: 'godot', unloadAfterInit: false }

Property Descriptions

unloadAfterInit

Whether the unload the engine automatically after the instance is initialized.

Type

boolean

Value

true

canvas

The HTML DOM Canvas object to use.

By default, the first canvas element in the document will be used is none is specified.

Type

HTMLCanvasElement

Value

null

executable

The name of the WASM file without the extension. (Set by Godot Editor export process).

Type

字符串

Value

""

mainPack

An alternative name for the game pck to load. The executable name is used otherwise.

Type

字符串

Value

null

locale

指定语言代码,为游戏选择合适的本地化。

The browser locale will be used if none is specified. See complete list of supported locales.

Type

字符串

Value

null

canvasResizePolicy

The canvas resize policy determines how the canvas should be resized by Godot.

0 means Godot won't do any resizing. This is useful if you want to control the canvas size from javascript code in your template.

1 means Godot will resize the canvas on start, and when changing window size via engine functions.

2 means Godot will adapt the canvas size to match the whole browser window.

Type

number

Value

2

args

The arguments to be passed as command line arguments on startup.

See command line tutorial.

Note: startGame will always add the --main-pack argument.

Type

Array.<string>

Value

[]

onExecute(path, args)

A callback function for handling Godot's OS.execute calls.

This is for example used in the Web Editor template to switch between project manager and editor, and for running the game.

参数
  • path (string) -- The path that Godot's wants executed.

  • args (Array.<string>) -- The arguments of the "command" to execute.

onExit(status_code)

A callback function for being notified when the Godot instance quits.

Note: This function will not be called if the engine crashes or become unresponsive.

参数
  • status_code (number) -- The status code returned by Godot on exit.

onProgress(current, total)

A callback function for displaying download progress.

The function is called once per frame while downloading files, so the usage of requestAnimationFrame() is not necessary.

如果回调函数接收到的字节总数为0,这意味着无法计算。可能的原因包括:

  • 文件随服务器端分块压缩一起提供

  • 文件在Chromium上通过服务器端压缩提供

  • 并非所有文件下载都已开始(通常在没有多线程的服务器上)

参数
  • current (number) -- The current amount of downloaded bytes so far.

  • total (number) -- The total amount of bytes to be downloaded.

onPrint([...var_args])

A callback function for handling the standard output stream. This method should usually only be used in debug pages.

默认情况下,使用的是“console.log()”。

参数
  • var_args (*) -- A variadic number of arguments to be printed.

onPrintError([...var_args])

A callback function for handling the standard error stream. This method should usually only be used in debug pages.

By default, console.error() is used.

参数
  • var_args (*) -- A variadic number of arguments to be printed as errors.