I made a few assumptions. The object being a sword and that it is being force held on player enter. The main piece of code you are looking for is force hold line but this should work.
// these are the required imports from horizon/core
import { Component, PropTypes, CodeBlockEvents, GrabbableEntity, Handedness } from 'horizon/core';
class ForceHoldObject extends Component<typeof ForceHoldObject > {
static propsDefinition = {
sword: { type: PropTypes.Entity },
};
// first we must connect to the player enter world event. This will give us a reference to the player when they enter the world.
preStart() {
this.connectCodeBlockEvent(this.entity, CodeBlockEvents.OnPlayerEnterWorld, data => this.OnPlayerEnterWorld(data));
}
// once the player enters the world we can force hold the sword.
// however, we must first ensure that the sword is set.
// we do this by checking with an if statement.
OnPlayerEnterWorld(player: Player) {
if (this.props.sword) { // if this returns true we can force hold the sword
this.props.sword.as(GrabbableEntity).forceHold(player, Handedness.Right, false)
}
else { // else we may want to know that it is not set so print a warning to the console.
console.warn("sword is missing or null!");
}
}
}
Component.register(ForceHoldObject);
GitHub: https://github.com/Gausroth/HorizonWorldsTutorials.git Discord: https://discord.gg/hNyDQwdw4N YouTube: https://www.youtube.com/@GausRothTutorials