Game Development Community

Best way to 'delete' an object ?

by Steve Vall · in Torque Game Builder · 07/06/2006 (12:35 am) · 20 replies

I'm having issues when I try to 'delete' objects I created in TorqueScript (no C++ here).

Let say I create a custome object like this:

%myClassName = "MyClass"
%this.MyObject = new ScriptObject()
{
	class = %myClassName;
};

How should I delete this object?

%this.MyObject.delete();

or ...

ScriptObject::delete( %this.MyObject );

or ... any better way?

#1
07/06/2006 (12:38 am)
Just to specify: when I use the first delete example, it seems like it doesn't get deleted, and maybe that's because it's looking for a delete() implementation of my MyClass namespace. And when I try the second one, my engine seems to become unstable and start doing weird stuff (like not letting me create a new ScriptObject() later on).

Thanks for any help !
#2
07/06/2006 (6:05 pm)
First, when TorqueScript (TS) looks for the delete method, it will look along this path:

%this.MyObject->MyClass->ScriptObject->SimObject

So the method will be found eventually because SimObject does define a delete method.


Second, calling ScriptObject::delete( %myScriptObj ) always leads to the Assertion: "Obj Passed to delete is not a SimObject" when running the debug version of TGB. Under the release version, an access violation occures (maybe that could be what is making the engine unstable?)


When you say it "seems" like it does not get deleted, what is giving you the impression it is not being deleted? In a simple test in the console: %obj = new ScriptObject(); %obj.delete(); %obj.dump(); gets me the error: "Unable to find object: '????' attempting to call function dump".


Lastly, since you are using %this in your examples, I assume the ScriptObject creation/deletion is taking place in a method. Are you sure that %this.MyObject actually exist?? Just a thought ...

HTH
#3
07/06/2006 (6:14 pm)
Thanks for that precious help. So at least I know that TS goes down the "inheritance" until it finds a delete() implementation. Also, you made me realize that I'm running on release all this time, so I'm going to build a debug version.

As for the "not being deleted", I didn't try to dump() it (I'll try it first thing when I get home). But when I look into the tree(); inspector, I can still see objects that was deleted befofe (I made sure by adding an echo just before the delete() call).
#4
07/06/2006 (10:09 pm)
I normally use %whatever.safeDelete(). That's what I suggest using as I've never run into any problems with it.
#5
07/06/2006 (10:51 pm)
Another safe deletion method is to check for it's existance first using...


if(isObject(%this.MyObject)) { %this.MyObject.delete(); }
#6
07/06/2006 (11:01 pm)
The very safest way is to use a schedule to delete the object.

Generic object deletion:
%this.schedule(0, "delete");
#7
07/06/2006 (11:46 pm)
The correct syntax for an object schedule would be...

%this.MyObject.schedule(0, delete);


NOTE: The quotes are optional.
#8
07/06/2006 (11:52 pm)
Ok I'm now running with a debug engine, should be easier to find my problems. First thing after I did my changes, is now I get this error message in the console :

Sourcefile.cs (100): Unknown command delete.
   Object (2990)  MyClass  ->  ScriptObject  ->  SimObject

and when I double check in my tree() inspector, all the objects remains in memory. It look so simple, yet it just doesn't work on my side.
#9
07/06/2006 (11:59 pm)
I even tried Gonzo's schedule trick, and I get the same error message, except for "schedule" being an unknown command ... I'm not sure what to do now, when such basic functionnalities doesn't work :-(
#10
07/07/2006 (12:08 am)
David Guy says:

Quote:
In a simple test in the console: %obj = new ScriptObject(); %obj.delete(); %obj.dump(); gets me the error: "Unable to find object: '????' attempting to call function dump".

The difference with me is that I declare a class to my ScriptObject. So if I try inside the console :

==> echo( new ScriptObject() );
3263
==>3263.dump();
 ... (dumping info here) ...

So it's working great. But problems happen when I define a class.

==> echo( new ScriptObject() {class="MyClass";} );
3264
==>3264.dump();
<input> (0):  Unknown command dump.
   Object (3264)  MyClass  ->  ScriptObject  ->  SimObject

So here you go. There got to be something I misunderstand with the classes, am I ?
#11
07/07/2006 (12:15 am)
Do me a favor and try this line...

echo( new ScriptObject() {class="MyClass";}; );

Then dump it
#12
07/07/2006 (12:22 am)
Argh, my bad. Thanks Gonzo. I think I was in trance when I wrote this... :-) Going to correct my statement above now... :-)
#13
07/07/2006 (12:29 am)
I usually create and delete a script object like this:

%myCoolObject = new ScriptObject(MyScriptObjectName)
{
    test = 1;
    hello = "world";
};
and then later:
MyScriptObjectName.delete();
or
MyScriptObjectName.schedule(0, delete);

