Game Development Community

CH4 Problem

by Martin Mc · in Torque Game Engine · 03/22/2005 (1:06 pm) · 5 replies

I've been reading (and typing through) the examples in chapter 4 of the book and I have a question that I hope someone can answer for me. Here goes:

on pgs 139-140 (concerned with creating the control/main.cs) I am having a little trouble understanding the following code / explanation (emphasis mine):

from the code:

function OnStart()
//-------------------------
// Called by root main when package is loaded
//-------------------------
{
Parent::OnStart();



from the explanation:


First, the parent OnStart() function is called. This would be the version that resides in root main


I've probably misunderstood since this is my first foray into Torque Script (Java is my native language), but I understand all this to amount to the following:

1) When OnStart in root main is invoked, it invokes the OnStart function of all sub modules (including control) EDIT: This bit of knowledge is gained from pg 139, although the actual OnStart implementation doesn't appear to actually do anything
2) When the OnStart function of control is called, it calls the parent::OnStart function (i.e. the one in root main)
3) Surely now the root main OnStart would execute and in turn invoke the OnStart of control thus causing recursion?

I'm probably way off base here (it's been a long day), but if anyone could help me understand what's going on here, it would be much appreciated.

Cheers

#1
09/22/2005 (7:50 pm)
Ive just read that section and have come to a similar conclusion... Im posting this to try and revive this topic to benifit you and I, Martin.
#2
09/23/2005 (7:24 am)
I don't have the book in front of me, but I don't see how the root main OnStart function would invoke the OnStart of all sub modules. I'll have to check out p139, but there's nothing in the code that would suggest that it would call any other function.

EDIT: if anyone has the book handy, what does it say on p139 about this?
#3
09/23/2005 (9:11 am)
In control/main.cs

package control 
{
  function OnStart()
 {
    Parent::OnStart();
 }
}

- the function onStart() in control/main.cs is inside a package so it overrides the one in the root/main.cs.

- Parent::OnStart actually call the onStart() in root/main.cs

no recursion here...
#4
09/23/2005 (9:20 am)
Right, I think he was confused about what happens when the root main OnStart function is called. He was thinking that it would invoke the OnStart functions of all sub modules (his point #1), but that's not the case.
#5
10/08/2005 (10:48 am)
Was curious myself with the flow. Made a few echo points using tutorial.base and the c++ codes.

At root/main.cs
/////////////////////////////////////////////////////////////////
function onStart()
{
// Default startup function
echo("OnStart in root =>>>>>>>>>>>>>");
}


At root/tutorial.base/main.cs in ttb package
/////////////////////////////////////////////////////////////////
// Package overrides to initialize the game
package ttb {

function onStart()
{
// Initialize the client and the server
echo("ttb in tutorial.base OnStart1 >>>>>>>>>>>>>");
Parent::onStart();
echo("ttb in tutorial.base OnStart2 =>>>>>>>>>>>>>");
initServer();
initClient();
}

function onExit()
{
// Save off our current preferences for next time
echo("ttb OnExit =-----------");
echo("Exporting prefs");
export("$Pref::*", "./prefs.cs", False);
Parent::onExit();
}

}; // Client package
activatePackage(ttb);


At root/common/main.cs in common package
/////////////////////////////////////////////////////////////////
package Common {
~
~
function onStart()
{
echo("OnStart1 in Common =>>>>>>>>>>>>>");
Parent::onStart();
echo("OnStart2 in Common =>>>>>>>>>>>>>");
echo("\n--------- Initializing MOD: Common ---------");
initCommon();
}

};
activatePackage(Common);


Here listing from console.log
/////////////////////////////////////////////////////////////////

~
~
Loading compiled script tutorial.base/main.cs.
~
~
Loading compiled script common/main.cs.
~
~
ActivatePackage(common)
ActivatePackage(ttb)
~
~
~
Invoking OnStart from Root

ttb in tutorial.base OnStart1 >>>>>>>>>>>>>
OnStart1 in Common =>>>>>>>>>>>>>
OnStart in root =>>>>>>>>>>>>>
OnStart2 in Common =>>>>>>>>>>>>>

--------- Initializing MOD: Common ---------
Loading compiled script common/client/canvas.cs.
Loading compiled script common/client/audio.cs.
ttb in tutorial.base OnStart2 =>>>>>>>>>>>>>

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
The way i understand it is

1) OnStart was first declared/compiled at root/main.cs .
2) common package is activated. So this overrides 1).
3) ttb package is activated after 2) , hence this overrides 2)

Hence 1) is parent of 2)
and 2) is parent of 3)

Therefore, when Onstart is invoke,
it will start at 3)
Parent::OnStart() makes it run the codes at 2)
Parent::OnStart() at 2) will call the Onstart() at root
as shown in console.log.

-Yuzairee