Impact

This forum is read only and just serves as an archive. If you have any questions, please post them on github.com/phoboslab/impact

8 years ago by orangebread

I'm having trouble loading my EntityPlayer inside my Weltmeister because I'm getting an undefined error in my EntityPlayer that takes a property value defined when client receives socket messages like such:


// socket.js (client-side)
var namerand  = Math.floor(Math.random()*999);
var playername = "player" + namerand;  // Player name defined here
var socket = io.connect('http://localhost:8080');

socket.on('message', function (data) {
    var player = ig.game.getEntitiesByType( EntityPlayer )[0];
    if(player) {
        player.messagebox = player.messagebox + '\n' + data + ' disconnected';
    }
});

And:

// player.js 
EntityPlayer = ig.Entity.extend({
        size: {x: 32, y: 48},
        direction: 1,
        type: ig.Entity.TYPE.A,
        nettimer: 10,
        movementspeed: 100,
        name: "player",
        gamename: playername,
        destinationx:99999999,
        destinationy:99999999,
        messageboxtimer: 200,
        messagebox:'',
        speed: 100,

        checkAgainst: ig.Entity.TYPE.NONE,
        collides: ig.Entity.COLLIDES.PASSIVE,

        animSheet: new ig.AnimationSheet( 'media/player.png', 32, 48 ),

        init: function( x, y, settings ) {
        ... 
       ....

Any ideas on how to fix this? Thanks!

8 years ago by Joncom

Code that should not be run inside Weltmeister can be gated with ig.global.wm.

EntityExample = ig.Entity.extend({
    ...
    init: function(x, y, settings) {
        this.parent(x, y, settings);
        ...
        if(!ig.global.wm) {
            // Connect to socket and do other things that should not be 
            // run inside of Weltmeister.
        }
        ...
    },
    ...
});

8 years ago by orangebread

Awesome, didn't know about that, thank you!

8 years ago by orangebread

@joncom is there a way to do like Dependancy Injection in ImpactJS or some how reference the socket?

8 years ago by Joncom

I usually setup my socket in main.js, and maybe create a pointer to it:

// main.js
MyGame = ig.Game.extend({
    ...
    init: function() {
        ...
        this.socket = ...
    },
    ...
});
...

// player.js
EntityPlayer = ig.Entity.extend({
    ...
    init: function(x, y, settings) {
        this.parent(x, y, settings);
        ...
        if(!ig.global.wm) {
            // Tell the socket server where this player just spawned
            ig.game.socket.emit("player_spawned", this.pos);
        }
        ...
    },
    ...
});

8 years ago by orangebread

Can you describe that a ltitle more? Specifically what goes after this.socket = ...

I'm just wondering how you can code this to keep the files as modular/loosely coupled as much as possible.

Thanks for your responses so far, really appreciated!

8 years ago by Joncom

Have you managed to connect to a socket server OK? Basically this.socket would be whatever object you use to send messages to the socket server. Your game will be somewhat coupled to your main.js ig.game object. This is not the worst thing in the world. Then your entities can call ig.game.socket.emit if necessary.

8 years ago by orangebread

Yes I've setup a node socket server and have a working prototype for multiplayer. But the code is very clunky and I'm looking to clean it up a bit with some better practice so I can expand quickly without worrying about anything breaking. My issue right now is referencing the socket object on client side. Is this what you meant?

this.socket = io.connect('http://localhost:8080');

8 years ago by Joncom

Yup, exactly.

Edit: If you make a new thread and post the code you have, perhaps people could critique it and provide suggestions/pointers?
Page 1 of 1
« first « previous next › last »