Skip to content

Resources

Pedro Clerici edited this page Jul 26, 2025 · 1 revision

Resources are world-unique data that exist for the entire lifetime of the world. Unlike components, they are not tied to a specific entity. Resources can elegantly handle things like time (frame number, delta, etc.), renderers, inputs, and other data that is unique to a world rather than an entity.

Defining resources

Resources are classes:

class Time {
	static fromWorld() {
		return new this();
	}
	previous: number = 0;
	current: number = 0;
	delta: number = 0;
	frameNumber: number = 0;
}

class Renderer {
	draw() {
		// ...
	}
}

All systems that use the same resource type refer to the same object.

class MyResource {}

function systemA(myResource: Res<MyResource>) {
	// myResource here...
}
function systemB(myResource: Res<MyResource>) {
	// ... is the same object as myResource here!
}

Like components, resources can be wrapped in Readonly if you'd like to narrow the type.

class MyResource {}

function someSystem(myResource: Res<Readonly<MyResource>>) {}

Resources must either implement a static fromWorld() {} method, be constructable with no arguments, or be directly inserted into the world with World.p.insertResource().

Tip

If you'd like to use the same type for multiple resources, you can simply subclass it.

Initializing Resources

If you'd like your resources to be automatically created by the world, you can implement the static fromWorld() {} method that will be called when schedules are prepared. This method will be passed the world, and so can insert additional resources or event listeners. This method can also simply return an instance of the class.

fromWorld() must return either an instance of the resource you'd like to create, or a promise that resolves to that instance.

import { World } from 'thyseus';

class Keyboard {
	static fromWorld(world: World) {
		return new this(world);
	}

	constructor(world: World) {
		function handleKeypress() {
			// ... your implementation here ...
		}
		world
			.addEventListener('start', () => {
				document.addEventListener('keydown', handleKeypress);
			})
			.addEventListener('stop', () => {
				document.removeEventListener('keydown', handleKeypress);
			});
	}
}

Local Resources

Local Resources or "System Resources" are a variation of resources that are unique per system, rather than per world. They can typically be used in place of closures over module-scope variables (although module-scope variables are fine, too!). Systems can also have multiple local resources of the same type, and each will be unique:

import { Res, Local } from 'thyseus';

import { Vec3 } from 'some-math-library';

function mySystem(a1: Res<Vec3>, a2: Res<Vec3>) {
	console.log(a1 === a2); // -> true
}
function mySystem(a1: Local<Vec3>, a2: Local<Vec3>) {
	console.log(a1 === a2); // -> false
}

Apart from their scope, system resources function identically to normal resources; the same class could be used both as a system resource and as a world resource.

Clone this wiki locally