Finding and using components
CompilesTo 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 |
On an entity
Section titled “On an entity”let transform: Component = $support.entity_get_component($entity.get(), "transform.vscript")In an entity’s children
Section titled “In an entity’s children”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)Anywhere in a hierarchy
Section titled “Anywhere in a hierarchy”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)}The one in the world
Section titled “The one in the world”Managers exist once. Ask for the singleton:
let world: Component = $component.get_singleton("catch_world.vscript", null)All of them
Section titled “All of them”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).
Keep what you found
Section titled “Keep what you found”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)}Is it the script I think it is?
Section titled “Is it the script I think it is?”fn is_effect(c: Component) -> bool { return $component.is_of_type(c, false, "effect.vscript")}false also accepts scripts that inherit from effect.
Tell it to do something
Section titled “Tell it to do something”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.
Read or change one of its fields
Section titled “Read or change one of its fields”let transform: Component = $support.entity_get_component($entity.get(), "transform.vscript")let old: vec3 = transform.positiontransform.position = old + vec3(0, 0, 10)Only let mut fields can be reached this way. See
Talking to other scripts.
Switch my own events on and off
Section titled “Switch my own events on and off”$component.set_event_enabled("tick", false, $component.get())