# Apple Game Frameworks and Technologies - Adding monster entity to scene array (779)

**URL:** <https://forum.devtalk.com/t/apple-game-frameworks-and-technologies-adding-monster-entity-to-scene-array-779/5697>\
**Category:** PragProg Customers\
**Tags:** book-apple-game-frameworks-and-technologies, errata\
**Created:** [6 January 2021 09:20 UTC](https://forum.devtalk.com/t/apple-game-frameworks-and-technologies-adding-monster-entity-to-scene-array-779/5697 "2021-01-06T09:20:51Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![cocojas](https://forum.devtalk.com/user_avatar/forum.devtalk.com/cocojas/32/1011_2.png) [@cocojas](https://forum.devtalk.com/u/cocojas)\
**Post date:** [6 January 2021 09:20 UTC](https://forum.devtalk.com/t/apple-game-frameworks-and-technologies-adding-monster-entity-to-scene-array-779/5697/1 "2021-01-06T09:20:51Z")

</div>

The code to add the monster entity to the scene’s entity array (so that the generator-spawned monsters get component updates) in chapter 13 comes on page 779 (or thereabouts, I’m not sure if my reader has re-numbered pages). This is fine, but it needs to be moved earlier in the chapter, to the point where the update method is added to the agent component for the monster. Since the agent component is not part of the component system that’s being driven by the GameScene update event, the monsters never participate in the “chase” behavior, which may confuse people who are new to the platform.

Excellent material, I’ve really enjoyed reading it and testing everything out since the original alpha. Once it’s out, I’m going to recommend to my friends who are interested in these technologies.

Cheers, Jason

---

<div class="post-metadata">

**Author:** ![Paradox927](https://forum.devtalk.com/user_avatar/forum.devtalk.com/paradox927/32/2978_2.png) [@Paradox927](https://forum.devtalk.com/u/Paradox927)\
**Post date:** [6 January 2021 15:04 UTC](https://forum.devtalk.com/t/apple-game-frameworks-and-technologies-adding-monster-entity-to-scene-array-779/5697/2 "2021-01-06T15:04:37Z")

</div>

Hey, Jason.

Thanks for reaching out.

So, I’m looking through the chapter now, and I’m not sure what you mean, so please forgive me if I’m not answering your question properly.

On page 301 (Creating the Agent Component), which I’m sure doesn’t align with your reader’s page numbers, there’s a bit of code that you need to add to the `didAddToEntity()` method. Specifically, the line that reads `scene.agentComponentSystem.addComponent(agent)` is where that component gets added, causing it to participate in the updates.

Is it possible that you missed that step? If not, maybe I’m misunderstanding what’s happening, which could be the case. Let me know. Thanks!

Also, I’m so happy to read that you’re enjoying the book. I had a lot of fun writing it. 😍

---

<div class="post-metadata">

**Author:** ![cocojas](https://forum.devtalk.com/user_avatar/forum.devtalk.com/cocojas/32/1011_2.png) [@cocojas](https://forum.devtalk.com/u/cocojas)\
**Post date:** [6 January 2021 17:23 UTC](https://forum.devtalk.com/t/apple-game-frameworks-and-technologies-adding-monster-entity-to-scene-array-779/5697/3 "2021-01-06T17:23:59Z")

</div>

Hi Tammy,

No worries, I struggled a lot with how to explain it, especially since e-readers constantly renumber pages 🙂

The actual agent simulation is working, but in the first half of the chapter (Chapter 13—Planning Routes and Creating Believable AI), if the reader is following along doing the coding, the monsters themselves won’t be participating in the update cycle since the MonsterEntity (that get created by the generators) are not yet in the scene’s entity array. The code that gets the monster to choose to chase the player (changes the goal weight for the `interceptGoal`) comes near the end of the section _Use Multiple Goals_, where the reader adds an `update` method to `MonsterEntity`.

But, the code to update the adding the monster entities into the scene’s entity array isn’t added until later in the chapter, specifically near the end of the section _Update Physics for Monsters and Projectiles_. So when the reader first tests picking up the key after adding the chase code, the monsters aren’t going to chase them, because the `MonsterEntity`'s `update` function won’t be triggered yet. I think just relocating the code block that does that to the earlier section would fix it.

```auto
if let scene = ocmponentNode.scene as? GameScene {
    scene.entities.append(monsterEntity)
}

```

---

<div class="post-metadata">

**Author:** ![Paradox927](https://forum.devtalk.com/user_avatar/forum.devtalk.com/paradox927/32/2978_2.png) [@Paradox927](https://forum.devtalk.com/u/Paradox927)\
**Post date:** [9 January 2021 16:14 UTC](https://forum.devtalk.com/t/apple-game-frameworks-and-technologies-adding-monster-entity-to-scene-array-779/5697/4 "2021-01-09T16:14:05Z")

</div>

Hello again.

Sorry for the late reply. It’s been a heck of a week! Also, I appreciate the additional information you’ve provided.

So, if I understand this issue correctly, it might be more about clarifying _why_ the code gets added where it does versus moving the code to an earlier section. As of now, I’m not sure which is the right way to go. But that’s OK…

I’ll be doing a final read-through this weekend and will make a note to review this chapter’s organization. If it makes sense to move the code, I’ll do that. If not, I’ll add something to the chapter to clarify the reason why it’s done the way it is.

Until then, I’ll keep this topic “unresolved.”

Talk soon!

---

<div class="post-metadata">

**Author:** ![Paradox927](https://forum.devtalk.com/user_avatar/forum.devtalk.com/paradox927/32/2978_2.png) [@Paradox927](https://forum.devtalk.com/u/Paradox927)\
**Post date:** [11 January 2021 17:08 UTC](https://forum.devtalk.com/t/apple-game-frameworks-and-technologies-adding-monster-entity-to-scene-array-779/5697/5 "2021-01-11T17:08:25Z")

</div>

All right, I’ve had a chance to review this chapter, and I made some changes.

In the _Add the Player Agent to the Component System_ section, I end with this:

> Build and run the project, but don’t pick up the key right away. Give the monsters some time to wander around first. After a short while, pick up the key, and watch how some of the monsters’ attention turn to Val. (Don’t worry, the monsters still can’t hurt Val, but you’re about to work on that next.)

That paragraph above indicates that the monsters will chase Val after she collects the key. Of course, the monsters won’t chase her because their entities haven’t (yet) been added to the scene’s entity array.

To fix this error, I’ve modified the end of that section, which now reads:

> Excellent, the `didMove(to:) method` is cleaner, but there’s still one more thing you need to do: get the monster entities to participate in the updates.

I also added a new section immediately after the _Add the Player Agent to the Component System_ section, which looks like this:

 ![new-section](https://forum.devtalk.com/uploads/default/original/2X/9/9fc855646b2e946345c5c275bef04ccc4b6f0333.png)

And, I updated the _Update Physics for Monsters and Projectiles_ section to reflect this change.

This flows a lot better and doesn’t give the reader the impression that they may have missed a step. Thanks for finding this one!

I don’t think another beta is coming out, but the “fix” will be in the final release. Marking this one solved. 🤩 😁

---

<div class="post-metadata">

**Author:** ![cocojas](https://forum.devtalk.com/user_avatar/forum.devtalk.com/cocojas/32/1011_2.png) [@cocojas](https://forum.devtalk.com/u/cocojas)\
**Post date:** [15 January 2021 18:37 UTC](https://forum.devtalk.com/t/apple-game-frameworks-and-technologies-adding-monster-entity-to-scene-array-779/5697/6 "2021-01-15T18:37:21Z")

</div>

That looks great! Congrats on being so close to launch 😃

---

<div class="post-metadata">

**Author:** ![Paradox927](https://forum.devtalk.com/user_avatar/forum.devtalk.com/paradox927/32/2978_2.png) [@Paradox927](https://forum.devtalk.com/u/Paradox927)\
**Post date:** [25 January 2021 14:32 UTC](https://forum.devtalk.com/t/apple-game-frameworks-and-technologies-adding-monster-entity-to-scene-array-779/5697/7 "2021-01-25T14:32:31Z")

</div>

Thank you!
