• Godot Help
  • adding a dynamic list of effects to an item

I am creating a solution where player could pick up items that give different bonuses.

for example

  • item 1 gives +1 max HP

  • item 2 gives +3 max HP

  • item 3 gives -10 max HP

  • ...

    Each item in game is represented by a resource, that could be simplified as:

public partial class Mod : Resource
{
    [Export]
    public string Id { get; set; } = "default name"; // name

    [Export]
    //public Resource[] Effects { get; set; } = new Resource[0];
    public Effect[] Effects { get; set; } = new Effect[0];
}

The effect class is represented also by a resource that contains a modification key (which stat of the user it affects) and a value (by how much it affects the stat):

public partial class Effect : Resource
{
    [Export]
    public EffectTypeEnum Type { get; set; }

    [Export]
    public int Value { get; set; } = 1;
}

public enum EffectTypeEnum
{
    StatHP,
    StatArmor,
    StatRangedDamage,
    StatCritChance,
}

what I would like to achieve is to be able to add/stack effects onto the item in the editor, however whenever I add a new item into array - it has no properties of the Effect:

see that I cannot set any of the properties of the Effect resource after adding new elements..

the solution that actually worked is to use this public Resource[] Effects { get; set; } = new Resource[0]; line instead, then create a new Resource(s) of type Effect and dragging the resource to an array:



in the example above I create 3 effect resources, each modifies the HP stat to some extent.

Now, this is the problem I would like to solve! Given that I have hundreds of items in game (lets be optimistic) and each item modifies HP stat to a different value - I don't want to create 100 resources that modify HP stat to a different value! Ideally I would like to just add an effect to an array as in attempt nr.1 and assign it the magnitude by which the value of a stat must be affected. Is there a way to accomplish it? Or is this a limitation of a Godot?

Ideas or solutions are appreciated 🙂

In my example I am using C# and a latest godot 4.0x.

  • xyz replied to this.

    dearme Why do you need to duplicate Effect resources? In the editor you can just assign an existing *.tres file. To do the same from code use load() to load an existing *.tres file and obtain the refence to the Resource object. Then push that reference into the array.

      xyz

      the reason why I need to duplicate effect resources is to be able to assign each item a different value of the effect.

      i.e. for item "armor" I assign a hp modification +3

      for item "helmet" I also assign hp modification effect +1

      see that I cannot use the same resource of the effect for each item, because the property values are shared. In order to have unique values I have to duplicate effects!

      now assigning a different value of HP stat to each item is possible:

      I will quote myself from original post:

      Given that I have hundreds of items in game (lets be optimistic) and each item modifies HP stat to a different value - I don't want to create 100 resources that modify HP stat to a different value!

      I don't want to do this in "code" because that is the whole purpose of why I create resources. I don't want to hardcode any values into code. I would like to be able to modify these properties in the visual editor by dragging stuff over and modifying properties.

      • xyz replied to this.

        dearme How about this. Make a resource class called ItemEffects containing only a dictionary. Make a copy of this resource per item and put all effects for that item as name/value pairs into the dictionary. You can have several templates of this resource with predefined typical effect types already in the dictionary.