This one is pretty straightforward. soldier2ai extends soldier2. I have a ParentClass and ChildClass test that works beautifully, but for some reason it won’t work on this AI script.
There’s a limitation on parsing where it only considers the file that the script type is in. As the parent class is in another file, it won’t find it on parsing.
From what I read in another post is that if you extend a class, you need to add both scripts to an object in order for it to work, like this:
Seems alright. But when you think about it, there isn’t just 1 attribute of the head anymore. There’s actually 2: one for the soldier2, and one for the soldier2ai, which is left undefined.
So the solution is to do this:
class Soldier2ai extends Soldier2 {
initialize() {
var soldier2 = this.entity.script.soldier2;
this.head = soldier2.head;
console.log(this.head); // prints the head entity.
}
}
Right, there is no direct access to the script attributes of the parent, unfortunately.
What I tend to do is avoiding attributes on parents (and children in general). Instead I have a shared entity with a ScriptType script. For example, “Settings” that holds some attributes with game settings. I then add it to the base class, and all children have access to it:
// Settings
class Settings extends pc.ScriptType {}
pc.registerScript(Settings, 'settings');
Settings.attributes.add(points, { type: 'number' });
// Parent
class Parent extends pc.ScriptType {
initialize() {
this.settings = this.app.root.findByName('Settings').script.settings;
}
}
pc.registerScript(Parent, 'parent');
// Child
class Child extends Parent {
initialize() {
// this is fine and uses attribute exposed in Editor
const points = this.settings.points;
}
}
pc.registerScript(Child, 'child');
You will still need to copy the attributes from the parent to the child, but at least this gives you an option for having classes in their own script file.