Skip to content

Finding and using components

Compiles

To talk to another script you need a Component handle to it. Pick the recipe that matches where it is, then see Talking to other scripts for what you can do with it.

It’s… Recipe
On a known entity On an entity
Below an entity In an entity’s children
Somewhere in the same hierarchy Anywhere in a hierarchy
The only one in the game The one in the world
One of many, anywhere All of them
let transform: Component = $support.entity_get_component($entity.get(), "transform.vscript")

The first effect anywhere below my entity (not on my entity itself):

let effect: Component = $entity.get_first_component_in_children($entity.get(), "effect.vscript", "", "", 0, false)

The "" and 0 mean “no name filter” and “no field filter”; false searches grandchildren too.

Every one, as a list:

let effects: list = $entity.get_all_components_in_children($entity.get(), "effect.vscript", "", "", 0)

From any entity inside a player, such as a hand or a collider, find the player’s catch_player:

fn player_of(touched: Entity) -> Component {
return $entity.get_hierarchy_singleton("catch_player.vscript", touched)
}

Managers exist once. Ask for the singleton:

let world: Component = $component.get_singleton("catch_world.vscript", null)
let pads: list = $world.get_all_components("teleport_pad.vscript", "", null, "", 0)
let first: Component = $world.get_first_component("teleport_pad.vscript", "", null, "", 0)

The filters after the script name: an entity name ("" for any), an owner (null for any), and a field name and value ("", 0 for none).

let mut transform: Component = null
event export fn created() {
transform = $support.entity_get_component($entity.get(), "transform.vscript")
}
event export fn tick() {
if !transform {
return
}
let position: vec3 = $entity.get_world_position(transform)
}
fn is_effect(c: Component) -> bool {
return $component.is_of_type(c, false, "effect.vscript")
}

false also accepts scripts that inherit from effect.

let effect: Component = $support.entity_get_component($entity.get(), "effect.vscript")
if effect {
$component.queue_event("play", effect, null, false)
}

If you’re not sure it has the event, $component.try_queue_event takes the same arguments and doesn’t assume.

let transform: Component = $support.entity_get_component($entity.get(), "transform.vscript")
let old: vec3 = transform.position
transform.position = old + vec3(0, 0, 10)

Only let mut fields can be reached this way. See Talking to other scripts.

$component.set_event_enabled("tick", false, $component.get())