Game Development Community

Destructor for script classes

by anycast · in Torque Game Builder · 01/09/2009 (3:08 am) · 6 replies

Hi,

I'm creating some objects of a script class like this

%object=new ScriptObject(MyClass);

I simulate a constructor call (albeit a manual constructor call) like this:

%object.Initialize();

Is there any way to call a specific function in my class automatically when I do a safeDelete of %object, or do I have to do it manually like I did for the constructor?

Thanks in advance,

Alex

#1
01/09/2009 (4:42 am)
Try onRemove().
#2
01/09/2009 (7:29 am)
Thanks, it works. I also found out that onAdd may function as a sort of default constructor. Do you know if there's anyway to override onAdd so that it accepts parameters (and a way to pass those parameters upon the creation of a script object).
#3
01/09/2009 (11:32 pm)
I don't think it's possible without engine changes.
#4
10/23/2009 (6:39 pm)
Better late than never :)

This would be your destructor onSafeDelete is called right after safeDelete();
#5
10/23/2009 (7:18 pm)
nice call there Max... i didnt know about that callback.
#6
10/23/2009 (10:09 pm)
As far as constructor parameters go, Torque technically has them, but they're rarely used and they're only accessible to engine objects. Instead, what I think most scripts typically do is use dynamic object fields instead. Dynamic fields are assigned before onAdd() is called, so by the time you get to onAdd(), you will have access to them.

Example:
function MyObject::onAdd(%this)
{
    echo("Parameter value 1:" SPC %this.value1);
    echo("Parameter value 2:" SPC %this.value2);
    echo("Parameter value 3:" SPC %this.value3);
}

...and then:
new ScriptObject() {
    class = MyObject;
    value1 = "A value";
    value2 = "Another value";
    value3 = "Yet another value";
};

EDIT: Gah. Thread necromancy. I should check original post dates.