Author Topic: Parent In vs. Outside A Package  (Read 629 times)

What's the difference in putting a function with a parent inside and outside of a package? It obviously still does something when it's not in a package. Some add-ons do it(default included). And in the default water bricks it doesn't parent onTrustCheckFinished, but when it does(even though it's not in a package), it's called fine.

it calls from the parent class

for instance, if you have a ScriptObject named spooky with the class of spoopy, and a superClass of spoppy, you can do somethin like this:


function ScriptObject::h(%this)
{
   echo("a");
}

function spoppy::h(%this)
{
   echo("c");

   parent::h(%this);
}

function spoopy::h(%this)
{
   echo("m");

   parent::h(%this);
}

function spooky::h(%this)
{
   echo("y");

   parent::h(%this);
}


and you call the method h directly on spooky, the console would echo:

y
m
c
a
==>spooky.h();


following the order of the class hierarchy. the same principle can apply to engine classes as well if you're aware of the hierarchy for a class. ShapeBase, for example, is commonly used to create methods that apply broadly to many rendered objects, and you could create a basic method in the ShapeBase namespace which is expanded individually in higher levels of the hierarchy using parent

it's super-useful for creating nice polymorphous object-oriented systems similar to classes found in other languages

really, i guess you could say that packages just add an additional layer onto this by shoving additional layers above the namespace that are called first. you can see this hierarchy in action whenever you use trace. parent realistically functions no differently in either scenario, it's just applied for a slightly different scenario. you can neglect to call the parent in a package to override base functionality, just as you can when creating a method of the same name in a higher level of a class's hierarchy. calling the parent is just a way to run the same code that's in a lower level.
« Last Edit: November 12, 2015, 10:45:46 PM by otto-san »