1

I'm developing a multiplayer game with Nodejs which will have a Lobby where I'll have all the logic related to hosting and connecting.

Actually I have three files server.js, lobby.js and game.j, the last one is where I want to do the logic of the game, which will run on the server.

When I initialite the server I require both lobby and game.

var Lobby = require("./server/lobby");
var Game= require("./server/game");

And after on creating a room I use a method inside the Lobby object.

socket.on('createRoom',function(roomName){
    Lobby.createRoom(roomName);
});

But into the Lobby when I create a new object game

createRoom: function(name){
    var game = new Game();

It tells me that Game is not defined and shut downs the server... What do I have to do to use the module Game inside Lobby??? it's my first time using nodejs and I'm still a newbie and I don't know how to make it work. :/

Thanks 4 your help!

2 Answers 2

2

You have called it into the scope of the Server module, and can currently only access it from that scope.

You have to require the Game module inside the Lobby module in order to use it there.

Sign up to request clarification or add additional context in comments.

Comments

1

var does not make an object global. In your lobby.js file, add

var Game = require('./game');

to the top of your code.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.