You could even leave out %myCoolObject = and use only the object name MyScriptObjectName later on for accessing it.
#14
07/07/2006 (1:04 am)
Gonzo: I tried your line but I get an error caused by the extra dot-comma ";" you added to the line. If I remove it it's working. But here's the thing that's weird: if I use a class name that is nowhere used in my scripts, it's all right and it dump fine! But as soon as I use a class name with few methods declared for it, then it refuse to dump with an error message.

For example I have a CMenu class for which I declared a bunch of methods in a CS file, like so :

function CMenu::AddItem(%this, %itemName, %callback)
function CMenu::Show(%this)
function CMenu::FadeAway( %this, %speed )
function CMenu::GetSelectedFullWidth( %this, %index )
function CMenu::onMenuEntranceDone( %this )
function CMenu::Update( %this )

(I omitted the method's code to not clutter this thread).

So apparently if I create a ScriptObject with a class name of "CMenu", then it's a problem, and the delete doesn't work, the dump doesn't work, etc.

So long story short, I cannot delete() an object created like so :

new ScriptObject()
{
      class="CMenu";
}

but I can delete an object created like so :

new ScriptObject()
{
       class="UnusedClassName";
}

(which is useless anyway, to have a class name without any attached methods)
#15
07/07/2006 (1:24 am)
Then something is wrong somewhere. I've always defined new objects with a Semi-Colon at the end of their declarations and I've never had a problem with them. I wasn't aware you could properly form a new object without ending it using a Semi-Colon


Examples:

new ScriptObject("SomeName"){};
 
 
or
 
 
new ScriptObject("SomeName")
{
    foo = "myFoo";
};


For example, in my SuperStarterPack code I create an AI manager for managing the AI using this block of code...


if($Module::Status::AI $= "Loaded")
	{
		//  Create a new AiManager and get it added to mission
		//  cleanup in case we have Ai that need to be managed.
		new ScriptObject(AIManager)
		{
			GTC = "Wise";
		};
		MissionCleanup.add(AIManager);

		AIManager.think();
	}

So I can only conclude that there is possibly another error somewhere in your code that is throwing everything out of whack or that T2D has been altered to create "new" objects without the use of a Semi-Colon to end their declaration. However I doubt that is the case because I took this section of code straight out of T2D...

function createFileRefList()
{
   %ret = new ScriptObject()
   {
      class = "FileRefList";
   };
   
   return %ret;
}


EDIT: Notice that Martin also ended his new object declaration with a Semi-Colon as well.
#16
07/07/2006 (1:32 am)
Oups I'm terribly sorry: my last emails has a typo, actually creating a new object DO requiere a semi-colon, except in the previous example when it's embedded inside an echo() call because it's not standing on it's own. But yeah, if I create an object in normal situation, then I do need the semi-colon.

But then it still doesn't fix my problem that using a class name for which I have methods causes issues :-(
#17
07/07/2006 (1:59 am)
Just out of curiosity I tried your exact methods and everything worked perfectly, here is my log output...


User==>echo( new ScriptObject() {class="MyClass";} );

4383

User==>4383.dump();

Member Fields:
class = "MyClass"
Tagged Fields:
Methods:
delete() -
dump() -
getAllFields() -
getClassName() -
getGroup() -
getId() -
getName() -
getType() -
save() -
schedule() -
setName() -

User==>4383.delete();


And the object deleted perfectly. Just to be sure I also created my own CMenu methods and repeated the same process and got the same results...

User==>echo( new ScriptObject() {class="CMenu";} );

4383

User==>4383.dump();

Member Fields:
class = "CMenu"
Tagged Fields:
Methods:
delete() -
dump() -
foo() -
getAllFields() -
getClassName() -
getGroup() -
getId() -
getName() -
getType() -
save() -
schedule() -
setName() -

User==>4383.delete();


And the objects performed as they should have. Once again I'm left with the feeling that there is an error in your code elsewhere that is causing the rest to foul up when it gets to the point where you are having difficulty. If you would like, you can email me the file you are working on to the email in my profile and I would be happy to check it for errors to see if any exist. Beyond that, I'm at a loss to explain the behavior you are experiencing.
#18
07/07/2006 (2:06 am)
A big thank you for taking the time to test all of that, I really appreciate it (and also to all the others who took the time to reply). It looks like it work on your side, so it's possible that something else, like you say, might cause avok.

I'm also starting to suspect something : I found on this website a "patch" to add XInput support to the engine, replacing few .cpp files. It has been made for Beta 2 of T2D, I wonder if overwritting a bunch of .C files on top of the latest TGB might cause some issues ...

I think for now, I'll start with a parallele empty project, and progressively re-add all my module and code, testing them as I add them back, to see if I can trace where is the code that mess with everything else ...
#19
07/07/2006 (12:30 pm)
Sorry about my previous post. I didn't know you were using a ScriptObject, which doesn't have a safeDelete method. Sorry if that confused you. I think the safeDelete method lives on t2dSceneObject. Anyhow, good luck!
#20
07/10/2006 (4:18 pm)
Steve could you e-mail me the entire set of script you're working with ?

mattl AT garagegames DOT com