[SOLVED] Problems with multiplayer movement


#1

so im making a multiplayer game and ive run into a problem once i login
(to test this use two tabs)
well once i login in both sry ull have to make an account but this is using just a server for tests so the data will be reset once i fix this
so its throwing this error

[Network.js?id=15526278&branchId=95cb14a6-7570-4738-92ed-01bbf4229117:60]: Uncaught TypeError: Cannot read property ‘rigidbody’ of null

TypeError: Cannot read property ‘rigidbody’ of null
at script.Network.updateplayers (https://launch.playcanvas.com/api/assets/files/scripts/Network.js?id=15526278&branchId=95cb14a6-7570-4738-92ed-01bbf4229117:60:40)
at r. (https://launch.playcanvas.com/api/assets/files/scripts/Network.js?id=15526278&branchId=95cb14a6-7570-4738-92ed-01bbf4229117:20:14)
at r.emit (https://launch.playcanvas.com/api/assets/files/scripts/socket.js?id=15702925&branchId=95cb14a6-7570-4738-92ed-01bbf4229117:4:12375)
at r.onevent (https://launch.playcanvas.com/api/assets/files/scripts/socket.js?id=15702925&branchId=95cb14a6-7570-4738-92ed-01bbf4229117:5:26293)
at r.onpacket (https://launch.playcanvas.com/api/assets/files/scripts/socket.js?id=15702925&branchId=95cb14a6-7570-4738-92ed-01bbf4229117:5:25915)
at r. (https://launch.playcanvas.com/api/assets/files/scripts/socket.js?id=15702925&branchId=95cb14a6-7570-4738-92ed-01bbf4229117:5:28033)
at r.emit (https://launch.playcanvas.com/api/assets/files/scripts/socket.js?id=15702925&branchId=95cb14a6-7570-4738-92ed-01bbf4229117:4:12375)
at r.ondecoded (https://launch.playcanvas.com/api/assets/files/scripts/socket.js?id=15702925&branchId=95cb14a6-7570-4738-92ed-01bbf4229117:4:18962)
at s. (https://launch.playcanvas.com/api/assets/files/scripts/socket.js?id=15702925&branchId=95cb14a6-7570-4738-92ed-01bbf4229117:5:28033)
at s.r.emit (https://launch.playcanvas.com/api/assets/files/scripts/socket.js?id=15702925&branchId=95cb14a6-7570-4738-92ed-01bbf4229117:4:12

375)

and i’ve tried to fix it several times and keep it working but so far its not worked.
help would be appreciated
https://playcanvas.com/editor/scene/662886

edit: i made it stop throwing the error but now it wont update where the players are when they move


#2

i made it stop throwing the error but now it wont update where the players are when they move


#3

Could this be your problem?

https://developer.playcanvas.com/en/user-manual/physics/#teleporting-rigid-bodies


#4

no as this is the script
and i have commented out the line throwing error

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

// static variables
Network.id = null;
Network.socket = null;

Network.attributes.add("player",{type:"entity"});
Network.attributes.add("other",{type:"entity"});
// initialize code called once per entity
Network.prototype.initialize = function() {
    var socket = io.connect('https://big-vinyl.glitch.me'); // Glitch hosted server
    Network.socket = socket;
    var self = this;
    socket.on("in game players",function(data){
        self.inital(data);
    });

    console.log(self.loggedin);
    socket.on("ingame players moved",function(data){
        self.updateplayers(data);    
    }); 

    socket.on("leave",function(data){
        self.removePlayer(data);   
    });
};
Network.prototype.inital = function(data){
    console.log("joining");
    console.table(data);
    this.igplayers = data.onlineplayers;
    Network.id = data.id;
    for(var id in this.igplayers){
        //this.igplayers[id].deleted = false;
        if(this.igplayers[id].id!==Network.id){
            console.log("creating player entity");
            this.igplayers[id].entity = this.createplentity(this.igplayers[id]);
        }
    }
    this.init = true;
};
Network.prototype.createplentity = function(data){
    var newpl = this.other.clone();
    newpl.enabled = true;
    this.other.getParent().addChild(newpl);
    this.plname = newpl.findByName('player name');
    this.plname.element.text = data.user;
    console.log("creating player entity ",newpl);
    
    if(data){
        newpl.rigidbody.teleport(data.x,data.y,data.z);}
    
    return newpl;
};
Network.prototype.updateplayers = function(data){
    if (this.init && !this.igplayers[data.id].deleted&&data.id!==Network.id) {
        console.log(data);
        console.table(this.igplayers);
        console.log(this.igplayers[data.id]);
        this.igplayers[data.id].entity.rigidbody.teleport(data.x, data.y, data.z);//here's where the error was being thrown
    }
};
Network.prototype.removePlayer = function (data) {
    if (this.igplayers[data].entity&&data.id!==Network.id) {
        this.igplayers[data].entity.destroy();
        delete this.igplayers[data];
    }
};

Network.prototype.update = function(dt) {
    this.updatePosition();
};

Network.prototype.updatePosition = function () {
    if (this.init) {    
    var pos = this.player.getPosition();
        Network.socket.emit('player moved', {id: Network.id, x: pos.x, y: pos.y, z: pos.z});
    }
};


#5

solved i figured out i had everything correct except every time someone else joined everyone else who was on was redefining their id to be that persons causing some problems which was easily fixed by making sure it was only defined once thanks for the halp and merry christmas