core icon indicating copy to clipboard operation
core copied to clipboard

Add support for yielding in component constructors

Open osyrisrblx opened this issue 2 years ago • 0 comments

Constructors that yield are useful for setting up class members using functions like .WaitForChild(). The component will not setup any lifecycle events until the constructor is complete. This allows for patterns like:

@Component({ tag: "CharacterComponent" })
export class CharacterComponent extends BaseComponent<{}, Model> implements OnPhysics {
	private humanoid: Humanoid;

	constructor() {
		super();

		const humanoid = this.instance.WaitForChild("Humanoid", 5);
		assert(humanoid && humanoid.IsA("Humanoid"));
		this.humanoid = humanoid;
	}

	onPhysics() {
		// use this.humanoid
	}
}

removeComponent would need to wait for the constructor to finish before calling destroy()

osyrisrblx avatar Jul 20 '22 03:07 osyrisrblx