Confused
by Very Interactive Person · in Torque Game Builder · 10/28/2006 (9:07 am) · 4 replies
After doing some basic tutorials I thought it was time to start doing some work on my game. But I'm really confused now... I'm used to OO programming, and its clear to me now that Torquescript may look like OO, but in fact isn't. So, could you more experienced Torque scriptors explain how you would code the following:
I want to create a class that handles the loading of a level pack. In real OO world ( :) ) that would be a singleton, with public methods like "loadLevelPack", and "getLevelAtIndex". It would have private variables like "levelPackArray" etc And it would also dispatch an event when a level pack is loaded, so the game could wait till the LevelPack class has loaded the file.
So this LevelPack class... in TS that'd be a scriptObject? Or? I'm very confused!
I want to create a class that handles the loading of a level pack. In real OO world ( :) ) that would be a singleton, with public methods like "loadLevelPack", and "getLevelAtIndex". It would have private variables like "levelPackArray" etc And it would also dispatch an event when a level pack is loaded, so the game could wait till the LevelPack class has loaded the file.
So this LevelPack class... in TS that'd be a scriptObject? Or? I'm very confused!
#2
10/29/2006 (1:43 am)
Thanks, I'm starting to understand now.
#3
You would call it locally like so:
10/31/2006 (3:22 pm)
You can have singleton script objects too. For example, you could have a getInstance function on your LevelPackLoader namespace:function LevelPackLoader::getInstance()
{
if( !isObject( $levelPackLoader ) )
{
$levelPackLoader = new ScriptObject()
{
class = LevelPackLoader;
};
}
return $levelPackLoader;
}You would call it locally like so:
LevelPackLoader::getInstance().doSomething();
#4
I played around with scripts the past 2 days (coded 5% of my game :) ), and I think I get it now.
11/03/2006 (8:04 am)
Thanks,I played around with scripts the past 2 days (coded 5% of my game :) ), and I think I get it now.
Torque 3D Owner Richard McKinney
Default Studio Name
In LevelPackLoader.cs:
new ScriptObject (LevelPackLoader) { OnLevelLoad = ""; }; function LevelPackLoader::RegisterOnLevelLoad (%this, %method) { // Append to a string of code to execute when levels are loaded %this.OnLevelLoad = %this.OnLevelLoad @ %method; } function LevelPackLoader::OnLevelLoaded (%this) { // If anyone's listening to this event, call them back. if (%this.OnLevelLoad !$= "") eval (%this.OnLevelLoad); } function LevelPackLoader::LoadLevelPack (%this) { // Do whatever you need to do here to load levels... %this.OnLevelLoaded(); }So let's say we have a gui that wants to know when the level changes:
function SomeGui:OnWake (%this) { LevelPackLoader.RegisterOnLevelLoad ("SomeGui.OnLevelLoaded();"); } function SomeGui::OnLoadLevelLoaded (%this) { // This method gets called when a level is loaded. }