[solved] How can I access behinstProto variable?

0 favourites
  • 5 posts
From the Asset Store
Easily store, modify, read and manipulate colors with Color Variables!
  • 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:

    this.gravFactor = this.properties[8];[/code:hwl4jqr4]
    
    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:hwl4jqr4]Box2D.Dynamics.b2Island.prototype._InitializeVelocities = function(step, gravity)[/code:hwl4jqr4]
    
    Getting this to work would be magic (and would open up a world of buoyant objects with different gravity values...).  Thanks!
  • 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:

    var _this;
    instanceProto.onCreate = function () {
    _this = this;
    this.gravFactor = this.properties[8];
    ....
    }
    
    Box2D.Dynamics.b2Island.prototype._InitializeVelocities = function(step, gravity){
     alert(_this.gravFactor);
    ....
    }
    [/code:348r2k3p]
    
    Use _this inside the Box2D functions
  • 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:

    > var _this;
    instanceProto.onCreate = function () {
    _this = this;
    this.gravFactor = this.properties[8];
    ....
    }
    
    Box2D.Dynamics.b2Island.prototype._InitializeVelocities = function(step, gravity){
     alert(_this.gravFactor);
    ....
    }
    [/code:1bq1ng6p]
    
    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 .

  • 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!

  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • Solved it! No globals required - just access "c2userdata" and all becomes available.

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)