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

1 decade ago by ShawnSwander

This isn't within impact Its on my server so I can't just declare it under ig.game but I need to pass from_sql out of the function its in and my attempt at return didn't work perhaps someone could point out what I need to change to do this?

If you scroll down to the comment
 //lost it

that is where I can't pass the variable.
   socket.on('moveitem', function (player_id,itemlist_id,item_id,move_from,move_to,posx,posy,land){
      connection.escape(player_id,itemlist_id,item_id,move_from,move_to,posx,posy,land)
      console.log(player_id,itemlist_id,item_id,move_from,move_to)
      
      var to_sql = null;
      var from_sql = null;
      var to_data = null;
      var from_data = null;
      
      sql ="SELECT next_turn,action_time,posx,posy,land FROM game_moblist WHERE id = "+player_id;
      connection.query(sql, function(err, rows, fields) {if (err) throw err
         if ((rows[0])&&rows[0].next_turn < new Date().getTime()){
            if (move_from!=move_to&&move_to!=null){
               sql="UPDATE game_moblist SET `next_turn`="+(new Date().getTime()+rows[0].action_time);
               connection.query(sql, function(err, rows, fields) {if (err) throw err;});
               switch(move_from){
               case 0://ground
                  //check if its last item in hex here
                  from_sql = "DELETE FROM `game_itemlist` WHERE `id` ="+itemlist_id;
               break;
               case 1://sack
                  sql = "SELECT sack FROM `game_moblist` WHERE id="+player_id;
                  connection.query(sql, function(error, rows, fields){
                     var sack = rows[0].sack.split(",")
                     for (i=0;i<sack.length;i++){
                        if (sack[i]==item_id.toString()){
                           sack[i]="0";
                           sack = sack.join();   
                           from_sql="UPDATE game_moblist SET sack='"+sack+"' WHERE id="+player_id
                           i=sack.length
                        }
                     }
                     
                  });
                  //lost it
                  console.log(from_sql)
               break;
               }
      
               switch(move_to){
               case 0:
                  to_sql="INSERT INTO game_itemlist (id,posx,posy,land) VALUES "+item_id+","+posx+","+posy+",'"+land+"'";
               break;
               case 1://sack
                  sql ="SELECT sack FROM `game_moblist` WHERE id="+player_id;
                  connection.query(sql, function(error, rows, fields){
                     var sack = rows[0].sack.split(",")
                        for (i=0;i<sack.length;i++){
                           if (sack[i]=="0"){
                              if(typeof item_id === "number"){
                                 sack[i]= item_id.toString()
                                 sack = sack.join();   
                                 to_sql ="UPDATE game_moblist SET sack='"+sack+"' WHERE id="+player_id
                                 i=sack.length
                                 to_data = sack;
                              }
                           }
                        }
                  });
               break;
               }
                  console.log(from_sql,to_sql)
      
               if (from_sql&&to_sql){
                  connection.query(from_sql, function(error, rows, fields){if (error) socket.emit('error', error);})
                  connection.query(to_sql, function(error, rows, fields){if (error) socket.emit('error', error);})
                  io.sockets.emit('moveitemresult',player_id,itemlist_id,item_id,move_from,move_to,to_data )
               }
            }
         }
      });
   });

1 decade ago by ShawnSwander

I feel more confused than when I started.

If I could simplify my question it might help.

connect.query(arg1,function(error,rows,fields){
  //get info I need for setting variable
   var I_need_this = true;
});
//I need to access that variable here outside the function

This doesn't work
var I_need_this
connect.query(arg1,function(error,rows,fields){
  //get info I need for setting variable
   var I_need_this = true;
});

This doesn't either
var I_need_this
connect.query(arg1,container = function(error,rows,fields){
  //get info I need for setting variable
   var I_need_this = true;
return I_need_this; 
});

Nor this
container =connect.query(arg1,container = function(error,rows,fields){
  //get info I need for setting variable
   var I_need_this = true;
return I_need_this; 
});

1 decade ago by Heiko

Try something like the following:

connect.query(arg1,function(error,rows,fields){
  //get info I need for setting variable
   this.I_need_this = true;
}.bind(this));

You can bind to any context, e.g. another object that has the callback function defined, along with I_need_this as a data member.

If bind not supported, then have a look at call or apply.
You can also use javascript closures but the syntax if a 'bit' more complicated.

(bind might only be supported from ECMASCRIPT 5, not sure there)

1 decade ago by jswart

Don't use 'var' when you define your variable.


No:

var I_need_this = 0;

---

Yes:

I_need_this = 0;

http://stackoverflow.com/questions/1470488/difference-between-using-var-and-not-using-var-in-javascript

1 decade ago by Heiko

Don't use global namespace if it can be avoided.
i.e. do use var.

* Making variables global is a hack (in most cases)

* Excluding var might not make your variable global - see nodejs implementation where it will only make your variable global to the current package/module, NOT to the rest of the code.

* Dominic also mentioned something about implementing namespaces in future version - this might also prevent variables without var defaulting to the same global namespace.

* If you use global variables/functions there is a chance that these globals might clash with other scripts defining the same globals, or that new/future webstandards might add functions/variables with the same name

1 decade ago by jswart

Quote from Heiko
Don't use global namespace if it can be avoided.
i.e. do use var.

* Making variables global is a hack (in most cases)

* Excluding var might not make your variable global - see nodejs implementation where it will only make your variable global to the current package/module, NOT to the rest of the code.

* Dominic also mentioned something about implementing namespaces in future version - this might also prevent variables without var defaulting to the same global namespace.

* If you use global variables/functions there is a chance that these globals might clash with other scripts defining the same globals, or that new/future webstandards might add functions/variables with the same name


Everything you said is valid, and a good point. My original answer was in fact a hack.

The other way and likely better is to do something like this:

myObject = ig.Entity.extend({

    var myVar = 'foo',
  
    firstFunction: function() {
        this.myVar = 'bar';
    },

   secondFunction: function() {
        someVar = this.myVar;
    },

});

use ' this.myVar ' to get access to the variable in the scope of the class you are working on.
Page 1 of 1
« first « previous next › last »