X Tutup
Skip to content

Commit 4e045b8

Browse files
committed
CreateScript example
1 parent 17ac57f commit 4e045b8

File tree

1 file changed

+24
-11
lines changed

1 file changed

+24
-11
lines changed

src/script/script.js

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,7 @@ pc.extend(pc, function () {
204204
/**
205205
* @function
206206
* @name pc.ScriptAttributes#remove
207-
* @description Remove Attribute
207+
* @description Remove Attribute.
208208
* @param {String} name Name of an attribute
209209
* @returns {Boolean} True if removed or false if not defined
210210
* @example
@@ -222,7 +222,7 @@ pc.extend(pc, function () {
222222
/**
223223
* @function
224224
* @name pc.ScriptAttributes#has
225-
* @description Detect if Attribute is added
225+
* @description Detect if Attribute is added.
226226
* @param {String} name Name of an attribute
227227
* @returns {Boolean} True if Attribute is defined
228228
* @example
@@ -238,7 +238,7 @@ pc.extend(pc, function () {
238238
* @function
239239
* @name pc.ScriptAttributes#get
240240
* @description Get object with attribute arguments.
241-
* Note: Changing argument properties will not affect existing Script Instances
241+
* Note: Changing argument properties will not affect existing Script Instances.
242242
* @param {String} name Name of an attribute
243243
* @returns {?Object} Arguments with attribute properties
244244
* @example
@@ -252,28 +252,41 @@ pc.extend(pc, function () {
252252

253253

254254
/**
255+
* @function
255256
* @name pc.CreateScript
256-
* @class Method to create named {@link ScriptType}.
257-
* It returns new class function "Script Type",
258-
* which is auto-registered to {@link pc.ScriptRegistry} using it's name.
259-
* @description This is main interface to create Script Types,
260-
* to define custom logic using javascript, that is used to create interaction for entities
257+
* @description Method to create named {@link ScriptType}.
258+
* It returns new function (class) "Script Type", which is auto-registered to {@link pc.ScriptRegistry} using it's name.
259+
* This is main interface to create Script Types, to define custom logic using javascript, that is used to create interaction for entities.
261260
* @param {String} name unique Name of a Script Type.
262261
* If Script Type of same name already registered and new one has `swap` method defined in prototype,
263262
* then it will perform hot swapping of existing Script Instances on entities using this new Script Type.
264263
* Note: there is a reserved list of names that cannot be used, such as list below as well as some starting from `_` (underscore):
265264
* system, entity, create, destroy, swap, move, scripts, onEnable, onDisable, onPostStateChange, has, on, off, fire, once, hasEvent
266265
* @param {pc.Application} [app] Optional application handler, to choose which pc.ScriptRegistry to add a script to.
267266
* By default it will use `pc.Application.getApplication()` to get current pc.Application.
268-
* @returns {function} So called Script Type, that developer is meant to extend by adding attributes and prototype methods.
267+
* @returns {ScriptType} Function so called {@link ScriptType}, that developer is meant to extend by adding attributes and prototype methods.
268+
* @example
269+
* var Turning = pc.CreateScript('turn');
270+
*
271+
* // define `speed` attribute that is available in Editor UI
272+
* Turning.attributes.add('speed', {
273+
* type: 'number',
274+
* default: 180,
275+
* placeholder: 'deg/s'
276+
* });
277+
*
278+
* // runs every tick
279+
* Turning.prototype.update = function(dt) {
280+
* this.entity.rotate(0, this.speed * dt, 0);
281+
* };
269282
*/
270283
var CreateScript = function(name, app) {
271284
if (CreateScript.reservedScripts[name])
272285
throw new Error('script name: \'' + name + '\' is reserved, please change script name');
273286

274287
/**
275288
* @name ScriptType
276-
* @class Class(function) that is returned by {@link pc.CreateScript}. Also referred as Script Type.<br />
289+
* @class Function that is returned by {@link pc.CreateScript}. Also referred as Script Type.<br />
277290
* This function is expected to be extended using JavaScript prototype. There is a <strong>list of expected methods</strong>
278291
* that will be executed by the engine, such as: initialize, postInitialize, update, postUpdate and swap.<br />
279292
* <strong>initialize</strong> and <strong>postInitialize</strong> - are called if defined when script is about to run for the first time. postInitialize will run after all initialize methods are executed in the same tick or enabling chain of actions.<br />
@@ -355,7 +368,7 @@ pc.extend(pc, function () {
355368
* @function
356369
* @name ScriptType#extend
357370
* @param {Object} methods Object with methods, where key - is name of method, and value - is function.
358-
* @description Shorthand function to extend Script Tyoe prototype with list of methods.
371+
* @description Shorthand function to extend Script Type prototype with list of methods.
359372
* @example
360373
* var PlayerController = pc.CreateScript('playerController');
361374
*

0 commit comments

Comments
 (0)
X Tutup