Same attribute can be accessed in one method and couldnt in another

Hi hi,

I cannot work and access any attributes in one method, however, I can call other methods through this.
Even the console.log() raises this error. Attributes can be accessed normally in other methods.

UpdateHealthEntity causes the error as well as the method it gets called in. However, everything works perfectly in lerp players.
image

Could it be that I am calling this method before the attributes are initialized?
Here is the link to the project PlayCanvas 3D HTML5 Game Engine
The issue is in client.js

Thank you in advance.

Hey @Daniil_KhoKho , You haven’t parsed the client.js script due to which this.healthBarLenght doesn’t take any value from editor.
image
image

Parse it and then try to run it again.

1 Like

Just out of curiousity. Is your attribute called healthBarLenght or healthBarLength? In addition to @saif’s answer, it could just be a case of a typo

1 Like

I rechecked the spelling and also reuse other existing attributes before - same result.

I parsed and tried again - same result. I found that the error occurs in the manageServerData function. Putting any of the attributes inside produces the same error. They work completely normal in other methods.

I can call other methods from this method but cannot do attributes. I cannot even try to check if undefined - it provides this error.

image
Player is a reference to entity here, I have parse the script.

I call the method every time the socket receives the frame call. I suspect maybe its due to the fact that it gets called somehow before the attributes are initialized. But I still have no clue tbh. This being said I can continue with the project but I still would be interested if someone can explain why this occurs.

Would it be possible to share the project to take a look? It sounds like you might have a scope issue where the this object isn’t the script instance on your object (Especially since you are working with Sockets), but without seeing the setup, I can’t say for sure.

I have created a fork and shared it with you.

Just took a look. It’s definitely a scope issue. I’ll create a fork, and suggest some corrections and pass it back to you

1 Like

Thank you very much.

Since you gave me write access on your fork, I went ahead and pasted my edits there, commenting out the original code so you can see what I did. It seemed to work when I tested my fork. If it works for you, I will delete the fork I made.

2 Likes

It works! So basically the reason was that by calling the method in socket this had another meaning due to scope?

1 Like

That’s right! I’ll go ahead and delete my fork.

For anyone else running into something similar, the edits I made on @Daniil_KhoKho 's project look something like this:

var SomeScript = pc.createScript('someScript');

SomeScript.prototype.initialize = function() {

	this.socket = io.connect('https://myVerySpecialSocketProject.glitch.me'); //Example domain

	this.socket.on('event1', function(data1) {
		this.onEvent1(data1);
	}.bind(this));

};

SomeScript.prototype.onEvent1 = function(data1) {

};

This ensures that you are calling the correct script instance when calling upon this

Another, ‘spicier’ option that I discovered recently is to do something like this:

var SomeScript = pc.createScript('someScript');

SomeScript.prototype.initialize = function() {

    this.socket = io.connect('https://myVerySpecialSocketProject.glitch.me'); //Example domain	

	this.onEvent2 = this.onEvent2.bind(this);

	this.socket.on('event2', this.onEvent2);
};

SomeScript.prototype.onEvent2 = function(data2) {

};

This second option might look a bit cleaner and work, but I personally prefer the first option as it is easier for me to follow.

1 Like