Game Development Community

Using :: for namespace of functions

by Alex Rice · in Torque Game Builder · 08/18/2005 (5:45 am) · 10 replies

I noticed that the T2D demo applications don't have any namespace for their functions. Fine for the engine developers, but for a newbie like me I don't know if there is already a function mountCamera(%pos) for instance, and torque is not going to warn me if I re-define that function, right? So I would rather not risk it, and just have a namespace for my game prefixed to everything. Am I paranoid or what :-)

OK "package" is not really for creating namespaces... it's more for function overriding, but I want to setup a namespace for all my variables and functions in my game. Is there anything wrong with naming a function like

function TTA::dBugOn () { ... }

Will the engine think it's a console method and suffer a performance hit? Or would it be better to make a scriptobject to contain the function and make it into an actual console method? These are just global functions and don't really belong in a class/object for any real reason.

#1
08/18/2005 (8:04 am)
Theres nothing to stop you just naming your functions with a common prefix such as

MyGame::myfunction()

As far as the engine is concerned this is just a single function name of "mygame::myfunction".

The only time the engine cares about the :: prefix is when you're using namespaces with objects. For example if the prefix is the name of an object, or the name defined in the "class" property for an object.

If you want to keep all your variables together you could add them to a $Game group by prefixing with $Game::MyVar.
#2
08/18/2005 (7:10 pm)
@Gary, OK thanks for confirming this
#3
09/18/2005 (2:21 pm)
Unfortunately the schedule() function does not accept function names containing ::. Any workarounds for that while still using the :: for naming?
#4
09/18/2005 (7:34 pm)
Parent::function -> parent.schedule()
#5
09/18/2005 (10:44 pm)
Lotekk- what are you suggesting? Suppose I have a function MyGameLevel::myfunction(). MyGameLevel is not an object, so how can I call MyGame.schedule() on it?
#6
09/18/2005 (11:08 pm)
Ah. Sorry, was about 4 in the morning my time when I responded, and I don't think I had understood your question fully.
#7
09/18/2005 (11:42 pm)
@Alex

Did you enclose the function name in quotes?

schedule(100, 0, "MyGame::myFunction", %foo);

--
Hans
#8
09/19/2005 (5:14 am)
@Hans, schedule will accept the function name in quotes, but when the message is fired, the console prints
MyGame::myFunction. Unknown command

@LoTekk, I was thinking you might have the solution after all. I tried creating a scriptobject like
$gLevel = new ScriptObject(MyGame);
and calling schedule like

%this.schedule( 2000, 0, myFunction, $gLevel );
%this.schedule( 2000, 0, myFunction, %this);
etc.
but I cannot get this to work.

What is the difference between the SimObject::schedule and the global schedule() ?
#9
09/19/2005 (6:57 am)
Try:

$gLevel.schedule( 2000, "myFunction" );

That assumes that there are no parameters you want to send. When you use the objectID.schedule(...) syntax, the objectID must be the namespace that you want to call schedule within...in this case I'm assuming that your %this objectID is NOT the same as $gLevel.
#10
09/21/2005 (9:27 pm)
@Stephen- OK I got it now. Thanks for clearing that up :-)