Global Class - TGE 1.5.2
by Arjan Gelderblom · in Torque Game Engine · 08/30/2007 (3:12 am) · 5 replies
Hi everyone,
Although I'm using RTSKit included in the TGE1.5.2 I think this post belongs more in this subforum then in that
of the RTSKit.
I'm trying to implement my own AIPathfinding, I've got it as far as the classes are created and the nodeMap
get's generated. This is server side; I've set the nodeMap class to be a NetObject so it should be
ghosted to the clients. But this class is now being declared within a function this means that as soon as the
function ends my nodeMap is gone. Now my question is how to create a global declaration of this class? So that
it is available all the time - which comes in handy when calculating paths on my nodeMap.
So in short is where to declare my class so that it is available all the time, like the terrain class? Or ofcourse if I missed something obvious please tell me :P!
Thanks in advance for your help and/or time.
Grtz,
Arjan Gelderblom
Although I'm using RTSKit included in the TGE1.5.2 I think this post belongs more in this subforum then in that
of the RTSKit.
I'm trying to implement my own AIPathfinding, I've got it as far as the classes are created and the nodeMap
get's generated. This is server side; I've set the nodeMap class to be a NetObject so it should be
ghosted to the clients. But this class is now being declared within a function this means that as soon as the
function ends my nodeMap is gone. Now my question is how to create a global declaration of this class? So that
it is available all the time - which comes in handy when calculating paths on my nodeMap.
So in short is where to declare my class so that it is available all the time, like the terrain class? Or ofcourse if I missed something obvious please tell me :P!
Thanks in advance for your help and/or time.
Grtz,
Arjan Gelderblom
About the author
#2
But if I can't get it working this might be a option...
Grtz,
Arjan Gelderblom
08/30/2007 (10:51 am)
Thanks for your reply but I'm trying to implement it C++ only... I've got a class that holds functions to uses the nodeMap that can be used in the RTSUnit class. So now if i can create the nodeMap as a globally available class so that I can calculate paths from the RTSUnit subclass.But if I can't get it working this might be a option...
Grtz,
Arjan Gelderblom
#3
--when the terrain block is created, it registers with the simulation with a name. This name is unique and consistent.
--when other code needs to access the terrain block, it first does a Sim::findObject("terrainblock");, and then dynamically casts that SimObject pointer to a TerrainBlock pointer (this is from memory, actual strings and classes may be slightly off).
--once the pointer has been cast, the code that needs to access the terrain block uses that pointer.
In effect, the class isn't global in the c++ sense, but the instantiation of the class (the terrain block) is available via lookup within the simulation.
08/30/2007 (11:27 am)
Well, if you look carefully at how the terrain is "globally available", what is actually occurring is the following:--when the terrain block is created, it registers with the simulation with a name. This name is unique and consistent.
--when other code needs to access the terrain block, it first does a Sim::findObject("terrainblock");, and then dynamically casts that SimObject pointer to a TerrainBlock pointer (this is from memory, actual strings and classes may be slightly off).
--once the pointer has been cast, the code that needs to access the terrain block uses that pointer.
In effect, the class isn't global in the c++ sense, but the instantiation of the class (the terrain block) is available via lookup within the simulation.
#4
Thanks for your help and sorry for me not being completely clear about it, but I want the instance of the nodeMap be globally available... I've seen the Sim::findObject() function but where is that instance created so that I can create my nodeMap instance on the same place and use the Sim::findObject method to get the pointer to that instance from everywhere in my script.
Again sorry for not being clear about this the first time around and thanks for your help.
Grtz,
Arjan Gelderblom
08/30/2007 (12:25 pm)
@StephenThanks for your help and sorry for me not being completely clear about it, but I want the instance of the nodeMap be globally available... I've seen the Sim::findObject() function but where is that instance created so that I can create my nodeMap instance on the same place and use the Sim::findObject method to get the pointer to that instance from everywhere in my script.
Again sorry for not being clear about this the first time around and thanks for your help.
Grtz,
Arjan Gelderblom
#5
First, we need to define "globally available"--are you saying that you want a static singleton class that has a c++ style pointer that can be referenced anywhere in c++ (that #includes the file where it's defined), or are you saying you want a single object within your simulation that can be accessed by other objects within your simulation?
If you want the first, then the approach to take is to define the class as a singleton, and use c++ standard practices with singleton classes (which I will state up front requires a bit of work around due to how the engine "stands up" classes). That's not Torque specific, and you should research the concept of c++ singleton classes, and cross file global access. In all honestly, this is not necessarily a best practice within Torque.
If you want the second, then the approach is to instantiate your class as a single instance (do a one time instantiation of the class, and then populate with data), register the object instantiated with the simulation by name, and then whenever you wish to access this class from other classes, use Sim::findObject("YourNameHere"); to search the simulation for the pointer that references your single registered instantiation.
This second approach is a standard best practice for Torque, as long as your object inherits from SimObject.
08/30/2007 (10:40 pm)
I think there is a bit of confusion here, so I'm going to try to take a half step back.First, we need to define "globally available"--are you saying that you want a static singleton class that has a c++ style pointer that can be referenced anywhere in c++ (that #includes the file where it's defined), or are you saying you want a single object within your simulation that can be accessed by other objects within your simulation?
If you want the first, then the approach to take is to define the class as a singleton, and use c++ standard practices with singleton classes (which I will state up front requires a bit of work around due to how the engine "stands up" classes). That's not Torque specific, and you should research the concept of c++ singleton classes, and cross file global access. In all honestly, this is not necessarily a best practice within Torque.
If you want the second, then the approach is to instantiate your class as a single instance (do a one time instantiation of the class, and then populate with data), register the object instantiated with the simulation by name, and then whenever you wish to access this class from other classes, use Sim::findObject("YourNameHere"); to search the simulation for the pointer that references your single registered instantiation.
This second approach is a standard best practice for Torque, as long as your object inherits from SimObject.
Torque Owner Steve D
I'm not sure if this is exactly what you are looking for but it might be worth a look.