Multiple CPU help

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.

1 Like

CPU?? don’t you mean AI?

1 Like

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

are those arent the same

nope they arent

oop no wonder

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

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:

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.


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

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

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:

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.


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!

1 Like

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

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

1 Like

I dont know. I’ll check