(I’m using Javascript and this has happened on Bonemender and Oasis.)
Is there some rule against starting your code with functions? I tried to build a couple functions to keep things tidy and kept getting an odd error:
The code:
function healUp() {
var pal = this.findNearest(this.findFriends());
if (this.canCast("regen", pal) && this.distanceTo(pal) < 10) {
this.cast("regen", pal);
}}
The error:
Upper or lowercase problem. Try this.findFriends()
Line 2: undefined is not a function.
That was from Bonemender. The error on Oasis was the same except it was for a this.say.
So can anyone tell me: bad coding or ghost in the machine?
inside the function the scope of ‘this’ changes to the function itself.
solve this by defining the variable self at the top of the file then change all of this.something to self.something
var self = this;
function healUp() {
var pal = self.findNearest(self.findFriends());
if (self.canCast("regen", pal) && self.distanceTo(pal) < 10) {
self.cast("regen", pal);
}}
or pass this into your healUp function call wherever that is.
function healUp(self) {
var pal = self.findNearest(self.findFriends());
if (self.canCast("regen", pal) && self.distanceTo(pal) < 10) {
self.cast("regen", pal);
}}
healUp(this);
Hello. I am trying to change the global variable “nearest” inside the function. And then use it outside the function. But my Hero performs strange jumps. Could you help me, please?