Game Development Community

Torquescript syntax question

by Eric Jergensen · in Torque Game Engine · 06/21/2004 (8:22 pm) · 9 replies

In starter.fps, server/scripts/game.cs: function startGame(), there is
new ScriptObject(AIManager) {};
   MissionCleanup.add(AIManager);
   AIManager.think();
Obviously, this code doesn't assign the reference returned by new to any variable. Further, it uses the class name as a reference to the created instance. What does this syntax mean? What would happen if there were two instances for AIManager?

#1
06/21/2004 (9:06 pm)
The class is actually ScriptObject while AIManager is the object. If there were two instances of AIManager, I believe that the newer instance would be referenced in the scripts by the variable name "AIManager". I don't think the old one would get overwritten/deleted by the creation of the second one.
#2
06/21/2004 (9:25 pm)
No, you can only have one object with a given name.
#3
06/22/2004 (5:20 am)
'new' would fail if you tried to make another, I believe.
#4
06/22/2004 (5:36 am)
What do you want to do, have 2 ai managers? you could make a AIManager2, ive tried that, you still get the same class ai spawns,

this brings up another question, how can we use this aimanager to spawn different ai with different models?

I worked with this a lot alreaddy and i just cant get it.
#5
06/22/2004 (6:40 am)
After pounding my head against the wall for several days trying to understand what AIManager was I discovered that it was really very simple : AIManager is nothing, nada, zero, null.

It's just an object that has no properties but has a method called think that is scheduled for execution every n seconds.

This method is where you are supposed to insert the code for all your bots. AIManager is not a reserved word and you may call it whatever you want.

But I obtained better results by having a think method for my AIplayer class and scheduling each of them directly.

HTH
#6
06/22/2004 (8:01 am)
I'm not really concerned about AIManager in particular, but rather the grammar of torque script.

So,
new ScriptObject() { blah... }
creates an "anonymous" instance of ScriptObject. And
new ScriptObject(MyScriptObject) { blah... }
creates a named instance of ScriptObject?
#7
06/22/2004 (8:32 am)
@Eric,
yes it's so, at least for what I know, but you can do also
MyScriptObject = new ScriptObject() { blah ...}
#8
06/22/2004 (8:35 am)
Actually there are several different ways to do this.

1) You could do it this way, but there would be no way for you to access this object.
new ScriptObject() {};


2) This way creates a ScriptObject and assigns it to local-scope variable. You could then proceed to add this to the MissionCleanup group, but there would be no way for you to gain access to the ScriptObject after you left the function it resides in unless you searched for it in the MissoinCleanup group.
%myAIManager = new ScriptObject() { };


3) This way creates a ScriptObject and assigns it to a global-scope variable. You could then access the ScriptObject throughout all of the scripts via this variable. If you add it to the MissionCleanup group you should be careful because the ScriptObject will be deleted on mission cycle and your global will point to nothing.
$myAIManager = new ScriptObject() { };


4) This way is like assigning a 'namespace'. The 'namespace' has global-scope.
// 
new ScriptObject(MyAIManager) { };

// namespace example:
MyAIManager::someFunction(%this, %someVar) { echo(%someVar); }
MyAIManager::someFunction("Hello there");


5) Same as above but we also assign it to a local-scope variable.
//
%myAIManager = new ScriptObject(MyAIManager) { };

// namespace example:
MyAIManager::someFunction(%this, %someVar) { echo(%someVar); }
%myAIManager.someFunction("Hello there");


6) Same as above but this time we assign it to a global-scope variable.
//
$myAIManager = new ScriptObject(MyAIManager) { };

// namespace example:
MyAIManager::someFunction(%this, %someVar) { echo(%someVar); }
$myAIManager.someFunction("Hello there");

Hope that helps a little bit.
#9
06/22/2004 (9:07 am)
@Robert: Just a little :)

Thanks a lot, great post!