This directory contains auto-generated game scripts managed by the External Script System.
⚠️ Note: Scripts here are excluded from TypeScript compilation and may show type errors in your IDE. This is intentional - see "TypeScript & IDE Setup" below.
Scripts in this directory are TypeScript files that define entity behaviors. They can be linked to entities via the Script component and are hot-reloaded during development.
External scripts are standalone .ts files in this directory that are loaded dynamically via the Script API. They use pattern-based execution and are ideal for rapid prototyping and editor workflows.
File Naming: Use kebab-case or dot-notation: player-controller.ts or game.player-controller.ts
Example:
function onStart() {
console.log('Entity started');
if (three.mesh) {
three.material.setColor('#00ff00');
}
}
function onUpdate(deltaTime) {
entity.transform.rotate(0, deltaTime * 0.5, 0);
}Legacy system using registerScript for compile-time registration.
Example:
import { registerScript } from '@core';
registerScript({
id: 'game.player-controller',
onInit: (entityId) => { /* ... */ },
onUpdate: (entityId, dt) => { /* ... */ },
onDestroy: (entityId) => { /* ... */ },
});entity.transform.setPosition(x, y, z)- Set entity positionentity.transform.setRotation(x, y, z)- Set entity rotation (radians)entity.transform.translate(x, y, z)- Move entity relativelyentity.transform.rotate(x, y, z)- Rotate entity relativelyentity.transform.position- Get position as[x, y, z]entity.transform.rotation- Get rotation as[x, y, z]
three.material.setColor(color)- Set material color (e.g.,"#ff0000")three.mesh- Access to the Three.js mesh objectthree.scene- Access to the Three.js scene
time.time- Current time in secondstime.deltaTime- Frame time in secondstime.frameCount- Total frames rendered
console.log(message)- Log messages (visible in console)
Development endpoints available at http://localhost:5173/api/script/:
POST /api/script/save- Save or update a scriptGET /api/script/load?id=<id>- Load a script by IDGET /api/script/list- List all available scriptsPOST /api/script/rename- Rename a scriptPOST /api/script/delete- Delete a scriptPOST /api/script/validate- Validate script codeGET /api/script/diff?id=<id>&hash=<hash>- Check for changes
Scripts in this directory are excluded from the TypeScript build (tsconfig.json line 30):
"exclude": ["src/game/scripts/*.ts", ...]This is by design because:
- Scripts are executed at runtime by the
ScriptExecutor, not compiled - Global APIs (
entity,three, etc.) are injected at runtime - Similar to Unity's C# scripts - they're not part of the editor codebase
The script-api.d.ts file provides TypeScript definitions for:
- ✅ Autocomplete in your IDE
- ✅ IntelliSense for API methods
- ✅ Type hints for function parameters
If your IDE shows errors on entity, three, etc., you can:
- Ignore them - The scripts work fine at runtime
- Add a workspace setting (VSCode):
{ "typescript.tsserver.watchOptions": { "excludeDirectories": ["src/game/scripts"] } }
- Keep scripts small and focused - Each script should handle one behavior
- Use parameters - Configure behavior via script parameters in the editor
- Handle errors gracefully - Scripts that throw errors will be disabled automatically
- Use deltaTime - Multiply movement/rotation by
deltaTimefor frame-rate independence - Test in Play Mode - Scripts only execute when play mode is active
Scripts use a safe pattern-based executor:
- No
eval()orFunction()constructor - Limited to predefined API surface
- Sandboxed execution context
- File size limited to 256KB per script