I'm creating a nodejs server for Unity3D, using socketio for the networking. I have a function that serves as match room and I want to leave that function when the player leaves the game. Each time the player creates a new room it should enter the function and start over. Is it possible to break execution of parent function from a child callback?
exports.initGame = function (userdata) {
//do prematch stuff with userdata and execute game logic on the callbaks
socket.on("something", function (someNetdata)({
// how do i stop parent function from here?
});
// more socket.on callbacks
}
EDIT: Forgot to mention that the function is being exported. Don't know if that change anything.
EDIT2: Given the suggestions discussed in jfriend00 answer, I'll give a more extended explanation of the problem.
The current flow is as it follows:
user connects and I define some functions on the Io "connected" callback. Relevant one is startMatchMaker(Io, socket, data). The matchMaker creates a room for the player (or joins one if there is one available) and proceed to executing the gameInit(io, socket, room, data) mentioned before.
The gameInit(...) function stores relevant data on global arrays with the room as key, for example, Players[room] = {/* player object */}. The match is 1 vs 1, so when one player abandons the game, I remove the entries from the arrays, referring that room: delete Players[room]. When the player moves, I update the player position on their object inside the global array. this is done on a callback, added inside the gameInit(...)
socket.on("move", function(data){
//do movement logic and update player position, if it is valid. Omitting the rest of the code.
Players[room].position = data.position;
});
Now lets imagine two players, player1 and player2 playing in room1. Player2 abandon the game by exiting the server. Player1 is still connected, but must leave that current game. All the variables are deleted (as mentioned before) and player1 joins a new game in room2. When player1 tries to move, 2 callbacks will be called: the one that was added in the first initGame with room = "room1" and another added upon execution of the second match: room = "room2". A exception is thrown because there is no entry in Players with key = room1.
Given this the solution is to remove the listeners from the previous match.
No. You can't stop execution of a parent function from within a child function. You could create a return value from the child function that would instruct the parent function what to do next and the parent function could then manage itself based on the return value.
But, in your example, initGame() is long since done executing when socket.on('something, ...) is called so your whole ask is just kind of wrong. In fact your event handler CANNOT get called until initGame() is already done. JS is single threaded so initGame() has to finish executing before any event handler can be called.
The sequence of events here is as follows:
initGame() runs.initGame() then finishes executing.initGame() running to even try to break execution of.If you want more concrete help, you will need to show what you're actually trying to accomplish from within your socket.on('something', ...) event handler. The whole notion of "break execution of parent function" from an inner event handler just doesn't even make sense in Javascript. That situation can never happen in Javascript. The parent function is already done executing before the event handler could ever get called. You will have to show the actual problem you're trying to solve for us to help you in more detail.
One thing that seems slightly troubling here is that you reference socket, but that is not passed into initGame(), yet you are exporting initGame() and calling it from some other context so it is unclear how the socket variable could possibly be the right variable.
Your notion of "parent" function is a little confused. There are no "parent functions" in Javascript.
Looking at your example above, it seems like inside the socket.on callback function you want to do something that affects code outside that function. The important thing to recognize here is that it's a callback function - when the socket receives a message identified as something, it calls the callback function. There's no parent/child relationship involved.
So now that's a little clearer, let's consider your problem. You haven't explained exactly what you want to do outside the callback, so it's not clear what function you want stopped. But that's not terribly important - basically, what you're trying to do is signal code outside the callback when something inside the callback happens. There are a number of ways to do this, but one basic technique is to use a variable as a signal.
It is important to note that node.js applications are (usually) event-driven. This means that your code will almost always be in callbacks responding to stuff that is happening - there is no "main loop" that is waiting for stuff to happen.
So your code might look something like this:
exports.initGame = function (userdata) {
// do prematch stuff with userdata and execute game logic on
// the callbacks
// set up a variable to signal when something important happens
var importantSignal = null;
socket.on("something", function (someNetdata)({
// something important has happened! let the rest of the application know about it
importantSignal = someNetdata.message;
// do some other stuff related to the callback
});
// another socket event
socket.on("another", function (someNetData) {
// has something important happened?
if (importantSignal) {
console.log("I see that 'something' has already happened!");
console.log("Message = " + importantSignal);
}
});
}
This is a simple type of message passing between callbacks - notice that the second callback has access to some state that was set in the first callback. But there's no sense in which the code in the first callback "controls" the second callback, except insofar as the second callback makes decisions based on state set in the first callback.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With