DynamicLoader

DynamicLoader

Class to load objects into the engine, or to update objects based on the data model.

Constructor

new DynamicLoader(options)

Parameters:
Name Type Description
options Object
Properties
Name Type Attributes Default Description
world World

The target World object.

rootPath string

The root path from where to get resources.

ajax Ajax <optional>
new Ajax(options.rootPath)

Can be used to overwrite how the loader fetches refs. Good for testing.

Methods

(static) _findRefsInConfig(config, onRef)

Traverses a json-like structure and calls a function for each ref

Parameters:
Name Type Description
config Object

The configuration to search

onRef function

function called when a ref is found.
The ref argument contains the ref and replace is a function that can be
called to replace the current ref with a different ref.

(static) _getRefsFromConfig(config) → {Array}

Traverses a json-like structure and collects refs in an array

Parameters:
Name Type Description
config

(static) getTypeForRef(ref) → {string}

Gets the type of a reference.

Parameters:
Name Type Description
ref string

Reference.

clear() → {Promise}

Clears the cache of all the handlers. Also clears the engine.

load(ref, options) → {Promise}

Load an object with the specified id into the engine. If an object is already
loaded, it will return that object without updating it.

The object can be of any type, what loading does is determined by the ref
type and the registered ConfigHandler.

Parameters:
Name Type Description
ref string

Id of object to load.

options Object
Properties
Name Type Attributes Default Description
progressCallback function <optional>

Function called while loading the world.
Arguments handled and total are both integer numbers and represent the loaded elements so far as well as the total elements.

preloadBinaries boolean <optional>
false

Load the binary data as soon as the reference is loaded.

noCache boolean <optional>
false

Ignore cache, i.e. always load files fresh from the server.

loadInstance(ref, options) → {Promise}

Like DynamicLoader#load, but creates an unique instance of the
specified resource each time loadInstance is called. This function works on
any resource type, but is intended primarily for use with entities.

In general, loadInstance creates a "shallow" instance of the resource. That
means that resources referenced by the instanced resource are not themselves
instanced. For example, instances of a material resource will initially all
share the same shader and texture resources; but the instances are distinct
from each other and can be individually modified to reference other shaders
or textures. For comparision, if load was used instead of loadInstance, then
the same material object would be returned every time, so changing the
shader or texture it references would apply to every use of the material.

However, when an entity that has child entities is instanced, an instance of
each of those child entities is also created. Otherwise the instance and the
original would both behave as if they had exclusive control over all the
children.

A few other resource types are also always instanced in order to insure they
work correctly.

Parameters:
Name Type Description
ref string

Id of object to load.

options Object
Properties
Name Type Attributes Default Description
progressCallback function <optional>

Function called while loading the world. Arguments handled and total are
both integer numbers and represent the loaded elements so far as well as
the total elements.

preloadBinaries boolean <optional>
false

Load the binary data as
soon as the reference is loaded.

noCache boolean <optional>
false

Ignore cache, i.e. always load
files fresh from the server.

instancedRefTypes Array.<string> <optional>
[]

Resource types that are
instanced. Behaviors are always instanced. You need to explicitly instance
'material' if you want the materials referenced by a loaded entity to
be independent from the material referenced by the original entity (so
you can change the colors independently, for example).

preload(bundle, clearopt)

Load configs into the loader cache without loading anything into the engine.
Subsequent calls to load and update will draw configs from the prefilled cache.

Parameters:
Name Type Attributes Default Description
bundle Object

Bundle object. Keys should be refs, and values are the config objects. If a config is null,
the loader will search for the appropriate config in the loader's internal cache.

clear boolean <optional>
false

If true, possible previous cache will be cleared. Otherwise the existing cache is extended.

update(ref, configopt, options) → {Promise}

Update an object in the world with an updated config. The object can be of any
type, updating behavior is determined by the registered {ConfigHandler}.

Parameters:
Name Type Attributes Description
ref string

Ref of object to update.

config Object <optional>

New configuration (formatted according to data model).
If omitted, works the same as {DynamicLoader.load}.

options Object
Properties
Name Type Attributes Default Description
noCache boolean <optional>
false

Ignore cache, i.e. always load files fresh from the server.