[solved] How can I access behinstProto variable?

For developers using the Construct 2 Javascript SDK

Post » Sat Jan 14, 2017 7:21 pm

Hello! I hope you might be able to help me with a bit of plugin manipulation. I'm slowly honing my understanding of Javascript and the SDK but I've come up against a bit of a mental brick-wall. The problem could be as much to do with my limited understanding of the Box2D Physics plugin as much as my superficial knowledge of Javascript....

Here's the problem:

When I create an object with my behaviour, it receives a variable via the OnCreate function, from edittime.js, thus:

Code: Select all
this.gravFactor = this.properties[8];


So far, so good. Now, how can I access that instance variable gravFactor inside a Box2d function (my understanding of OOP is beaten here and I am about to resort to trial and error!):

Code: Select all
Box2D.Dynamics.b2Island.prototype._InitializeVelocities = function(step, gravity)


Getting this to work would be magic (and would open up a world of buoyant objects with different gravity values...). Thanks!
Last edited by Colludium on Sun Jan 15, 2017 7:20 am, edited 1 time in total.
I only occasionally visit - I'm learning C# for Unity, but c2 is still a respectable game engine imo....
B
73
S
19
G
66
Posts: 2,198
Reputation: 42,188

Post » Sat Jan 14, 2017 8:02 pm

@Colludium The reason why it doesn't work is because the statement "this" is encapsulated inside the instance prototypes scopes. So it cannot pass the borders towards other functions, therfore you should pass it as a paramter if you're planning to make your own function outside the .prototypes. If it is a function of a library such as Box2D , which is the case here, you should assign it to a global variable declared outside the scopes.
Like this:
Code: Select all
var _this;
instanceProto.onCreate = function () {
_this = this;
this.gravFactor = this.properties[8];
....
}

Box2D.Dynamics.b2Island.prototype._InitializeVelocities = function(step, gravity){
 alert(_this.gravFactor);
....
}


Use _this inside the Box2D functions
Banned User
B
17
S
7
G
23
Posts: 388
Reputation: 13,964

Post » Sat Jan 14, 2017 8:34 pm

X3M wrote:@Colludium The reason why it doesn't work is because the statement "this" is encapsulated inside the instance prototypes scopes. So it cannot pass the borders towards other functions, therfore you should pass it as a paramter if you're planning to make your own function outside the .prototypes. If it is a function of a library such as Box2D , which is the case here, you should assign it to a global variable declared outside the scopes.
Like this:
Code: Select all
var _this;
instanceProto.onCreate = function () {
_this = this;
this.gravFactor = this.properties[8];
....
}

Box2D.Dynamics.b2Island.prototype._InitializeVelocities = function(step, gravity){
 alert(_this.gravFactor);
....
}


Use _this inside the Box2D functions



@X3M - thank you. I was focusing too much on trying to find a way to reverse-navigate up the scopes somehow. I'll give it a go now - it looks perfect and perfectly simple :).
I only occasionally visit - I'm learning C# for Unity, but c2 is still a respectable game engine imo....
B
73
S
19
G
66
Posts: 2,198
Reputation: 42,188

Post » Sat Jan 14, 2017 9:15 pm

@X3M, I'm afraid I can't get it to work. I don't know how to correlate "_this" with "this" in the scope of the "Box2D...Velocities" function? Looking through the array "_this" it appears that it's populated with values from only one object rather than arrays for each one that hold's the behaviour I'm testing.

As an aside, there's a lot more data included in "_this" than I ever expected...!! Thanks!
I only occasionally visit - I'm learning C# for Unity, but c2 is still a respectable game engine imo....
B
73
S
19
G
66
Posts: 2,198
Reputation: 42,188

Post » Sun Jan 15, 2017 7:21 am

Solved it! No globals required - just access "c2userdata" and all becomes available.
I only occasionally visit - I'm learning C# for Unity, but c2 is still a respectable game engine imo....
B
73
S
19
G
66
Posts: 2,198
Reputation: 42,188


Return to Javascript SDK

Who is online

Users browsing this forum: No registered users and 0 guests