# Multiple CPU help

**URL:** <https://forum.makecode.com/t/multiple-cpu-help/15896>\
**Category:** Arcade\
**Created:** [October 18, 2022, 4:43pm UTC](https://forum.makecode.com/t/multiple-cpu-help/15896 "2022-10-18T16:43:59Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![PixelDoodle](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/pixeldoodle/32/24129_2.png) [@PixelDoodle](https://forum.makecode.com/u/PixelDoodle)\
**Post date:** [October 18, 2022, 4:43pm UTC](https://forum.makecode.com/t/multiple-cpu-help/15896/1 "2022-10-18T16:43:59Z")

</div>

Name says it all, But ill clear it up anyway. I wanna Make **MULTIPLE CPU’S** that all have their own traits similar to a bot game in A Battle Royale. Like they know what to do. And say one shoots, How can I do That **WITHOUT** making A different CPU Do the same thing as well. please help.

---

<div class="post-metadata">

**Author:** ![personalnote](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/personalnote/32/31359_2.png) [@personalnote](https://forum.makecode.com/u/personalnote)\
**Post date:** [October 18, 2022, 11:09pm UTC](https://forum.makecode.com/t/multiple-cpu-help/15896/2 "2022-10-18T23:09:52Z")

</div>

CPU?? don’t you mean AI?

---

<div class="post-metadata">

**Author:** ![PixelDoodle](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/pixeldoodle/32/24129_2.png) [@PixelDoodle](https://forum.makecode.com/u/PixelDoodle)\
**Post date:** [October 18, 2022, 11:47pm UTC](https://forum.makecode.com/t/multiple-cpu-help/15896/3 "2022-10-18T23:47:54Z")

</div>

Yea. Sorry. I get confused between those two terms.

---

<div class="post-metadata">

**Author:** ![isoffline](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/isoffline/32/8791_2.png) [@isoffline](https://forum.makecode.com/u/isoffline)\
**Post date:** [October 18, 2022, 11:47pm UTC](https://forum.makecode.com/t/multiple-cpu-help/15896/4 "2022-10-18T23:47:59Z")

</div>

are those arent the same

---

<div class="post-metadata">

**Author:** ![personalnote](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/personalnote/32/31359_2.png) [@personalnote](https://forum.makecode.com/u/personalnote)\
**Post date:** [October 19, 2022, 4:50pm UTC](https://forum.makecode.com/t/multiple-cpu-help/15896/6 "2022-10-19T16:50:01Z")

</div>

nope they arent

---

<div class="post-metadata">

**Author:** ![isoffline](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/isoffline/32/8791_2.png) [@isoffline](https://forum.makecode.com/u/isoffline)\
**Post date:** [October 19, 2022, 5:48pm UTC](https://forum.makecode.com/t/multiple-cpu-help/15896/7 "2022-10-19T17:48:02Z")

</div>

oop no wonder

---

<div class="post-metadata">

**Author:** ![richard](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/richard/32/5417_2.png) [@richard](https://forum.makecode.com/u/richard)\
**Post date:** [October 19, 2022, 6:02pm UTC](https://forum.makecode.com/t/multiple-cpu-help/15896/8 "2022-10-19T18:02:43Z")

</div>

There’s a really great block for this inside of @jwunderl’s jwunderl/arcade-sprite-util extension (disclaimer: i am the one who added this block)

![Screen Shot 2022-10-19 at 10.59.34 AM](https://us1.discourse-cdn.com/flex020/uploads/makecode/original/2X/8/8a3395461c5777ebc1edd1ad522b756185f70d54.png)

This block lets you define an `on game update` for only one sprite! That means you can have that sprite make decisions inside that `on game update` without having to loop over all the sprites or make them all do the same thing.

Here’s some sample code:

> **[enemy-ai example](https://arcade.makecode.com/73909-49546-71029-53727)**
>
> Made with ❤️ in Microsoft MakeCode Arcade.

In this project, I create three enemies that each have their own simple AI. The two in the corners are programmed to follow the player when the player gets close. The third one just wanders around randomly on the screen.

---

<div class="post-metadata">

**Author:** ![richard](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/richard/32/5417_2.png) [@richard](https://forum.makecode.com/u/richard)\
**Post date:** [October 19, 2022, 6:09pm UTC](https://forum.makecode.com/t/multiple-cpu-help/15896/9 "2022-10-19T18:09:26Z")

</div>

You’ll see this if you look at the code, but the way I like to use this block is to define an “add behavior to sprite” function like this:

![Screen Shot 2022-10-19 at 11.03.41 AM](https://us1.discourse-cdn.com/flex020/uploads/makecode/original/2X/4/4e4b0242030e846290ade99bdcbcbbb78e2681ef.png)

Then, inside that function you add the on game update like so:

 ![Screen Shot 2022-10-19 at 11.04.24 AM](https://us1.discourse-cdn.com/flex020/uploads/makecode/original/2X/6/68d69d83952b851bcddfc1eac7a5511133ef1ac9.png)

If you want to have any local state for the sprite, make sure you DON’T put it inside a variable. All variables are global in blocks projects meaning they’ll be shared between all of your sprites. Instead, you’ll want to use the arcade-sprite-data extension to store it on the sprite. Here’s an example of how you might store/use a “state” variable:

 ![Screen Shot 2022-10-19 at 11.07.58 AM](https://us1.discourse-cdn.com/flex020/uploads/makecode/original/2X/6/6cc8edcc11b594098b84d9402d41d249f56acdb6.png)

You can put the code for each state inside the branch in the if statement.

Once you’ve created a function like this, you can simply call it when the sprite is created. The nice thing about putting it in a function is that you can apply it to different kinds of sprites. For example, you might make a random wander ai for some enemies in your game but that same ai could be used with NPCs to make them wander around town.

---

<div class="post-metadata">

**Author:** ![richard](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/richard/32/5417_2.png) [@richard](https://forum.makecode.com/u/richard)\
**Post date:** [October 19, 2022, 6:18pm UTC](https://forum.makecode.com/t/multiple-cpu-help/15896/10 "2022-10-19T18:18:39Z")

</div>

One more important note:

When defining a function like this, make sure you use the “sprite” argument that you drag out of the function instead of the variable your sprite is stored in (e.g. mySprite). As I mentioned before, all variables are global so that won’t work if you have multiple sprites. If weird things are happening with your ai, check the variables first!

---

<div class="post-metadata">

**Author:** ![PixelDoodle](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/pixeldoodle/32/24129_2.png) [@PixelDoodle](https://forum.makecode.com/u/PixelDoodle)\
**Post date:** [October 19, 2022, 7:15pm UTC](https://forum.makecode.com/t/multiple-cpu-help/15896/11 "2022-10-19T19:15:03Z")

</div>

WOAH! I’ve gotta Learn More About Sprite Utils. Thanks @richard

---

<div class="post-metadata">

**Author:** ![isoffline](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/isoffline/32/8791_2.png) [@isoffline](https://forum.makecode.com/u/isoffline)\
**Post date:** [October 20, 2022, 4:41pm UTC](https://forum.makecode.com/t/multiple-cpu-help/15896/12 "2022-10-20T16:41:05Z")

</div>

is there a tutorial on how to use the data extension?

---

<div class="post-metadata">

**Author:** ![PixelDoodle](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/pixeldoodle/32/24129_2.png) [@PixelDoodle](https://forum.makecode.com/u/PixelDoodle)\
**Post date:** [October 20, 2022, 6:49pm UTC](https://forum.makecode.com/t/multiple-cpu-help/15896/13 "2022-10-20T18:49:45Z")

</div>

I dont know. I’ll check
