4
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

[PlayCanvas]他のEntityのScriptを取得する

Last updated at Posted at 2020-09-24

概要

例えば、衝突処理などが発生した時に、そのぶつかった対象に影響を与えるなどをするために必要な処理。

実装例

Sample.js
var Sample = pc.createScript('sample');

Sample.prototype.test = function( param ){
    console.log( param );
};
Call.js
var Call = pc.createScript('call');

Call.prototype.initialize = function(){
    var entity = ~~~; // 何かしらの手段でEntityを取得したとする

    // 安全でないアクセス
    entity.script.sample.test('Unsafe Call.');

    // 安全なアクセス方法
    // 1. EntityにScriptコンポーネントがあるかを調べる
    // 2. Scriptコンポーネント内にScriptがあるか調べる
    if(entity.script && entity.script.has('sample')){
        entity.script.sample.test('Safe Call.');
    }
};

PlayCanvasはEntityにScriptというコンポーネントを追加して、そこにJavaScriptで書かれたScriptを追加していく方式なので注意が必要。

4
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?