Access Mouse Expression from Another Plugin

For developers using the Construct 2 Javascript SDK

Post » Mon Jul 23, 2012 9:22 pm

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
    http://localhost:50000/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: http://www.scirra.com/forum/behavior-accessing-a-plugin_topic50811.html, but it isn't working for me.

Again, any assistance would be appreciated.Wastrel2012-07-24 15:10:23
Don't see the fnords and they won't eat you!
B
75
S
16
G
12
Posts: 322
Reputation: 11,608

Post » Wed Jul 25, 2012 5:39 am

@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).rexrainbow2012-07-25 05:44:40
B
97
S
22
G
178
Posts: 4,118
Reputation: 104,043

Post » Thu Jul 26, 2012 6:12 pm

[QUOTE=rexrainbow]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).[/QUOTE]

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.

[QUOTE=rexrainbow]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...[/QUOTE]

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!Wastrel2012-07-26 18:13:39
Don't see the fnords and they won't eat you!
B
75
S
16
G
12
Posts: 322
Reputation: 11,608

Post » Sat Jul 28, 2012 1:29 am

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.rexrainbow2012-07-28 03:53:09
B
97
S
22
G
178
Posts: 4,118
Reputation: 104,043

Post » Sun Jul 29, 2012 12:22 pm

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.
Scirra Founder
B
359
S
214
G
72
Posts: 22,952
Reputation: 178,580

Post » Mon Jul 30, 2012 12:15 am

@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.
B
97
S
22
G
178
Posts: 4,118
Reputation: 104,043

Post » Mon Jul 30, 2012 5:41 pm

@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!
Don't see the fnords and they won't eat you!
B
75
S
16
G
12
Posts: 322
Reputation: 11,608

Post » Fri Nov 09, 2012 4:39 am

Hey @Wastrel ,

I found a way to get return value from expression.
1. create a fake ret instance like
[code]fake_ret = {value:0,
                           set_any: function(value){this.value=value;},
                           set_int: function(value){this.value=value;},                          
                          };[/code]

2. use it with "apply" (or "call") like
[code]cr.plugins_.Function.prototype.exps.ReturnValue.apply(plugin_obj, [fake_ret])[/code]

3. get return value like
[code]fake_ret.value[/code]
B
97
S
22
G
178
Posts: 4,118
Reputation: 104,043

Post » Fri Nov 09, 2012 10:51 am

Nice discovery, @rexrainbow! Thanks for the update.
Don't see the fnords and they won't eat you!
B
75
S
16
G
12
Posts: 322
Reputation: 11,608


Return to Javascript SDK

Who is online

Users browsing this forum: No registered users and 0 guests