Talking to other scripts
CompilesYour script rarely works alone. A teleport pad moves a player’s transform; a scoreboard reads a match’s score;
a switch tells a door to open. There are two ways to reach another componentcomponent: One script attached to one entity. The same script on ten entities is ten components, each with its own copy of the script's fields.: read or
write its fields, or send it an event.
Either way, you first need a handle to it, a Component. The Cookbook shows
all the ways to find one; the most common is asking an entity for the script by name:
let me: Entity = $entity.get()let transform: Component = $support.entity_get_component(me, "transform.vscript")Reading and writing fields
Section titled “Reading and writing fields”With a handle, a dot reads another component’s mutablemutable: A field declared with let mut. The script's running state: it changes while the game runs. field by name, and = writes it:
let transform: Component = $support.entity_get_component($entity.get(), "transform.vscript")let where: vec3 = transform.positiontransform.position = where + vec3(0, 0, 100)The name must be spelled exactly as the other script declares it. Reading a field it doesn’t have prints a warning
like Script … does not have a mutable named … and gives you nothing.
Sending an event
Section titled “Sending an event”When you want another component to do something, send it an event with $component.queue_event. You name one of
its event export fn functions and pass the arguments as a list:
let effect: Component = $support.entity_get_component($entity.get(), "effect.vscript")if effect { $component.queue_event("play", effect, null, false)}queue_event takes the event’s name, the component, the arguments (null for none, or a list like
[position, rotation]), and whether to wait until the end of the frame (true) or run as soon as the current event
finishes (false).
The event doesn’t run immediately: it’s queued, and runs after the code that sent it has finished. Don’t expect its effects on the next line.
// A ball's own script owns its position,// so this write is overwritten.ball_transform.position = destination// Ask the ball to move itself.$component.queue_event("set_position_and_rotation", ball, [destination, rotation], true)Prefer the event when a script offers one. The owner knows what else has to change along with the value (its own caches, the network, the animation), and a field written around it may be overwritten on its next tick.
Finding what another script offers
Section titled “Finding what another script offers”- Its events are listed on the Events page with the scripts that export them:
filter for a word like
ballorjump. - Its fields are the
let mutlines at the top of its source, which you can read in your editor.
For programmers
Field access by name is late-bound: it compiles to a lookup on the target at runtime, so a typo is a runtime
warning, not a compile error. queue_event is fire-and-forget with no return value; end_of_frame: false runs
it after the active event, true at end of frame. $component.try_queue_event doesn’t assume the event exists.
Calling another script’s non-event function directly isn’t something this guide covers; use events.