What is the best advice on importing a customisable class to use its methods into a script ?
I would like for instance to have Controller.ts in one prefab and that this class can access methods from a generic Movements.ts in different instances.
// as ES5 import ?
import { ES5Movements } from "./Velocity/Movements";
const { ccclass, property } = _decorator;
@ccclass("Controller")
export class Controller extends Component {
movements: Movements;
// As property ?
@property({ type: Script })
ScriptMovements: Script;
onLoad() {
// or as component ?
this.movements = this.getComponent(Movements);
}
// ... later
onKeyDown(event: EventKeyboard) {
this.movements.moveLeft();
}
The thing is that I would like to have a different Movements class for every prefab instance, so importing will cause duplication of Controller.js. As property seems to not work and getComponent is flaky
I might be wrong with what you want but maybe check this post out to see if it helps with your understanding. Universal access to variables - #3 by iDevGames
Thank you for your answer, this is what I finally did
import { ForceMovement } from "./ForceMovement";
import { VelocityMovements } from "./VelocityMovements";
import { ImpulseMovements } from "./ImpulseMovements";
import { PositionMovement } from "./PositionMovement";
@ccclass("Controller")
export class Controller extends Component {
movements: ForceMovement;
onLoad() {
this.movements =
this.getComponent(ForceMovement) ||
this.getComponent(VelocityMovements) ||
this.getComponent(PositionMovement) ||
this.getComponent(ImpulseMovements);
}
onKeyDown(event: EventKeyboard) {
switch (event.keyCode) {
case KeyCode.ARROW_LEFT:
this.movements.moveLeft();
break;
case KeyCode.ARROW_RIGHT:
this.movements.moveRight();
break;
case KeyCode.ARROW_UP:
this.movements.moveUp();
break;
case KeyCode.ARROW_DOWN:
this.movements.moveDown();
break;
so that I can refer to a generic type of movement like

or

so that this.movements.moveLeft() has a different implementation depending on what file is chosen.
I use this in some prefabs to test different behaviours of move interaction