Object Utils
ObjectUtil provides helpers for comparing, splitting, cloning, and resolving objects so that engine systems or the game itself can share data without mutations.
fastEquals
Performs a shallow comparison between two values, returning true for identical references or when both contain the same keys and values.
static fastEquals(target_1:any, target_2:any):boolean
const textField = new TextField('Congratulations!', {
type: ComponentType.TEXT,
autoTranslate: false,
style: {
fill: '#ff0000',
fontSize: 64
}
});
const nextStyle = {
fill: '#ffcc00',
fontSize: 64
};
if (!ObjectUtil.fastEquals(textField.style, nextStyle)) {
console.log('Text style changed!');
textField.style = nextStyle;
}
extractProps
Splits an object into a tuple of two plain objects: the first contains keys that start with any provided prefix while the second contains the remaining entries. The original object is left untouched.
static extractProps(target:any, prefixes:string[]):[ any, any ]
const config = {
gameTitle: 'Space Adventure',
gameVersion: '1.0.0',
uiScale: 1.5,
uiTheme: 'dark',
playerName: 'Astronaut',
playerLevel: 10
};
const [ extracted, remaining ] = ObjectUtil.extractProps(
config, [ 'ui', 'game' ]
);
console.log(extracted);
// Output: {
// gameTitle: "Space Adventure",
// gameVersion: "1.0.0",
// uiScale: 1.5,
// uiTheme: "dark"
// }
console.log(remaining);
// Output: {
// playerName: "Astronaut",
// playerLevel: 10
// }
deepClone
Creates a deep copy of the provided value, recursing through arrays and plain objects.
static deepClone(target:any):any
const originalObject = {
name: 'Player',
stats: {
health: 100,
mana: 50
},
inventory: [
'Sword',
'Shield',
'Potion'
]
};
const clonedObject = ObjectUtil.deepClone(originalObject);
clonedObject.stats.health = 80;
clonedObject.inventory.push('Map');
console.log(clonedObject.stats.health, clonedObject.inventory);
// Output: 80 [ "Sword", "Shield", "Potion", "Map" ]
console.log(originalObject.stats.health, originalObject.inventory);
// Output: 100 [ "Sword", "Shield", "Potion" ]
resolvePath
Traverses a slash-delimited path and returns the value found at the final segment. Any missing segment short-circuits the lookup and returns null.
static resolvePath(target:any, path:string):any
const gameConfig = {
graphics: {
resolution: {
width: 1920,
height: 1080
},
quality: 'high'
},
audio: {
volume: {
master: 0.8,
music: 0.6,
sfx: 1.0
}
}
};
const resolutionWidth = ObjectUtil.resolvePath(gameConfig, 'graphics/resolution/width');
console.log(resolutionWidth);
// Output: 1920
const musicVolume = ObjectUtil.resolvePath(gameConfig, 'audio/volume/music');
console.log(musicVolume);
// Output: 0.6
const nonExistent = ObjectUtil.resolvePath(gameConfig, 'input/controller/deadzone');
console.log(nonExistent);
// Output: null