# Add or update function?

**URL:** <https://forum.elixirforum.com/t/add-or-update-function/61465>\
**Category:** Questions\
**Tags:** ecsx\
**Created:** [February 5, 2024, 5:56pm UTC](https://forum.elixirforum.com/t/add-or-update-function/61465 "2024-02-05T17:56:50Z")\
**Posts on this page:** 1\
**Showing post:** 5

<div class="post-metadata">

**Author:** ![APB9785](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/apb9785/32/24101_2.png) [@APB9785](https://forum.elixirforum.com/u/APB9785)\
**Post date:** [February 5, 2024, 7:35pm UTC](https://forum.elixirforum.com/t/add-or-update-function/61465/5 "2024-02-05T19:35:25Z")

</div>

As @smathy pointed out, :ets inserts generally allow overwrites, and this was originally how ECSx worked as well, with just a single `add` operation that covered both cases. The reason we separated into two functions, is because of component persistence. Take the following case:

```elixir
HitPoints.add(player, 100, persist: true)
HitPoints.add(player, 95)

```

The first operation inserts a persistent HitPoints component with value `100`  
The second operation overwrites this with a non-persistent (default is false) HitPoints component with a value of `95`.

What was the problem? If a user provides options to the component creation, they would have to remember those options and pass them to every update, forever. Forgetting to include the original options would overwrite with the defaults. We considered setting the options globally, but quickly found use-cases where a single component type (such as HitPoints) would be persisted for some entities, but non-persisted for others. And we want to keep the door open for future options to be added.

So the end result is that we need one function for the original creation of a component, where the options are set, and then another function for updating the component, where no options = keep the existing options.

---

_[View the full topic](https://forum.elixirforum.com/t/add-or-update-function/61465)._
