AmAuron Posted July 28, 2015 Share Posted July 28, 2015 Hi there, sorry to bother you guys with maybe a simple mistake, but i'm doing this for at least 8 hours without sucess, if you guys can help me out, would be awesome... So i have a server, and i'm using jquery to retrieve the information from the server, the problem is...i get the information but it doesn't save it to my variable, and i kinda need the information on the variable. Here's part of my code so you understand the problem: I tried 2 ways to retrieve the data... this is the current way, where he says SaveDataonVar is not a function...TheGame.MainMenu= function() { this.recieved = false; //declaring an empty userfound this.userfound; };TheGame.MainMenu.prototype = { init: function(identifier){ this.identifier = identifier; }, preload: function(){ this.FetchData(); }, update: function(){ if(this.recieved == true) { this.hellouser = this.game.add.text(0,0,'Hello' + this.userfound.name); this.hellouser.font = 'Arial'; this.hellouser.fontSize = 18; this.hellouser.stroke = '#FFFFFF'; this.hellouser.aligh = 'center'; this.hellouser.fill = '#FFFFFF'; this.hellouser.update(); this.hellouser.updateText(); this.hellouser.x = this.game.world.centerX; this.hellouser.y = 40; } }, FetchData: function(){ $.ajax({ type: "POST", url: 'http://localhost:1337/user/find', data: { id: this.indentifier }, success: function(data, status, xhr){ if (data){ this.SaveDataonVar(data); } } }); }, SaveDataonVar: function(data){ this.recieved = true; this.userfound = data; console.log('user found has the value :' + this.userfound); } and this way where the code doens't give me an error, but still the variable userfound isn't change outside the function only inside.FetchData: function(){ $.ajax({ type: "POST", url: 'http://localhost:1337/user/find', data: { id: this.indentifier }, success: function(data, status, xhr){ if (data){ function SaveDataonVar(data){ this.recieved = true; this.userfound = data; console.log('user found has the value :' + this.userfound); } } }); }Can someone help me?I know that maybe this is some newbie mistake... Thank you for your time to read this topic and thank you for your answers in advance. Link to comment Share on other sites More sharing options...
Skeptron Posted July 28, 2015 Share Posted July 28, 2015 I think that both times you are having scope issues, that is, the variables you think you target are in fact new, function-local variables (with the same names). Try declaring userfound as a global variable and use solution n2, and you should see it change. Be careful when using functions inside functions, the scope of the vars is very different from other programming languages. AmAuron 1 Link to comment Share on other sites More sharing options...
AmAuron Posted July 28, 2015 Author Share Posted July 28, 2015 I think that both times you are having scope issues, that is, the variables you think you target are in fact new, function-local variables (with the same names). Try declaring userfound as a global variable and use solution n2, and you should see it change. Be careful when using functions inside functions, the scope of the vars is very different from other programming languages.Thank you very much! This solved it, one more question...is there any way of making this work with a variable on the state (this.variableonthestate)? Link to comment Share on other sites More sharing options...
Skeptron Posted July 28, 2015 Share Posted July 28, 2015 Yes. See my answer to that post for the solution : http://www.html5gamedevs.com/topic/14602-call-method-inside-method/ AmAuron 1 Link to comment Share on other sites More sharing options...
AmAuron Posted July 29, 2015 Author Share Posted July 29, 2015 Thank you, i will try this out Link to comment Share on other sites More sharing options...
Recommended Posts