Search Unity

Scripting behaviours of items and abilities

Discussion in 'Scripting' started by TheBlueDragon, Sep 13, 2019.

  1. TheBlueDragon

    TheBlueDragon

    Joined:
    May 15, 2014
    Posts:
    2
    Hello!
    I'm using ScriptableObjects to create items and abilities as easily changeable assets. They contain fields like name, id, description, cost, icon, type etc. I would like to additionally assign behaviors based on needs. For instance: healing item should heal for X on use, healing ability should heal for X based on formula referring to player statistics.

    And here is where my problem begins. I would like to assign one or more of such "custom" behaviors (probably as scripts) to item and ability assets, optimally in inspector.

    Some ideas/thoughts:
    - ScriptableObject could handle this logic. I could add such scripts to items/spells assets and let them handle the logic. Sadly, these ScriptableObject would require assets that must be customized. When I need healing behavior for multiple potions, it would require creating a new separate asset for each item, each with a different value. This may be chaotic, if one item would combine multiple behaviors.
    Another and better option I see is to create one ScriptableObject uniquely for each item/ability (with everything that item needs), which could be loaded and activated via CreateInstance(string) with the name of wanted script - which would not require creating of additional assets.
    - I could give up items/abilities as ScriptableObjects and switch to classes using interfaces/inheritance. It would easily allow each item/spell to run specific logic on use. However, coding of every spell/item as a separate class sounds like pain while I can benefit from flexibility. Interfaces and inheritance have own downsides in flexible/big project.
    - Create a custom class for scripts, that could be added and edited directly in assets of abilities/items. However, I'm not sure if it is possible and I'm still learning editor/inspector scripting. It would be great to be able to add custom behavior scripts (in some form) to ScriptableObjects assets of items/abilities via Unity inspector. Additionally, it would be great if scripts would be unique for each item/ability they are attached to (unlike ScriptableObjects that are asset-based) and their values could be edited inside of item/ability asset inspector. It sounds like a pain to program.
    - I would like to follow principle of component-based design. Behaviors of items/abilities could be set with combinations of elements like: custom targeting script complemented with animation/on-hit script.

    Any suggestions for most optimal solutions for this issue? Thanks in advance!
     
    Last edited: Sep 14, 2019
  2. Dorscherl

    Dorscherl

    Joined:
    May 29, 2017
    Posts:
    26
    Looking for something similar, not sure how to link abilities to items while letting some abilities be independent and vice versa. An ability where it summons a sword to use for a duration for example or a armor that grants a double jump or shield projection