Users and players
CompilesTwo words that sound alike mean different things:
- A useruser: A person playing, or a bot. Players, the balls they hold and the things they spawn are owned by a user. (
User) is who is playing: a person or a bot, with a name. - A player is the character in the world: an entity with a
catch_playercomponent, built from the player prefab, owned by a user.
From a user to their player
Section titled “From a user to their player”let me: User = $user.get_primary()let body: Entity = $catch.get_player_entity(me)$user.get_primary() is the main user playing on this machine. $catch.get_player_entity gives the root entity
of a user’s player.
From a player to their user
Section titled “From a player to their user”Anything in the world can have an owner. A player’s entities are owned by its user, and so are things they spawn:
fn owner_name(thing: Entity) -> string { let user: User = $entity.get_owning_user_from_entity(thing) return $user.get_name(user, false)}Who touched my trigger?
Section titled “Who touched my trigger?”A trigger reports the component that overlapped it, and that’s usually a collider on some child of the player, not
the player’s root. Search the hierarchy for catch_player:
- playercatch_playerplayer_movementtransform
- body
- collider…this is what overlapped
event export fn trigger_overlapped(other, unused) { if !other { return } let touched: Entity = $component.get_entity(other) let player: Component = $entity.get_hierarchy_singleton("catch_player.vscript", touched) if !player { return // not a player: a ball, perhaps } let user: User = $entity.get_owning_user_from_entity($component.get_entity(player)) $debug.print_line("{} stepped on me", [$user.get_name(user, false)], false)}Everyone
Section titled “Everyone”let everyone: list = $user.get_all_users()for i in 0..everyone.len() { let user: User = everyone.get(i) $debug.print_line("{}", [$user.get_name(user, false)], false)}$user.is_local(user) tells you whether a user plays on this machine; use it to show something only to the
player it concerns.
For programmers
User is a value handle; compare two with $user.compare_users (0 when equal). $entity.get_owning_user() is
the calling component’s owner. Bots are users too.