dot big bang

Developer API
Menu
All
  • Public
  • Public/Protected
  • All

Many games require the player to interact with objects by pressing a key or button when they are near the object. While there isn't a built in API to do this, the default player Avatar does come with some scripts that handle these interactions already!

Contents

  1. Bindings
  2. Interacting With Objects
  3. Picking Up And Using Objects

Bindings

The bindings associated with these interactions vary per platform / input device:

  • Keyboard / Mouse
    • Interact: Left Mouse, E
    • Use Held Item: Left Mouse, E
    • Drop Held Item: Z
  • Controller
    • Interact: A Button
    • Use Held Item: A Button
    • Drop Held Item: Not Supported
  • Mobile
    • Interact: Button 2
    • Use Held Item: Button 2
    • Drop Held Item: Not Supported

Interacting With Objects

In dot big bang interactable objects will show an arrow icon when you are close enough to the object to interact with it. To get this arrow to appear and for the following example to work you will need to add the interactable Entity Tags to the Entity.

Once that tag is added the onInteractionInitiated() method in all scripts on that Entity will be called if the player presses the interact binding while close enough to it.

Example 1 - A simple interaction

This example shows the event that will be called when a successful interaction takes place. You can run any other logic you want in this method, but be aware that it will only run on the client that prompted the interaction. If you want the result of the interaction to appear for all clients you should check out Networking State and potentially Spawning Objects.

onInteractionInitiated(entity: Entity) {
    console.log(`${entity.name} interacted with me!`);
}

Example 2 - Toggling interactivity on and off

In some cases you may want to restrict when players can interact with an object. This can be done fairly simply by removing the interactable tag.

disabledSeconds = 3;

private _disabledSecondsRemaining = 0;

start() {
    // Make sure the Entity is interactable.
    this.entity.tags.add("interactable");
}

tick() {
    // If this is disabled check to see if it can be enabled again.
    if (this._disabledSecondsRemaining > 0) {
        this._disabledSecondsRemaining -= this.game.frameDeltaTime;

        if (this._disabledSecondsRemaining <= 0) {
            // Enable interaction.
            this.entity.tags.add("interactable");
        }
    }
}

onInteractionInitiated(entity: Entity) {
    // Disable interaction.
    this.entity.tags.remove("interactable");

    // Set the timer.
    this._disabledSecondsRemaining = this.disabledSeconds;
}

Picking Up And Using Objects

The player Avatar also comes with a simple inventory system that allows you to pickup, use and (with keyboard/mouse) drop items. Since dropping items with controller and mobile input isn't supported it is recommended to have some other way for players to get rid of held items. The "Pickup Trash" template toy in the bottom bar will do this for you!

To make an object into something that the player can pickup you need to do a few things.

  1. At the moment the dbb_link_object_to_slot script is admin only, so you need to use an existing template to base your pickup off. I recommend dragging a copy of the "Flower Gun" template toy from the bottom bar into your game.
  2. Select the Flower Gun, expand the "entity" section in the Entity Panel and click "Unlink from Template". Press "OK" on the prompt that appears.
  3. Remove the dbb_flowergun script.
  4. Change any properties you need to in the dbb_link_to_slot script.
  5. Modify the object as you want!

At this point you can optionally make your own template of your object, but that isn't necessary for picking it up. Running the game now should let you run into the object and pick it up! Walking up to a "Pickup Trash" template and interacting with it will drop the object if the object has the dbb_trash script on it. Or if you are using keyboard / mouse, you can press Z to drop it.

While the player is holding an object they can press the interact key to interact with that held object, or if they are near another interactable object, they can press the interact key to use the held object with the other object. The examples below show how to use these interactions.

Example 3 - Using the held object

This example shows the event that will be called when a player interacts with a picked up object. You can run any other logic you want in this method. The object attaching to the player will be networked since the inventory scripts manage all of that, but just like interactions any other effects will only run on the client that prompted the pickup. If you want any effects to appear for all clients you should check out Networking State and potentially Spawning Objects.

Note: This script should be on the picked up object.

onHeldInteractionInitiated(interactor: Entity) {
    console.log(`${interactor.name} interacted with me while holding me!`);

    // Tell the player that this interaction was successful so it doesn't try any further interactions.
    interactor.sendEvent("onLocalInteractionSuccess");
}

Example 4 - Animating the player when using a held object

You can pass some additional data back to the player to make them play an animation when they use their held item.

// The animation to play while using the object.
useAnimation = new SkeletalAnimationRef();

onHeldInteractionInitiated(interactor: Entity) {
    // Create some animation settings.
    const animationSettings = {
        // A name for the animation. This should be unique to the animation you are playing.
        name: "myAnimation",
        // The animation to play.
        animRef: this.useAnimation,
        // The playback rate of the animation.
        timeScale: 1
    };

    // Create some interaction settings.
    const interactionSettings = {
        // If true, the player cannot move while the animation plays.
        pauseMovement: true
    };

    // Tell the player that this interaction was successful so it doesn't try any further interactions.
    interactor.sendEvent("onLocalInteractionSuccess", animationSettings, interactionSettings);
}

Example 5 - Using the held object on a different object

An example of this kind of interaction is putting your held object in the trash. The following method will be called on the target object when a player interacts with it while holding an object.

Just like the above example you can send additional data through the "onLocalInteractionSuccess" event to animate the player.

Note: This script should be on the object the player is interacting with.

onHeldInteractionInitiatedWith(interactor: Entity, item: Entity) {
    console.log(`${interactor.name} interacted with me while holding ${item.name}!`);

    // Tell the player that this interaction was successful so it doesn't try any further interactions.
    interactor.sendEvent("onLocalInteractionSuccess");
}