保持对代码的理解,不要完全依赖AI Coding——由一段Babylon.js开发出现的bug引发的感慨
子曰AI Coding并不知道自己在干什么。子曰如果一段代码的bug找不到不要整晚死磕好好睡一觉很可能第二天早上就找到了。子还曰保持清醒比无脑努力重要136.25倍。子路重要程度需要精确到小数点后两位吗子曰需要使用cursor开发一个基于Babylonjs引擎的内容cursor通过opus4.8帮我写了如下代码注意的两段代码中关于attach的部分import { Observable, TransformNode, type Behavior } from babylonjs/core; export default abstract class LerpBehavior implements BehaviorTransformNode { public get name(): string { return LerpBehavior; } public static readonly behaviorName: string LerpBehavior; private _uuid: string ; public get uuid(): string { return this._uuid; } protected _setUuid(uuid: string): void { this._uuid uuid; } protected _target: TransformNode | null null; public get target(): TransformNode | null { return this._target; } public readonly onSetProcessObservable new Observablenumber(); public readonly onDetachObservable new Observablevoid(); init() { } protected _isInitialized: boolean false; protected _attached: boolean false; attach(target: TransformNode) { if (this._attached) return; this._attached true; this._target target; } detach() { this.onDetachObservable.notifyObservers(); this.onDetachObservable.clear(); this._target null; this._attached false; this._isInitialized false; } public dispose(): void { this._target?.dispose(); } public setProcess(process: number): void { if (!this._isInitialized this._target) { this._initialize(); } this._applyProcess(process); } protected abstract _applyProcess(process: number): void; protected abstract _initialize(): void; }import LerpBehavior from ./LerpBehavior; import { Vector3, TransformNode, Axis, Space, Scalar } from babylonjs/core; import { DTO_LerpPosition } from ../../../../../Shared/TScripts/DTO/DTO_RuntimeSystem; export default class LerpPosition extends LerpBehavior{ public get name():string{return LerpPosition;} public static readonly behaviorName LerpPosition; public static createFromJson(trNode: TransformNode, jsonBehav: string): LerpPosition | undefined{ try { if(trNode){ const data: DTO_LerpPosition JSON.parse(jsonBehav); if (!data) return undefined; const s data.start; const e data.end; const offsetStart new Vector3(s.x, s.y, s.z); const offsetEnd new Vector3(e.x, e.y, e.z); const behavior new LerpPosition(offsetStart, offsetEnd); behavior._setUuid(data.uuid); trNode.addBehavior(behavior, true); behavior.setProcess(data.process || 0); return behavior; } return undefined; } catch (error) { console.error(Failed to create LerpPosition:, error); return undefined; } } private _offsetStart:Vector3; private _offsetEnd:Vector3; private _posStart: Vector3 | null null; private _posEnd: Vector3 | null null; constructor(offsetStart:Vector3, offsetEnd:Vector3){ super(); this._offsetStart offsetStart; this._offsetEnd offsetEnd; } public override attach(target: TransformNode): void { if (this._attached) return; this._attached true; super.attach(target); } /** * 初始化起始和结束位置 * 使用局部空间的translate来计算相对于当前位置的偏移 */ protected override _initialize(): void { if (!this._target || this._isInitialized) return; const currentPosition this._target.position.clone(); this._target.translate(Axis.X, this._offsetStart.x, Space.LOCAL); this._target.translate(Axis.Y, this._offsetStart.y, Space.LOCAL); this._target.translate(Axis.Z, this._offsetStart.z, Space.LOCAL); this._posStart this._target.position.clone(); this._target.position currentPosition; this._target.translate(Axis.X, this._offsetEnd.x, Space.LOCAL); this._target.translate(Axis.Y, this._offsetEnd.y, Space.LOCAL); this._target.translate(Axis.Z, this._offsetEnd.z, Space.LOCAL); this._posEnd this._target.position.clone(); this._target.position currentPosition; this._isInitialized true; } protected override _applyProcess(process: number): void { if (!this._target) { return; } if (this._posStart this._posEnd) { const p Scalar.Clamp(process, 0, 1); this._target.position Vector3.Lerp(this._posStart, this._posEnd, p); this.onSetProcessObservable.notifyObservers(process); } } }注意这中写法会导致子类中的this._target target;这行代码永远得不到执行所以this._target 一直是null 让opus4.8在debug模型调试了一晚上都找不到问题回家睡觉第二天早上一眼就看到了只要把LerpPosition里面的public override attach(target: TransformNode): void { if (this._attached) return; this._attached true; super.attach(target); }写成public override attach(target: TransformNode): void { if (this._attached) return; super.attach(target); this._attached true; }就好了。当然这种 override 代码本来就没个P用我不明白cursor写他干嘛因为这几行狗P根本就是直接删除了也行子最后曰有了AI Coding也不要当甩手掌柜的。