[SOLVED] Script.create() gives me errors

Hello again. I am trying to add a script component (and a script) programmatically.
I use self.Players[playersData.ID].addComponent("script"); to add a script component. Then I add the desired script using: self.Players[playersData.ID].script.create("player");

This generates me the following errors:

EDIT: Project link: https://playcanvas.com/armainap

Thank you very much!

Hi - the link you posted is a link to your profile not your project but that’s fine because you only have one project :slight_smile: .

I am launching your scene but I don’t see any errors. What are the steps to reproduce this?

I changed the code since then…also commented the code that gives me the issue while experimenting with it. Check again now. The line of code that is creating the issue is line 24 from Scripts/network.js

I will also add here the whole network.js script:

var Network = pc.createScript('network');

// initialize code called once per entity
Network.prototype.initialize = function() {
    this.socket = io.connect('https://pongmmo.glitch.me');
    var self = this;
    this.Players = {};
    this.Boards = [];
    
    this.socket.on ('playerAdd', function(PlayerObject) {
        self.createPlayer(PlayerObject);
    });
    
    this.socket.on ('playerData', function(playersData) {
        self.app.root.findByName("statusText").element.text = "";
        
        for(var index in playersData.Players)
            self.createPlayer(playersData.Players[index]);
        
        /*var script = self.app.assets.find("player");
        script.ready(function (asset) {});
        self.app.assets.load(script);*/
        self.Players[playersData.ID].addComponent("script");
        self.Players[playersData.ID].script.create("player"); //Based on the error it has trouble with loading player.js
    });
    
    this.socket.on ('playerRemove', function(idsData) {
        self.removePlayer(idsData);
    });
    
    this.socket.on ('boardAdd', function(BoardObject) {
        self.createBoard(BoardObject);
    });
    
    this.socket.on ('boardData', function(boardsData) {
        for (var index = 1; index < boardsData.length; index++)
            self.createBoard(boardsData[index]);
    });
    
    this.socket.on ('boardRemove', function() {
        self.removeBoard();
    });
};

Network.prototype.createPlayer = function (playerData) {
    var playerEntity = new pc.Entity();
    playerEntity.addComponent("model", {type: 'box'});
    playerEntity.model.material = this.app.assets.find("PaddleMaterial").resource;
    playerEntity.setPosition(playerData.X, 1, playerData.Y);
    playerEntity.setEulerAngles(0, playerData.Yaw, 0);
    playerEntity.setLocalScale(25, 1, 250);
    this.app.root.addChild(playerEntity);
    this.Players[playerData.ID] = playerEntity;
};

Network.prototype.removePlayer = function (idsData) {    
    this.Players[idsData.Replacer].setPosition(this.Players[idsData.Removed].getPosition());
    this.Players[idsData.Replacer].setRotation(this.Players[idsData.Removed].getRotation());
    
    this.Players[idsData.Removed].destroy();
    delete this.Players[idsData.Removed];
};

Network.prototype.createBoard = function (boardData) {
    var boardEntity = new pc.Entity();
    boardEntity.addComponent("model", {type: 'box'});
    boardEntity.model.material = this.app.assets.find("BoardMaterial").resource;
    boardEntity.setPosition(boardData.X * 1000, 0, boardData.Y * 1000);
    boardEntity.setLocalScale(1000, 1, 1000);
    this.app.root.addChild(boardEntity);
    this.Boards.push(boardEntity);
};

Network.prototype.removeBoard = function () {
    this.Boards.pop().destroy();
};

The problem is this script:

var Player = pc.createScript('player');

// initialize code called once per entity
Player.prototype.initialize = function() {
    this.entity.model.meshInstances[0].material.setParameter('material_diffuse', new pc.Color(0, 1, 0, 1));
    
    console.log("Worked!");
};

// update code called every frame
Player.prototype.update = function(dt) {
};

Why are you setting a material’s diffuse color like that?

Change it to:

var Player = pc.createScript('player');

// initialize code called once per entity
Player.prototype.initialize = function() {
    var material = this.entity.model.meshInstances[0].material;
    material.diffuse.set(0, 1, 0);
    material.update();
    
    console.log("Worked!");
};

// update code called every frame
Player.prototype.update = function(dt) {
};
1 Like

I was setting it like that because that is one method of setting that I found by doing a quick search. Everything works now, thank you very much!

No problem. :slight_smile: Glad to be of assistance.

1 Like