CUSTOM INVENTORY

Integrating Export Functions for Item Handling

The sanba-realisticitemdrop resource provides two exports that streamline item transactions in your custom inventory system. These exports ensure that item drops and item give/throw operations are validated against your configuration and handled uniformly via client events.

1. sanbarealisticdrop

Purpose

This export is used for processing item drops. It checks if an item exists in your configuration and then triggers a client event to handle the drop.

Usage Example

if exports['sanba-realisticitemdrop']:sanbarealisticdrop(source, toData) then
    return
end

How It Works

  • Input Parameters:

    • source: The player's identifier.

    • toData: A table containing item details, which must include:

      • name: The item’s name.

      • count: The number of items.

      • metadata: Additional item data (if any).

  • Processing:

    • The function converts the item name to lowercase to standardize lookups.

    • It then checks if this normalized name exists in config.itemprops.

    • If the item is found:

      • It triggers the client event "sanbarealisticdrop" with the appropriate parameters.

      • Returns true, allowing your script to exit early.

    • If the item isn’t found, it simply returns false.

2. Givethrow

Purpose

This export handles giving or throwing items from the inventory. It validates that you have enough items and then triggers a client event to manage the transaction.

Usage Example

How It Works

  • Input Parameters:

    • source: The player's identifier.

    • data: A table representing the item from your inventory (e.g., fromInventory.items[slot]). This table should include:

      • name: The item's name.

      • count: The current quantity available.

      • metadata: Additional item data.

      • label: The display name (used for error messages).

    • itemCount (or count in the snippet): The number of items to be given or thrown.

  • Processing:

    • The function first checks if the inventory has enough of the item by comparing data.count with itemCount. If not, it returns an error table (e.g., { "cannot_give", count, data.label }).

    • It then converts the item name to lowercase and looks it up in config.itemprops.

    • If the item is found:

      • It triggers the client event "sanbarealisticgive" with the necessary details.

      • Returns true, so your script can stop further processing.

    • If the item is not found, it returns false.

How to Integrate These Exports

  1. Data Preparation: Ensure your inventory data tables (toData for drops and the item data from fromInventory.items[slot] for giving/throwing) include all required fields: name, count, metadata, and optionally label for user-friendly error messages.

  2. Passing the Player Identifier: The source parameter should correctly represent the player’s unique identifier to ensure the correct client receives the event.

  3. Handling Export Return Values: Use the export functions in an if statement. If the export returns true, it means the action was successfully processed (item found and client event triggered), so you can return immediately to prevent further handling.

  4. Resource Dependency: Make sure that the sanba-realisticitemdrop resource is started and properly configured in your server’s resource list, as these exports depend on it.

By integrating these functions, your custom inventory system will efficiently handle realistic item drops and give/throw events, centralizing logic and reducing redundant code.

Last updated