Access Mouse Expression from Another Plugin

  • It's me again!

    This time I'm attempting to access the Mouse object's X & Y expressions from a custom plugin.

    In my project, I have the Mouse plugin and my custom plugin loaded. From the custom plugin, I am attempting to access the expressions from a condition. I am using the following lines:

        var mouseX;

        cr.plugins_.Mouse.prototype.exps.X(mouseX, 0);

    I receive the following error when the condition is executed:

        Javascript error!

        TypeError: this.runtime is undefined

    localhost/Mouse_plugin.js, line 339

        This is either a bug in Construct 2 or a problem in a third party    

        plugin or behavior - please report it to the developer!

    Line 339 of the Mouse plugin is:

        layer = this.runtime.getLayerByNumber(layerparam);

    I only have 1 layer in the project (layer 0).

    Any assistance would be appreciated.

    Thanks!

    EDIT:

    So it became obvious that I am not working with an instance of the Mouse object, thus the errors. But I can't seem to figure out how to obtain a reference to the instance. I've followed rexrainbow's logic in this post: scirra.com/forum/behavior-accessing-a-plugin_topic50811.html, but it isn't working for me.

    Again, any assistance would be appreciated.

  • Wastrel

    Uh, I only could run "action" of plugin, not "expression", since the expression will put return value on it's first parameter (ret.set_int(3), for example), not really return a value (return 3).

    I also have the same issue before, I don't want to repeat mouse/touch in my behaviors because that I need more effort to maintain.

    So I made a touch wrap, just copy from official touch plugin then add some functions for passing some information outside. So other behaviors could just get events or information from this touch wrap (see cursor2 for more detail).

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • Uh, I only could run "action" of plugin, not "expression", since the expression will put return value on it's first parameter (ret.set_int(3), for example), not really return a value (return 3).

    I'm not sure I understand the difference between the two, which may be part of my problem. :D Every time I learn something new in JS, 10 more things pop up that I don't understand.

    I also have the same issue before, I don't want to repeat mouse/touch in my behaviors because that I need more effort to maintain.

    So I made a touch wrap, just copy from official touch plugin then add some functions for passing some information outside. So other behaviors could just get events or information from this touch wrap...

    I assumed we were not supposed to use code from built-in plugins, based on what Ashley said before. If we can use this code in our custom plugins, then that makes it a lot easier.

    I guess I still don't understand why I'm unable to get a reference to the existing Mouse instance and use the expressions in the instance to get the information.

    Thanks for your reply!

  • Expression is to get a return value, it put return value at ret.set_int() for example, not by "return". You might trace engine to see how engine to get epxression return.

    Behavior with touch/mouse input always repeat touch/mouse listening code. I try not to repeat these code, but I could not get information (I need not only action, but also condition and expression) from touch plugin, so I made a wrap to let my behavior to get touch/mouse information.

    User need to put more one plugin to support my behavior. It will be inconvenient for user.

  • It is relatively difficult to call another object's expression, because you need:

    1) an instance to call it on - you don't pass any instance, so 'this' in the expression method is not set and ends up being the global scope. Since the method uses 'this' to refer to the object instance, the method fails and causes the javascript error.

    2) as rexrainbow mentioned you need to pass a 'ret' parameter in order to receive the result. This is undocumented and you shouldn't really be doing this in the first place.

    Basically, the easiest thing to do is to copy and paste the code from the Mouse plugin.

  • Wastrel

    You might get touch/mouse expression from my touchwrap plugin. A copy of code from touch plugin is better if you don't want to have dependence.

  • Ashley & rexrainbow, thanks for your help.

    I think I had a misunderstanding about our limitations on using built-in plugin code in our custom plugins.

    I will just include the code I need, and not try to call the expression.

    Thanks!

  • Hey Wastrel ,

    I found a way to get return value from expression.

    1. create a fake ret instance like

    fake_ret = {value:0,
                               set_any: function(value){this.value=value;},
                               set_int: function(value){this.value=value;},                           
                              };

    2. use it with "apply" (or "call") like

    cr.plugins_.Function.prototype.exps.ReturnValue.apply(plugin_obj, [fake_ret])

    3. get return value like

    fake_ret.value
  • Nice discovery, rexrainbow! Thanks for the update.

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