Skip to main content
flarb
Up and Coming
April 3, 2025
Solved

Is there a way to auto-grab items?

I want to make a player automatically grab an object when they bump into it instead of having to press the hold/grab button. Is there a way to do this? I don't see any method that would allow me to place an object in an avatar's hand.

gausroth
gausrothAnswer
Honored Contributor
April 3, 2025

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

4 replies

ExoGuid
Honored Contributor
April 3, 2025

Codeblocks has a "Force Hold" block which you can activate on collision.  TS likely has an equivalent

gausroth
gausrothAnswer
Honored Contributor
April 3, 2025

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
flarb
flarbAuthor
Up and Coming
April 18, 2025

Hey thanks! Sort of related--is there a way to know if the player is holding an object? I don't want the forcehold to happen if the player is already holding something.

@flarb
gausroth
Honored Contributor
April 18, 2025

You will have to add a boolean to the held items script and set to true if held and false if not. If it is a local script you will need to create a manager script for this.

GitHub: https://github.com/Gausroth/HorizonWorldsTutorials.git Discord: https://discord.gg/hNyDQwdw4N YouTube: https://www.youtube.com/@GausRothTutorials