• Help
  • Not possible to addItem by id after drop

Hello,

I am implementing the possibility of dropping items from inventory and I have one problem.
I have a callback to create items with random properties like this:
items/database-random/weapons/swords/BigSword.ts

import { v4 } from 'uuid';
import Sword from './../../../item/Sword';
import { RpgPlayer } from '@rpgjs/server';

export default function BigSword() {
    @Sword({
        id: v4(),
        name: 'Sword',
        graphic: 'sword',
        price: 2000,
        atk: Math.round(Math.random() * 5) + 3,
    })
    class BigSword {
    }

    return BigSword;
}

It creates item with random uuid and then I give this item to the player
Next I drop this item to the ground like this:

        @EventData({
            id: v4(),
            name: 'dropped-item',
        })
        class DroppedItem extends RpgEvent {
            onInit() {
                this.setGraphic(dropContext.item.item.graphic);
            }
            onAction(player: RpgPlayer) {
                TakeItem.toInventory(player, {
                    item: dropContext.item.item.id,
                    nb: dropContext.item.nb
                });
                dropContext.map.removeEvent(this.id);
            }
        }

        dropContext.map.createDynamicEvent({
            x: dropContext.position.x,
            y: dropContext.position.y,
            event: DroppedItem,
        });

Then I go to this item and take it using action (space) button to pick it.

Result

added item id a952401d-dcf7-44d4-88bb-9956319c210c
drop item from main 0
  Error: The ID=a952401d-dcf7-44d4-88bb-9956319c210c data is not found in the database. Add the data in the property "database" of @RpgServer decorator.

  - Player.ts:576 Proxy.databaseById
    [v4]/[@rpgjs]/server/src/Player/Player.ts:576:26

  - ItemManager.ts:97 Proxy.addItem
    [v4]/[@rpgjs]/server/src/Player/ItemManager.ts:97:51

  - TakeItem.ts:13 Function.toInventory
    /workspace/rpg/v4/items/service/TakeItem.ts:13:9

  - DropItem.ts:23 Proxy.onAction
    /workspace/rpg/v4/items/domain/items/DropItem.ts:23:22

  - Player.ts:948 Proxy.execMethod
    [v4]/[@rpgjs]/server/src/Player/Player.ts:948:37

  - AbstractObject.ts:318 Proxy.triggerCollisionWith
    [v4]/[@rpgjs]/common/src/AbstractObject.ts:318:45

  - Game.ts:119 RpgCommonGame.processInput
    [v4]/[@rpgjs]/common/src/Game.ts:119:30

  - server.ts:271 RpgServerEngine.updatePlayersMove
    [v4]/[@rpgjs]/server/src/server.ts:271:44

  - server.ts:307 RpgServerEngine.step
    [v4]/[@rpgjs]/server/src/server.ts:307:14

  - server.ts:223 Object.next
    [v4]/[@rpgjs]/server/src/server.ts:223:18

I added drop-item-on-ground branch on my repository if you want to reproduce it. (C - attack, I -inventory)

I could misunderstand how database decorators work and it is impossible to implement this.

Check video: https://imgur.com/a/SzmNhBv

  • In fact, if you use an identifier (string) to add (or any other action), the object must be in the server's in-memory database

    If it's done dynamically (which is the case here), you need to use addInDatabase() on the server.

    This can be accessed with the RpgPlayer instance:

    player.server.addInDatabase(dropContext.item.item.id, BigSword, 'weapon')

    Example here:
    https://docs.rpgjs.dev/guide/create-database.html#solution-2-dynamic-data

In fact, if you use an identifier (string) to add (or any other action), the object must be in the server's in-memory database

If it's done dynamically (which is the case here), you need to use addInDatabase() on the server.

This can be accessed with the RpgPlayer instance:

player.server.addInDatabase(dropContext.item.item.id, BigSword, 'weapon')

Example here:
https://docs.rpgjs.dev/guide/create-database.html#solution-2-dynamic-data

You can be faster:

const id = v4()
player.server.addInDatabase(id, {
        name: 'Sword',
        graphic: 'sword',
        price: 2000,
        atk: Math.round(Math.random() * 5) + 3,
}, 'weapon')
player.addItem(id)
player.equip(id)