# Adding Data to sprites setData for array

**URL:** <https://phaser.discourse.group/t/adding-data-to-sprites-setdata-for-array/5563>\
**Category:** Phaser 3\
**Created:** [March 18, 2020, 5:53pm UTC](https://phaser.discourse.group/t/adding-data-to-sprites-setdata-for-array/5563 "2020-03-18T17:53:34Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Paul\_Hastings](https://avatars.discourse-cdn.com/v4/letter/p/dbc845/32.png) [@Paul\_Hastings](https://phaser.discourse.group/u/Paul_Hastings)\
**Post date:** [March 18, 2020, 5:53pm UTC](https://phaser.discourse.group/t/adding-data-to-sprites-setdata-for-array/5563/1 "2020-03-18T17:53:34Z")

</div>

I’m trying to modify some phaser 2 code to work with phaser 3 and am having an issue setting some data.

I have a sprite ‘tile’ that I set data for:

tile.setData({ row: x / \_this.tileWidth, column: y / \_this.tileHeight, words: {}, letter: letter, startWord: false});

This works fine but later in the code the Phaser 2 version adds a word and some properties to the words array in data(‘entry’ is an object):  
tile.data.words[entry.word] = { orientation: entry.orientation, length: entry.word.length };

I’ve tried:  
tile.setData.words[entry.word] = { orientation: entry.orientation, length: entry.word.length };

but get an error. Any insight into how I should do this?

---

<div class="post-metadata">

**Author:** ![samme](https://yyz2.discourse-cdn.com/free1/user_avatar/phaser.discourse.group/samme/32/5275_2.png) [@samme](https://phaser.discourse.group/u/samme)\
**Post date:** [March 18, 2020, 6:40pm UTC](https://phaser.discourse.group/t/adding-data-to-sprites-setdata-for-array/5563/2 "2020-03-18T18:40:34Z")

</div>

```javascript
tile.data.merge({
  row: x / _this.tileWidth,
  column: y / _this.tileHeight,
  words: {},
  letter: letter,
  startWord: false
});
// OR
tile.setData('row', x / _this.tileWidth);
// etc.

tile.getData('words')[entry.word] = {
  orientation: entry.orientation,
  length: entry.word.length
};

```

---

<div class="post-metadata">

**Author:** ![Paul\_Hastings](https://avatars.discourse-cdn.com/v4/letter/p/dbc845/32.png) [@Paul\_Hastings](https://phaser.discourse.group/u/Paul_Hastings)\
**Post date:** [March 18, 2020, 6:47pm UTC](https://phaser.discourse.group/t/adding-data-to-sprites-setdata-for-array/5563/3 "2020-03-18T18:47:38Z")

</div>

@samme

Your code is just getting the data from the words array though.

I’m not having a problem setting the data for row, column, etc. The problem I’m having adding data to the words array like the phaser 2 code is doing:

tile.data.words[entry.word] = { orientation: entry.orientation, length: entry.word.length };

this code is adding entry.word, orientation: entry.orientation, length: entry.word.length to the words array stored in data.

---

<div class="post-metadata">

**Author:** ![Paul\_Hastings](https://avatars.discourse-cdn.com/v4/letter/p/dbc845/32.png) [@Paul\_Hastings](https://phaser.discourse.group/u/Paul_Hastings)\
**Post date:** [March 18, 2020, 7:13pm UTC](https://phaser.discourse.group/t/adding-data-to-sprites-setdata-for-array/5563/4 "2020-03-18T19:13:25Z")

</div>

if it helps this is what the object looks like then it’s logged out:

tile.data.words[entry.word] = { orientation: entry.orientation, length: entry.word.length };

—\>  
console.log(tile.data.words);  
gameboy: Object { orientation: “diagonal”, length: 7 }

---

<div class="post-metadata">

**Author:** ![samme](https://yyz2.discourse-cdn.com/free1/user_avatar/phaser.discourse.group/samme/32/5275_2.png) [@samme](https://phaser.discourse.group/u/samme)\
**Post date:** [March 18, 2020, 9:42pm UTC](https://phaser.discourse.group/t/adding-data-to-sprites-setdata-for-array/5563/5 "2020-03-18T21:42:52Z")

</div>

In Phaser 3 [sprite.data](https://photonstorm.github.io/phaser3-docs/Phaser.GameObjects.Sprite.html#data) is a [DataManager](https://photonstorm.github.io/phaser3-docs/Phaser.Data.DataManager.html), not a plain object. You mustn’t set anything on `tile.data`, which is the manager. Use only `tile.getData()`, `tile.setData()`, or `tile.data.values`.

There are two ways to add properties to the `words` data value:

```
tile.getData('words')['name'] = {/* … */};

tile.data.values.words['name'] = {/* … */};
```

---

<div class="post-metadata">

**Author:** ![Paul\_Hastings](https://avatars.discourse-cdn.com/v4/letter/p/dbc845/32.png) [@Paul\_Hastings](https://phaser.discourse.group/u/Paul_Hastings)\
**Post date:** [March 18, 2020, 9:48pm UTC](https://phaser.discourse.group/t/adding-data-to-sprites-setdata-for-array/5563/6 "2020-03-18T21:48:54Z")

</div>

@samme The ‘tile.data’ code is the code from the Phaser 2 version I’m trying to duplicate. I tried:

tile.setData(‘words’)[entry.word] = {  
orientation: entry.orientation,  
length: entry.word.length  
};

but then:

tile.getData(‘words’);

is undefined.

---

<div class="post-metadata">

**Author:** ![Paul\_Hastings](https://avatars.discourse-cdn.com/v4/letter/p/dbc845/32.png) [@Paul\_Hastings](https://phaser.discourse.group/u/Paul_Hastings)\
**Post date:** [March 18, 2020, 9:50pm UTC](https://phaser.discourse.group/t/adding-data-to-sprites-setdata-for-array/5563/7 "2020-03-18T21:50:55Z")

</div>

@samme I did just try:

tile.data.values.words[entry.word] = { orientation: entry.orientation, length: entry.word.length};

and i think it’s working.
