Jump to content

Pass a variable to an input callback function


ForgeableSum
 Share

Recommended Posts

When I create a callback function on input, I want to pass a variable to it at the time I create it. Is there a way to do that? This doesn't work:

var objectCategory = 'test';game.input.addMoveCallback(function(pointer, x, y,state,objectCategory) {// objectCategory is undefined});

I'm sure there is something fundamental I am missing here ...

Link to comment
Share on other sites

Phaser won't allow you to do it but Function#bind will. It's built-in to JavaScript and is probably supported by a browser near you. It would change your function definition a bit, though, since your bound params would come first before the ones Phaser passes in:

function onInputMove(objectCategory, pointer, x, y) { ... }game.input.addMoveCallback(onInputMove.bind(null, objectCategory));

I'm not sure this is what you're asking though. Is there a specific Phaser "objectCategory" that you want here, or will that code snippet do it?

Link to comment
Share on other sites

Phaser won't allow you to do it but Function#bind will. It's built-in to JavaScript and is probably supported by a browser near you. It would change your function definition a bit, though, since your bound params would come first before the ones Phaser passes in:

function onInputMove(objectCategory, pointer, x, y) { ... }game.input.addMoveCallback(onInputMove.bind(null, objectCategory));

I'm not sure this is what you're asking though. Is there a specific Phaser "objectCategory" that you want here, or will that code snippet do it?

The code snippet will do it. I'm really looking to pass any variable (I define) into the callback from within the function defining the callback. So:

function test(variable) {game.input.addMoveCallback(function(pointer, x, y,state,variable) {console.log('need ' + variable + ' here'); });}var variable = 'test';test(variable); 

I'm surprised that there's not a less-hacky way to do this. I suppose I could rely on global variables ...

Link to comment
Share on other sites

Wow, This is the third post I saw this question being asked in... surprised there isnt an answer yet.. I will try it and report back..

 

 btn1DollarSign.on('click', {
            name: "btn1DollarSign"
        }, noop)
 
    function noop() {        console.log("Hello " + event.data.name);        console.log("Hello " + name);        // console.log("X = " + event.data.originalEvent.target);        //}
 With no success :(
Edited by alwayzambitious
Link to comment
Share on other sites

If you can see the variable objectCategory from the function you don't need to pass it as an argument. If you want to use the variable in a function from another object or another source file then you can use a wrapper function.

var objectCategory = 'test';game.input.addMoveCallback(function(pointer, x, y, state) {    // objectCategory is visible inside here    realFunction(pointer, x, y, state, objectCategory);});
Link to comment
Share on other sites

If you can see the variable objectCategory from the function you don't need to pass it as an argument.

 

 

But that's the problem. I can't see objectCategory from the function. At least when that callback fires, it shows up as undefined:

function someFunction() {var objectCategory = 'test';game.input.addMoveCallback(function(pointer, x, y,state,objectCategory) {// objectCategory is undefined});}someFunction(); 
Link to comment
Share on other sites

Hi, try to add "this" as second parameter when setting callback function. If you do not, your callback is called but with incorrect context. If you pass "this" then it will be called with correct context and you should be able to use your variable.

function someFunction() {  var objectCategory = 'test';  game.input.addMoveCallback(function(pointer, x, y,state) {    // objectCategory should be visible now with correct context    console.log(objectCategory);  }, this);}
Link to comment
Share on other sites

You can pass a context to a callback in Phaser. There are two ways that probably will solve your problem. 

The first one is passing the context of the object and setting the variable to public as a property of the object.

The problem with the Tom Atom solution is that objectCategory is "private" so passing the context won't help unless you put the variable as a property of the object, see the example:

function probalbyCreateFunction() {   this.objectCategory = 'test';   game.input.addMoveCallback(function(pointer, x, y, state) {       console.log(this.objectCategory);   }, this);  }

You could also pass the variable as the context, so inside the callback, this would be the variable:
 

function probalbyCreateFunction() {   objectCategory = 'test';   game.input.addMoveCallback(function(pointer, x, y, state) {       console.log(this);   }, objectCategory);  }
Link to comment
Share on other sites

But that's the problem. I can't see objectCategory from the function. At least when that callback fires, it shows up as undefined:

function someFunction() {var objectCategory = 'test';game.input.addMoveCallback(function(pointer, x, y,state,objectCategory) {// objectCategory is undefined});}someFunction(); 

Drhayes mentions it, what you are doing is overwriting the objectCategory variable. Don't pass it in the parameter list and you should be able to use it. I recommend looking up some details about JavaScript scopes.

Link to comment
Share on other sites

  • 2 weeks later...

 

You can pass a context to a callback in Phaser. There are two ways that probably will solve your problem. 

The first one is passing the context of the object and setting the variable to public as a property of the object.

The problem with the Tom Atom solution is that objectCategory is "private" so passing the context won't help unless you put the variable as a property of the object, see the example:

function probalbyCreateFunction() {   this.objectCategory = 'test';   game.input.addMoveCallback(function(pointer, x, y, state) {       console.log(this.objectCategory);   }, this);  }

You could also pass the variable as the context, so inside the callback, this would be the variable:

 

function probalbyCreateFunction() {   objectCategory = 'test';   game.input.addMoveCallback(function(pointer, x, y, state) {       console.log(this);   }, objectCategory);  }

Had to dig up this thread tonight because I ran into the problem again. Your solution worked swimmingly. Thanks :)

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...