Game Development Community

Emulate mouse event for the object

by Rpahut · in Torque Game Builder · 07/07/2010 (6:10 am) · 9 replies

Hello

I'm trying to call object's onMouseLeave() function from the object behavior script. It is the object who actually leaves, but I want to inform all object behaviors that the object is not under cursor anymore. What makes me think it is possible, is the fact that
objName.onMouseLeave()
code actually works as expected from the console. However, when the same function called for the same object from behavior script, it fails with "unknown command" error. That is really confusing.
Any suggestions would be appreciated.

#1
07/07/2010 (9:33 am)
That should work. Did you include the semicolon when called from the behavior?

objName.onMouseLeave();
#2
07/07/2010 (10:02 am)
Yes, I did.
Below is the actual code with commentary
// in the behaviors onMouseDown() callback
/* I assume that behavior owner is the object I need */
%ownr = %this.owner;
/* proper object name is printed as expected */
echo(%ownr.getName());
/* and then it loses the tracks for some reason */
%ownr.onMouseLeave(); // unknown command onMouseLeave
Should I cast %ownr somehow to the t2dSceneObject type, or can I?
#3
07/07/2010 (12:24 pm)
I'm not very fluent in behaviors, but that code should work. I manually call events like this all of the time and never had to recast anything.

Do you have mouse events enabled on the owner?
Can you test another (non-mouse) function of the owner to see if that works?
Can you post the onMouseLeave function block of the owner?
#4
07/07/2010 (1:47 pm)
It sounds like you could also call a function from the onMouseLeave() callback as well as directly from the object.

In your behavior:
%this.owner.doSomething();

In your callback:
function objectClassOrName::onMouseLeave(%this...)
{
 %this.doSomething();
{
#5
07/07/2010 (2:07 pm)
I don't think it's behavior specific problem. It's more like how TGB works with diferent objects, which is not clear to me.

Yes, mouse events are enabled, other mouse-related functions are working.

Other object functions are accessible, except for any "on*" callbacks. None of them implemented though.

I probably should make it more clear - owner object itself doesn't have onMouseLeave() function. Instead, behaviors are attached to it implementing mouse functionality. I'm expecting this object to pass mouse events to every behavior implementing appropriate callback. At least it does so when function called from console.

Maybe I really need another object. Looks like object-by-name from console and the object obtained from .owner field are actually different objects.
#6
07/07/2010 (2:21 pm)
The error is generated because you haven't coded the event. There's nothing there, just as it says. In order to code the event you'll need to make a class for that object (at least that's the way I'm familiar with):

function ownerClass::onMouseLeave(%this, %modifier, %worldPos)
{
   //pass it off to the behavior from here?
}
#7
07/07/2010 (2:47 pm)
Thanks, but unfortunately separate class-script is not an option. I can actually get all behaviors assigned to the object from this particular behavior, problem then is how to determine which of them has a required callback function.
Also, a perfect, simple solution exists - it still working from the console. I will try to find my way to it.
#8
05/15/2011 (10:45 am)
I've done this recently. Is the owner's "mouseLeave" really in the owner object? Or one of his behaviors? If it's in a behavior you may have to send the mouse leave event to the behavior instead. This is the only way I've tried it so I'm not sure.

Something like:

%behavior = %this.owner.getBehavior("MyBehavior");
%behavior.onMouseLeave(%modifier, %worldPos);
#9
05/16/2011 (3:15 am)
I can't remember now what I was trying to achieve back then. Maybe it's a good thing I couldn't call behavior from another behavior through the owner, I would end up with lots of nasty recursive calls eventually.

Anyway, now I have nice sceneWindow extension class to relay the mouse events to my objects. Still looking first through object's behaviors for callback before calling it, but I think I could get rid of that part now too.