Health for multiplayer script not working correctly

Okay so the game is essentially a gather, build and survive multiplayer game (hopefully it will be soon at least) You can find the editor here if there seems to be something you need to fidget with yourself. I wanted to use a damageDemo script I made to get the player to take damage when colliding with an entity with the script attached, and I added this to the gamePlayManager script to make sure the player is disabled from the network entity when the health has reached 0, however, I guess somewhere in between the lines I messed up or miscalculated somewhere? I have three separate scripts trying to work together to complete the task of hopefully soon what will be PVP, I will list the scripts below:

damageDemo script:

var DamageDemo = pc.createScript('damageDemo');

DamageDemo.prototype.initialize = function() {
    this.entity.collision.on('collisionstart', this.onCollisionStart, this);
};

DamageDemo.prototype.onCollisionStart = function(result) {
    if (result.other && result.other.tags.has('player')) {
        result.other.script.health.decreaseHealth(10);
    }
};

GamePlayManager script:

var GamePlayManagerScript = pc.createScript('gamePlayManagerScript');

GamePlayManagerScript.attributes.add('mainMenuComp', {type: 'entity'});
GamePlayManagerScript.attributes.add('inputBoxNickNameComp', {type: 'entity'});
GamePlayManagerScript.attributes.add('playButtonComp', {type: 'entity'});
GamePlayManagerScript.attributes.add('chatBoxPanelComp', {type: 'entity'});
GamePlayManagerScript.attributes.add('inputBoxChatMsgComp', {type: 'entity'});
GamePlayManagerScript.attributes.add('scrollContentPanelComp', {type: 'entity'});
GamePlayManagerScript.attributes.add('verticalScrollbarComp', {type: 'entity'});
GamePlayManagerScript.attributes.add('msgHolderImageComp', {type: 'entity'});

GamePlayManagerScript.attributes.add('networkEntity', {type: 'entity'});

GamePlayManagerScript.attributes.add('playerEntity', {type: 'entity'});
GamePlayManagerScript.attributes.add('otherEntity', {type: 'entity'});


// initialize code called once per entity
GamePlayManagerScript.prototype.initialize = function() {
    
    this.app.mouse.disableContextMenu();
    
    this.mainMenuComp.enabled = true;
    this.networkEntity.enabled = false;
    this.playerEntity.enabled = false;
    this.otherEntity.enabled = false;
    this.chatBoxPanelComp.enabled = false;
    
    this.gameStarted = false;
    this.allowMovement = false;
    this.lastMsg = "";
    
    this.nicknameVal = "";
    
    this.playButtonComp.element.on('click', function (evt) {
        
        setTimeout(function(){
            
            this.nicknameVal = this.inputBoxNickNameComp.script.input.getCleanValue();
            // console.log('btn clicked ' , this.nicknameVal);

            if(this.nicknameVal !== "")
            {
                this.playerEntity.findByName('playerNickText').element.text = this.nicknameVal;
                
                this.mainMenuComp.enabled = false;
                this.networkEntity.enabled = true;
                this.playerEntity.enabled = true;
                this.chatBoxPanelComp.enabled = true;
                
                this.gameStarted = true;
                this.allowMovement = true;
            }
            else
            {
                alert("Please enter proper NickName!");
            }
    
        }.bind(this),100);
        
    }, this);
    
};

// update code called every frame
GamePlayManagerScript.prototype.update = function(dt) {
    
    if(this.gameStarted === true)
    {
        if (this.app.keyboard.wasPressed(pc.KEY_ENTER))
        {
            // console.log(this.inputBoxChatMsgComp.script.input.checkFocus());
            
            if(this.inputBoxChatMsgComp.script.input.isFocued() === true)
            {
                var textBoxVal = this.inputBoxChatMsgComp.script.input.getCleanValue();
                
                this.lastMsg = textBoxVal;
                this.inputBoxChatMsgComp.script.input.deleteValue();
                this.inputBoxChatMsgComp.script.input.blur();
                
                if(this.lastMsg !== "")
                {
                    this.networkEntity.script.network.sendMsg(this.nicknameVal,this.lastMsg);
                }
            }
            else
            {
                this.inputBoxChatMsgComp.script.input.focus();
            }
            
            // console.log("this.lastMsg = ", this.lastMsg);
        }
        
        if(this.inputBoxChatMsgComp.script.input.isFocued() === true)
        {
            this.allowMovement = false;
        }
        else
        {
            this.allowMovement = true;
        }
    }
};

