Skip to content

Latest commit

 

History

History
139 lines (97 loc) · 3.6 KB

File metadata and controls

139 lines (97 loc) · 3.6 KB
source

Represents an event sent locally or over a network within the code block scripting system. These events only supports predefined serializable types and are primarily used to interact with scripting events from a world.

export declare class CodeBlockEvent<T extends BuiltInVariableType[]> 

Example 1

This example demonstrates how to create a custom code block event and send it to code blocks.

import { Component, *CodeBlockEvent*, Entity, PropTypes } from 'horizon/core';

class CodeBlockEvent_CB extends Component<typeof CodeBlockEvent_CB> {

  static propsDefinition= {
    target: {type: PropTypes.Entity},
  };

  sendEvent = new CodeBlockEvent<[player_name: String, player_id: Number]>('sendEvent', [PropTypes.String, PropTypes.Number]);
  receiveEvent = new CodeBlockEvent<[score: Number]>('receiveEvent', [PropTypes.Number]);

  start() {
    // Register for CodeBlock events.
    this.connectCodeBlockEvent(
    this.entity,
    this.receiveEvent,
    (score: Number) => {
       console.log(score);
     });

    // Delay by 500 milliseconds to ensure listeners are ready.
    this.async.setTimeout(() => {
      this.sendCodeBlockEvent(
        this.props.target!,
        this.sendEvent,
        "Player One",
        123
      );
     }, 500);
    }
  }
Component.register(CodeBlockEvent_CB);

Example 2

This example demonstrates how to receive a built-in CodeBlock event using the Component.connectCodeBlockEvent() function.

// Import CodeBlockEvents to access Built-in Events.
import { Component, CodeBlockEvents, Player } from 'horizon/core';

class BuiltInEventExample extends Component {
  start() {
    this.connectCodeBlockEvent(
     this.entity,
     CodeBlockEvents.OnIndexTriggerDown,
     (player: Player) => {
       // Perform an action when the Index Trigger is pressed.
     }
   );
     this.connectCodeBlockEvent (
       this.entity,
       CodeBlockEvents.OnGrabEnd,
       (player: Player) => {
       // Perform another action when the Grab Action ends.
     }
   );
 }
}

Component.register(BuiltInEventExample);

A code block event is a legacy event that doesn't perform as well as a local event or a network event. You should only use the CodeBlockEvent class to interact with world scripting events.
You can create, send, and receive custom code block events, or subscribe to built-in code block events defined in the CodeBlockEvents variable.
For information about using code block events, see the Code Block Events guide.

Creates a CodeBlockEvent object.

Signature

constructor(name: string, expectedTypes: ConstrainedPropTypes<T> | []);

Parameters

name: string

The name of the event.

expectedTypes: ConstrainedPropTypes<T> | []

The list of possible event types.

Remarks

Each of these types defines the parameters for the event and must be of type PropTypes.

A list of possible types of the event.

Signature

expectedTypes: ConstrainedPropTypes<T> | [];

The name of the event.

Signature

name: string;