Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6930559
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T11:27:46+00:00 2026-05-27T11:27:46+00:00

I`m trying to make a small game on node.js + socket.io + mysql. The

  • 0

I`m trying to make a small game on node.js + socket.io + mysql.

The node.js server reads the database info about the field and items on it(40×40). This info it keeps itself, and gives it on request.

The problem is that if you simply send a request, the info comes. But if you try to do something with this information after it came, for example to put the sectors and objects on them in arrays, request doesnt work. I tried to put a timeout before the function to allow time to get info; i tried to put it on a button. Didn’t help. The code which blocks everything is in client.html between /* and */. It works great without this code.

  • server.js:

    var mysql = require('mysql');
    var io = require('socket.io').listen(8080);
    var TESTGAME = 'testgame';
    var TABLES_MAP1 = 'map1';
    var client = mysql.createClient({
      user: 'root',
      password: '',
    });
    
    client.query('USE '+TESTGAME);
    
    client.query(
      ('SELECT * FROM '+TABLES_MAP1+' WHERE id_sector_x < 40 && id_sector_y < 40'),
      function(err, results, fields) {
        if (err) {throw err;}
    
        Map1 = (results);
    
      io.sockets.on('connection', function (socket) {
      socket.emit('info', Map1);
      socket.on('back', function (data) {
        console.log(data);
      });
    });
    
        client.end();
      }
    );
    
  • client.html:

    <!doctype html>
    <html>
    <head>
        <meta charset="utf-8">
        <title>Map</title>
        <script src="http://127.0.0.1:8080/socket.io/socket.io.js"></script>
        <script src="json.js" type="text/javascript"></script>
        <script type='text/javascript'>
        window.onload = function() {
            var socket = io.connect('http://127.0.0.1:8080');
    
            socket.on('info', function (data) {
                Mappp = (data);
                socket.emit('back', Mappp.length);
            });
        };
    
        Map1=Mappp;
        function mapp(){
            m=0; //As info looks like this [1600] i need to make it look like this [40][40]. So this m will be m++ on each iteration to know what info should be put in.
            for (x=0;x<40;x++){
                for (y=0;y<40;y++){
                    /*
                    switch (Map1[m].type_sector) {
                       case "grass":
                          map[x][y]=0;
                          [break]
                       case "water":
                          map[x][y]=1;
                          [break]
                       case "swamp":
                          map[x][y]=2;
                          [break]
                       default:
                          map[x][y]=0;
                          [break]
                    };
    
                    switch (Map1[m].obj_sector) {
                       case "no":
                          objectMap[x][y]=0;
                          [break]
                       case "wall":
                          objectMap[x][y]=1;
                          [break]
                       default:
                          objectMap[x][y]=0;
                          [break]
                    }; */
                    m++;
                                      }
                              }
            window.alert('Done');       }
        setTimeout(mapp, 1000);
    </script>
    <form name="">
        <input type="submit" value="call mapp"
        onclick=mapp()>
        </form>
    </head>
    <body>
    <script type="text/javascript">
    </script>
    </body>
    </html>
    

Sorry for my English 🙂

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T11:27:47+00:00Added an answer on May 27, 2026 at 11:27 am

    You’re dealing with an asynchronous request. You won’t be able to set Map1=Mappp; until after the request returns and your callback function is executed (sidenote: please, please use var statements). You sort of address this with your timeout, but because the mapp() function depends on Map1 being set synchronously, even if the request has returned and Mappp is set, mapp() will still fail.

    To fix this, you need to make sure that anything that requires the data returned by your request isn’t executed until the request returns – that’s what the callback function is for. The easiest way to do this with your current code is probably to dispense with the Mappp and Map1 variables entirely and pass the returned data to mapp() as a function argument, invoking the function in the callback:

    socket.on('info', function (data) {
        socket.emit('back', data.length);
        mapp(data);
    });
    

    You’d need to adjust the mapp() function for this to work:

    function mapp(Map1){
        // etc
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've been trying to get input with IUP to make a small pong game.
I'm trying to make a small Flash game that has a GUI, which is
I'm trying to make a small game that supports Python scripting. I've no problems
I am trying to make a small game like app that has some interaction,
I'm trying to make a small game using both SFML and Box2D. I have
I'm trying to make a small game using (free)GLUT. I know that it's old
I am trying to make a small C utility that reads in a MP3
So I am trying to make a small game for a project in college.
I'm trying to make a small android Jump and Run game but my problem
I'm trying to make a small game based on the canvas in Delphi. Basically,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.