Game Development Community

OnMouse callbacks and new class based on standard t2d

by Matt Veenstra · in Torque Game Builder · 02/24/2006 (6:57 am) · 7 replies

Hello,

This should be easy but I am missing the knowledge in torque script to make this happen.

I create a new object based on t2dStaticSprite. I can make a call back for the onMousUp work and get what it is I want going. By adding something like this to my script.

function t2dStaticSprite::onMouseDown(%this, %mod, %worldPos, %mouseClicks)
{
// do my things
}

My question is how do I add a unique onMouseDown for each object I create.

Even if I can create a new class based on t2dStaticSprite this would be fine. How does own do this with torque script?

Thanks,
Matt

#1
02/24/2006 (7:04 am)
I dont quite understand what you're asking.

Your function will exist for every t2dStaticSprite you create.

function className::functionName(%this, %vars...)
{
}
#2
02/24/2006 (7:07 am)
You just need to use a switch statement or something similar to find out what %this is and do stuff accordingly.
#3
02/24/2006 (7:24 am)
So then only way to do this is via switch? OK, I can work with that.

I just had some vision of creating an object in a unique namespace. Then calling back onMouse functions for that unique namespace only. This seemed to be a nice objected oriented approach than using a list and switch.

Thanks for the feedback? Is what I am wanting not possible?

Matt
#4
02/24/2006 (7:33 am)
You can do what you want, only it's a touch more involved. Little theory on namespaces:

(this is theory, and there are of course exception cases!)

If an object is derived from SimObject in the c++ inheritence tree (most t2d objects are), then you will have access to the namespace based on the true c++ class of the object, as Chris describes above.

If an object is dervived from GuiObject (standard guis), then you will also have an additional namespace available to you based on the object's name, if you name the gui with a unique identifier (you can't have multiple gui's named the same, so don't overload it this way).

If you create your object as a scriptObject, then you get namespaces for: class, object name, object's .class variable, and finally the object's .superclass variable.

There is some dissatisfaction in the dev team as to how the switch statement is the "current demonstrated usage" of namespaces, and they are working on a better solution, but for now either the switch statement, or creating your objects as script objects and then assigning namespaces based on that will work for you.

FYI, the main problem in coming up with a solid solution is all the varied ways to stomp on namespaces if you provide too many.

For those not worried about delving into source code, and looking to make your own changes now namespace access is handled in the c++ implementation of ::onAdd callbacks for objects from SimObject on down...you can certainly take a look at how SimObject::onAdd, GuiObject::onAdd, and ScriptObject::onAdd implement them, and do your own that works for your project.
#5
02/24/2006 (7:45 am)
There is a third implementation strategy by the way (Note: while this is what we expect folks to do in TGE, we don't want TGB devs to need to do things like this in TGB, so it's not the best solution, and we're working on better!):

In c++ create a new class, for example myT2DStaticSprite, that inherits directly from t2dStaticSprite, but re-implement the myT2DStaticSprite::onAdd to create a new namespace for you based on whatever criteria you want--maybe an internal "additionalNameSpace" field you may define in the constructor, or something along those lines.

You would then use this class to instantiate your objects in script, and you would have the t2dStaticSprite capabilities, as well as a new namespace.
#6
02/24/2006 (8:08 am)
OK...I think I understand a bit now.

If I want to create my own class (namespace?) based on t2dStaticSprite. I must do this in C++?

I cannot do something like the below in torque script...
--------------------------------------
new ScriptObject(mm2dStaticSprite : t2dStaticSprite){
class = t2dStaticSprite;
};
$mmSprite = new mm2dStaticSprite(mm2dStaticSprite : t2dStaticSprite)
{
scenegraph = t2dScene;
};

// And then I would call back like

function mm2dStaticSprite::onMouseUp(...)
{
// Do my stuff
}

--------------------------------------

Correct? Thanks for helping me understand.

Thanks for the help.
Matt
#7
02/24/2006 (8:22 am)
Matt Langley explains this better than I can in his TDN article on Strategy Games, specifically This section on Scripted Actions.

He demonstrates the use of Script Objects for NameSpace creation, and how to implement various custom functions very well. I suggest you take a look at the whole tutorial, but specifically this section!

Keep in mind that I pointed you to a sub-section of the tutorial, and it builds on previous sections, so you might be better served spending the time to read through the tutorial from the beginning.