GamePlayManagerScript.prototype.update = function(dt) {
    // check for collisions with entities with the damageDemo script
    var entities = this.app.root.find(function(node) {
        return node.script && node.script.damageDemo;
    });
    for (var i = 0; i < entities.length; i++) {
        if (this.playerEntity.collision.collidesWith(entities[i])) {
            // player is hit, decrease health
            this.playerEntity.script.playerHealth.takeDamage(10);
            // reset the entity that caused the damage
            entities[i].script.damageDemo.onCollision();
        }
    }
};

GamePlayManagerScript.prototype.addMsgInChatBox = function(nicknameVal, actualMsg) {
    
    // console.log("GamePlayManagerScript.prototype.addMsgInChatBox = ",nicknameVal, actualMsg);
    var msgHolderImageComp = this.msgHolderImageComp.clone();
    msgHolderImageComp.findByName('sentMsgText').element.text = nicknameVal + " : " +actualMsg;
    msgHolderImageComp.tags.add('msgBlock');
    
    this.scrollContentPanelComp.addChild(msgHolderImageComp);
    msgHolderImageComp.enabled = true;
    
    
    if(this.scrollContentPanelComp.findByTag('msgBlock').length * msgHolderImageComp.element.height > 260)
    {
        this.scrollContentPanelComp.element.height = this.scrollContentPanelComp.findByTag('msgBlock').length * msgHolderImageComp.element.height;
        this.verticalScrollbarComp.scrollbar.value = 1;
    }
};

// swap method called for script hot-reloading
// inherit your script state here
// GamePlayManagerScript.prototype.swap = function(old) { };

// to learn more about script anatomy, please read:
// http://developer.playcanvas.com/en/user-manual/scripting/

playerHealth script:

var PlayerHealth = pc.createScript('playerHealth');

PlayerHealth.attributes.add('startingHealth', {type: 'number', default: 100});

// initialize code called once per entity
PlayerHealth.prototype.initialize = function() {
    this.health = this.startingHealth;
};

// decrease player health by given amount
PlayerHealth.prototype.takeDamage = function(amount) {
    this.health -= amount;
    
    if (this.health <= 0) {
        // player is dead, disable entity and reload page
        this.entity.enabled = false;
        window.location.reload();
    }
};

// increase player health by given amount
PlayerHealth.prototype.heal = function(amount) {
    this.health += amount;
};

// swap method called for script hot-reloading
// inherit your script state here
// PlayerHealth.prototype.swap = function(old) { };

Also here is an error when the game is launched: [gamePlayManagerScript.js?id=115546646&branchId=7132ce7f-f6d7-45ed-90f8-24d0578f92c5:112]: this.playerEntity.collision.collidesWith is not a function

