Difference between revisions of "API:Import"

From Spherical
Jump to: navigation, search
(miniRT -> sphere-runtime rename)
 
(4 intermediate revisions by one other user not shown)
Line 1: Line 1:
{{DISPLAYTITLE:API:import()}}
+
{{DISPLAYTITLE:API:import}}
  
 
Sphere uses the CommonJS standard for dependency management. In CommonJS, each script is run as a function, which isolates modules from one another and enables more granular control of exports. In addition, because the code importing a module decides under what name to store the imports (if at all), naming conflicts between modules are avoided entirely.
 
Sphere uses the CommonJS standard for dependency management. In CommonJS, each script is run as a function, which isolates modules from one another and enables more granular control of exports. In addition, because the code importing a module decides under what name to store the imports (if at all), naming conflicts between modules are avoided entirely.
Line 19: Line 19:
  
 
<syntaxhighlight>
 
<syntaxhighlight>
import { from, Delegate, Music, Scene } from 'miniRT';
+
import { from, Delegate, Music, Scene } from 'sphere-runtime';
  
Music.play('sounds/music/somefile.ogg'); //Use the imported Music function
+
Music.play('music/somefile.ogg'); //Use the imported Music object
 
</syntaxhighlight>
 
</syntaxhighlight>
  
 
[[Category:Sphere 2 API]]
 
[[Category:Sphere 2 API]]
 
[[Category:Sphere 2 native functions]]
 
[[Category:Sphere 2 native functions]]

Latest revision as of 20:41, 20 June 2017


Sphere uses the CommonJS standard for dependency management. In CommonJS, each script is run as a function, which isolates modules from one another and enables more granular control of exports. In addition, because the code importing a module decides under what name to store the imports (if at all), naming conflicts between modules are avoided entirely.

Usage

import { FunctionName } from 'module';

Loads a CommonJS module. `module_id` is an abstract path (a string) identifying the module to load, with the same semantics as in, e.g. Node.js. If the module cannot be found, an error will be thrown. The engine will look for modules in the following locations:

   @/lib/
   #/modules/

If require() is provided with a module ID beginning with either `./` or `../`, this indicates a path relative to the location of the calling module.


Examples

import { from, Delegate, Music, Scene } from 'sphere-runtime';

Music.play('music/somefile.ogg'); //Use the imported Music object