Variable _Const Player

The _ object is an instance of PlayerScript.

Available only within the script of the Player Script component. You can set the script of the Player Script component to a player by calling $.setPlayerScript(playerHandle).

The execution environment differs from that of Scriptable Item scripts. Scriptable Item and Player Script scripts run independently.

Player Script scripts do not automatically share variables, etc. with Scriptable Item scripts. To pass values between Scriptable Item and Player Script scripts, use PlayerScript.sendTo or PlayerHandle.send.

Unlike Scriptable Item scripts, PlayerScript scripts do not reset the environment periodically. Therefore, you can save the state in a global variable and use it across callbacks.

Example

The following is an example of a Scriptable Item script that sets a Player Script. The Player Script script is set to the player who "interact" the item. If the Player Script component is not attached to this item, an error will result.

$.onInteract((player) => {
// Set PlayerScript for player
$.setPlayerScript(player);
});

The following is an example script for a Player Script component that logs on initialization. When used in conjunction with the Scriptable Item above, it will log when the player uses the item.

// The following log display is executed when PlayerScript is set
_.log("PlayerScript is initialized.");
_.log("Source Item ID is " + _.sourceItemId.id);

Example

The following is an example script of a Player Script component that sends back a message in the next frame for an ItemId received in a message.

By using the Scriptable Item component script below together with the Player Script component script, you can see how to send a message from ScriptableItem to PlayerScript and how to send a message from PlayerScript to ScriptableItem to PlayerScript.

First, write the following script in Scriptable Item.

$.onStart(() => {
// Record the history of the player who gave the PlayerScript in state.
$.state.players = [];
});

$.onInteract((player) => {
if ($.state.players.find(p => p.id === player.id)) {
// Send a message to players who have already given PlayerScript
player.send("send", "")
} else {
// If a player does not exist in the state history,
// give it a PlayerScript and record it in the history.
$.setPlayerScript(player);
$.state.players = [...$.state.players, player]
}

// Remove players that no longer exist from the history
$.state.players = $.state.players.filter(p => p.exists());
});

$.onReceive((messageType, arg, sender) => {
if (messageType === "hello") {
// Display in log when "hello" message is received
$.log("hello " + arg);
}
}, { player: true });

This script does the following.

  • When a new user "interact" this item, it gives the PlayerScript to the user.
  • Sends a message when a user who has already been given the PlayerScript "interact" the item.
  • Display the "hello" message in the log when it is received.

Next, write the following script in the Player Script component.

// Prepare a variable to store the message sender's ItemId.
let itemId = null;

// Register a callback when PlayerScript receives a message
_.onReceive((messageType, arg, sender) => {
switch (messageType) {
case "send":
if (sender instanceof ItemId) {
// Display log when a "send" message is received from an Item
_.log("Received from ItemId: " + sender.id);
// Assigns sender to the global variable itemId
itemId = sender;
}
break;
}
});

// Register a callback to be called every frame in PlayerScript
_.onFrame((deltaTime) => {
if (itemId !== null) {
// If itemId is not null, send "hello" message to itemId to make itemId null
_.sendTo(itemId, "hello", "world");
itemId = null;
}
});

This script does the following.

  • When it receives a message "send", it records the sender's ItemId in the variable itemId.
  • checks the variable itemId every frame to see if it is null, and if not, sends the message and assigns null to it

Generated using TypeDoc