TypeError: this.playerEntity.collision.collidesWith is not a function
at GamePlayManagerScript.update (https://launch.playcanvas.com/api/assets/files/scripts/gamePlayManagerScript.js?id=115546646&branchId=7132ce7f-f6d7-45ed-90f8-24d0578f92c5:112:41)
at ScriptComponent._scriptMethod (https://code.playcanvas.com/playcanvas-1.60.0.dbg.js:57557:22)
at ScriptComponent._onUpdate (https://code.playcanvas.com/playcanvas-1.60.0.dbg.js:57590:15)
at ScriptComponentSystem._callComponentMethod (https://code.playcanvas.com/playcanvas-1.60.0.dbg.js:58026:52)
at ScriptComponentSystem._onUpdate (https://code.playcanvas.com/playcanvas-1.60.0.dbg.js:58038:11)
at ComponentSystemRegistry.fire (https://code.playcanvas.com/playcanvas-1.60.0.dbg.js:623:21)
at Application.update (https://code.playcanvas.com/playcanvas-1.60.0.dbg.js:35391:19)
at Application.tick (https://code.playcanvas.com/playcanvas-1.60.0.dbg.js:35881:20)
at Application.start (https://code.playcanvas.com/playcanvas-1.60.0.dbg.js:35370:11)
at https://launch.playcanvas.com/editor/scene/js/launch.js:1:142473

(Note that this error does not appear when the player comes in contact with the damaged entity, yet it gives the error right when the game is loaded)

Hi @Jacob_McBride1!

You try to exexute a function with the name decreaseHealth, while the name of the function seems to be takeDamage.

That’s because you try to execute a function in the update function of your gamePlayManager script, that doesn’t seem to exist. Is this.playerEntity.collision.collidesWith a custom function you created somewhere?

I also see two functions with the name update. I’m not sure if this is possible, so I suggest to merge them to one function.

1 Like

is there a way to execute all of this at once in simple terms?

Sorry, I have no idea what you mean.

1 Like

Is there a way to do all of this in one script? there is an entity called network, that holds the networking script as I recall, perhaps the script could be one script such as Health manager, and the damageDemo as DamageManager which handles each logic contained in two separate scripts? Or would I have to rewrite the code in all three scripts to make all the errors gone and make it work?

here is the new health script

var PlayerHealth = pc.createScript('playerHealth');

PlayerHealth.attributes.add('maxHealth', {
    type: 'number',
    default: 100
});

// initialize code called once per entity
PlayerHealth.prototype.initialize = function() {
    this.currentHealth = this.maxHealth;
};

// function to decrease player health
PlayerHealth.prototype.takeDamage = function(damage) {
    this.currentHealth -= damage;
    if (this.currentHealth <= 0) {
        this.currentHealth = 0;
        this.entity.enabled = false;
        // reload page
        window.location.reload();
    }
};

// function to increase player health
PlayerHealth.prototype.heal = function(amount) {
    this.currentHealth += amount;
    if (this.currentHealth > this.maxHealth) {
        this.currentHealth = this.maxHealth;
    }
};

here is the new damage script:

var DamageDemo = pc.createScript('damageDemo');

DamageDemo.prototype.initialize = function() {
};

DamageDemo.prototype.onCollisionEnter = function(otherEntity) {
    if (otherEntity.tags.has('player')) {
        otherEntity.script.playerHealth.takeDamage(10);
    }
};

the gameplay manager script:

var GamePlayManagerScript = pc.createScript('gamePlayManagerScript');

GamePlayManagerScript.attributes.add('mainMenuComp', {type: 'entity'});
GamePlayManagerScript.attributes.add('inputBoxNickNameComp', {type: 'entity'});
GamePlayManagerScript.attributes.add('playButtonComp', {type: 'entity'});
GamePlayManagerScript.attributes.add('chatBoxPanelComp', {type: 'entity'});
GamePlayManagerScript.attributes.add('inputBoxChatMsgComp', {type: 'entity'});
GamePlayManagerScript.attributes.add('scrollContentPanelComp', {type: 'entity'});
GamePlayManagerScript.attributes.add('verticalScrollbarComp', {type: 'entity'});
GamePlayManagerScript.attributes.add('msgHolderImageComp', {type: 'entity'});

GamePlayManagerScript.attributes.add('networkEntity', {type: 'entity'});

GamePlayManagerScript.attributes.add('playerEntity', {type: 'entity'});
GamePlayManagerScript.attributes.add('otherEntity', {type: 'entity'});


// initialize code called once per entity
GamePlayManagerScript.prototype.initialize = function() {
    
    this.app.mouse.disableContextMenu();
    
    this.mainMenuComp.enabled = true;
    this.networkEntity.enabled = false;
    this.playerEntity.enabled = false;
    this.otherEntity.enabled = false;
    this.chatBoxPanelComp.enabled = false;
    
    this.gameStarted = false;
    this.allowMovement = false;
    this.lastMsg = "";
    
    this.nicknameVal = "";
    
    this.playButtonComp.element.on('click', function (evt) {
        
        setTimeout(function(){
            
            this.nicknameVal = this.inputBoxNickNameComp.script.input.getCleanValue();
            // console.log('btn clicked ' , this.nicknameVal);

            if(this.nicknameVal !== "")
            {
                this.playerEntity.findByName('playerNickText').element.text = this.nicknameVal;
                
                this.mainMenuComp.enabled = false;
                this.networkEntity.enabled = true;
                this.playerEntity.enabled = true;
                this.chatBoxPanelComp.enabled = true;
                
                this.gameStarted = true;
                this.allowMovement = true;
            }
            else
            {
                alert("Please enter proper NickName!");
            }
    
        }.bind(this),100);
        
    }, this);
    
};

// update code called every frame
GamePlayManagerScript.prototype.update = function(dt) {
    
    if(this.gameStarted === true)
    {
        if (this.app.keyboard.wasPressed(pc.KEY_ENTER))
        {
            // console.log(this.inputBoxChatMsgComp.script.input.checkFocus());
            
            if(this.inputBoxChatMsgComp.script.input.isFocued() === true)
            {
                var textBoxVal = this.inputBoxChatMsgComp.script.input.getCleanValue();
                
                this.lastMsg = textBoxVal;
                this.inputBoxChatMsgComp.script.input.deleteValue();
                this.inputBoxChatMsgComp.script.input.blur();
                
                if(this.lastMsg !== "")
                {
                    this.networkEntity.script.network.sendMsg(this.nicknameVal,this.lastMsg);
                }
            }
            else
            {
                this.inputBoxChatMsgComp.script.input.focus();
            }
            
            // console.log("this.lastMsg = ", this.lastMsg);
        }
        
        if(this.inputBoxChatMsgComp.script.input.isFocued() === true)
        {
            this.allowMovement = false;
        }
        else
        {
            this.allowMovement = true;
        }
    }
};

var GamePlayManagerScript = pc.createScript('gamePlayManagerScript');

GamePlayManagerScript.prototype.initialize = function() {
    this.playerEntity = this.app.root.findByName('Player');
};

GamePlayManagerScript.prototype.update = function(dt) {
    // check for collisions with damageDemo entities
    var damageDemoEntities = this.app.root.findByTag('damageDemo');
    for (var i = 0; i < damageDemoEntities.length; i++) {
        if (this.playerEntity.collision.collidesWith(damageDemoEntities[i])) {
            // player is hit, decrease health
            this.playerEntity.script.playerHealth.takeDamage(10);
        }
    }
};

GamePlayManagerScript.prototype.addMsgInChatBox = function(nicknameVal, actualMsg) {
    
    // console.log("GamePlayManagerScript.prototype.addMsgInChatBox = ",nicknameVal, actualMsg);
    var msgHolderImageComp = this.msgHolderImageComp.clone();
    msgHolderImageComp.findByName('sentMsgText').element.text = nicknameVal + " : " +actualMsg;
    msgHolderImageComp.tags.add('msgBlock');
    
    this.scrollContentPanelComp.addChild(msgHolderImageComp);
    msgHolderImageComp.enabled = true;
    
    
    if(this.scrollContentPanelComp.findByTag('msgBlock').length * msgHolderImageComp.element.height > 260)
    {
        this.scrollContentPanelComp.element.height = this.scrollContentPanelComp.findByTag('msgBlock').length * msgHolderImageComp.element.height;
        this.verticalScrollbarComp.scrollbar.value = 1;
    }
};

// swap method called for script hot-reloading
// inherit your script state here
// GamePlayManagerScript.prototype.swap = function(old) { };

// to learn more about script anatomy, please read:
// http://developer.playcanvas.com/en/user-manual/scripting/

Please let me know if this is what you mean.

I don’t think there is anything wrong with your logic, you just have to make sure the functions you execute also exist. You still need to check the things I mentoined about your gamePlayManager script.

1 Like

can you help me with the project, please?

What do you need help with?

1 Like

where the script goes, how to make it functional, what code needs to be added/deleted and what I did wrong. Want me to add you to the project?

The problem is that I don’t know the logic and goals of your game, so I can help with solving problems, but I can’t work on the project myself. Another problem is that I can break things too and checkpoints are expensive in storage.

1 Like

Eventually you can create a branch, so I can modify some things without effecting your project.

I still think the best way for you is to look at the things I mentioned and try to correct these things. If you do it yourself, you learn too.

1 Like

I assume as much but I do not know all too well of coding so perhaps the second option for now, then you can explain what and how you did?

1 Like