getComponent is an API function that allows you to retrieve the first
component of a specific type from an object.
There is also getComponents, which returns all components
of that type on the object.
const comp = getComponent(obj, "Type");
const comps = getComponents(obj, "Type");
"AudioSource", "PlatformMover").null if it doesnβt exist).Suppose you have an enemy with an AudioSource component:
{
"type": "enemy",
"name": "Enemy1",
"x": 100,
"y": 200,
"components": [
{
"type": "AudioSource",
"clip": "explosion.wav",
"volume": 0.8,
"loop": false
}
]
}
From a script you can access the component like this:
const audio = getComponent(obj, "AudioSource");
if (audio) {
audio.playing = true; // play sound
audio.volume = 0.5; // change volume
}
const movers = getComponents(platform, "PlatformMover");
movers.forEach(m => {
m.speed = 200;
});
null (in getComponent) or an empty array (in getComponents).getComponentFromObject("name", "Type") to access a component from an object by name.