Kodeco Forums

GameplayKit Tutorial: Entity-Component System, Agents, Goals, and Behaviors

In this GameplayKit tutorial, you will learn how to create flexible and scalable games by using the Entity-Component system with Agents, Goals and Behaviors.


This is a companion discussion topic for the original entry at https://www.raywenderlich.com/706-gameplaykit-tutorial-entity-component-system-agents-goals-and-behaviors

i hope its totally swift based game so we don’t need to use unity or any other platform to make it right ??

can u give me any reference link for it which has some video content to show it practically step by step…
thanks @naturaln0va

hey @jay005, this tutorial is written in Swift and it uses SpriteKit. We have a few video courses on the site that cover both Swift and SpriteKit. Check them out:

Hi.

In AiComponent.swift it will be universal:
nextMonster = MonsterType.allValues[Int(arc4random() % UInt32(MonsterType.allValues.count))]

Otherwise, the program gives an error

I have checked high and low for a good tutorial on this topic and none of them come close to the knowledge bomb this tutorial contains. Thank you so much for creating it and updating it to the latest syntax. Aside from the GameplayKit info, I learned a lot of other useful swift techniques (escaping button handler creation, better use of enums, and how to manage objects in general.

hi. can you explain, for example, how I would add the following things here? let’s say i had a new sprite, that was based on a ShapeNode instead of a SpriteNode. would I make a new entity?

let’s say I wanted to make a badge that some sprites will have, which could change, based on how many of them there are… would that be a new entity? or a new component? would I be adding the badge as a child node? or should there be an array of possible badges that would all be in one component?

if I wanted to make something like a teleportation ring, so entities that go in one end come out the other, would that be a new entity?

could you give the most brief overview of how each ought to be implemented?

I would create a new ShapeNode sprint as a new entity and the badges a component. Try and think of a component as a reusable piece that can fit into one or more entities.

I’m not sure how you’re wanting to represent the badge visually but adding the badge as a child should work.

Yes I’d make a teleportation ring an entity since it’s a tangible object that the game exists in the world.

thank you for that! I’m still a bit confused, because also there are the component systems to deal with. in the mean time while I was awaiting your first reply, I came across Overview of Entity Component System (ECS) variations with pseudo-code · GitHub which discusses ECS and in particular, the Bilas vs. Martin’s models. one thing I didn’t see how to do here is touch handling. for example, if i want to be able to tap on certain entities, drag others, can’t do anything to yet others… am I going to make a touch component, a drag component etc? then when it’s update time, am I going to have a main update loop that iterates only through all the components, regardless of what entities they are attached to (martin’s model)? I think this subject has a lot more going on than I first imagined, and I’m really eager to learn much more about it.

so more specifically, clarifying from above, let’s say, for example, I have monsters, like those in your game. Let’s say if a quirk is near 5 other quirks (a clump of quirks) that I want them all to turn red, or make a swarm noise… that would not just be a new component for the quirk entity? I would need a component to detect if they are in a clump. a component to change their color. a component that plays the sound? if i had 7 clumps going on at one time, and i wanted a slightly different behavior for each size clump, would I put all that logic into a component, component system, entity, entity manager? would I have the entities figure out their own clumps and add to sets? you can see how confusing it can be…

one other thing that i’m really eager to try to do is to use a protocol/extension/struct oriented approach to components and entities just because it seems to be much more flexible moving forward, but what do you think? is that a good way to go in general? and if so, is it possible to have a tutorial that melds the two concepts together? I’d love to see.

Thank You Very much…
@naturaln0va

Can someone tell me why this game (complete tutorial when run on iPad 2) gets the FPS to drop to 20 after approx 70 nodes are present.

I thought this is a rather simple game and yet it slows the system down heavily. Naturally there could be things that weren’t implemented in the tutorial, because its only a tutorial which cause this, but can someone point out what those are and how to make it playable?

I’ve run into similar problems with a much more complex and spaghetti like game I made so am surprised to see it in what would seem a crystal clear and simple code in this tutorial.

Thanks

@naturaln0va Can you please help with this when you get a chance? Thank you - much appreciated! :]

1 Like

I just ran this on a first gen iPad mini and I’m seeing around the same drop in FPS you are. As I’m looking around nothing sticks out to me that needs optimization. In this game there is a lot of computation going on in regards to calculating the location for each of the little “quirks” to move to, as well as the calculation to determine which friendly quirks to avoid. On these slower devices it makes sense for there to be slow down when multiplying the number of each of the calculations.

Honestly there’s not a lot I can think of to make the game more efficient without removing too much of the game play.

1 Like

Thanks for replying.

I was thinking same as you in regards to the reasons for the slowdown.

Now I was thinking, how about instead of iterating through every unit for avoiding friendly collisions it would somehow fetch only the colliding units. I am looking to make a similar game but based on physics engine and it would be logical to have the units delegate when they run into each other’s body, but I am wondering how I would tie that into the gameplaykit moveBehaviour.

Also on that note, it is hard for me to grasp what the moveBehaviour does with the goals exactly, does it choose one and then use up that frame’s “time” to work towards it and check again next frame? or does it somehow work towards all goals but with “weighted” efforts. Maybe we could have the move behaviour check for goals such as seek and avoid less often than 60 times a second? :slight_smile: i think maybe 1 time every 1 second would suffice and give enough lag in the behaviour to make it more realistic even.

Also, is it using physics (i doubt it) to move the nodes? if not, can it be made to? (this is more in line of what I want to do next, but possibly a question many people might ponder). Surely there are optimisations to be made if you think about how complex games get even on iDevices.

Thanks
Arek

Hello! I’ve completed the basic tutorial and added the other entities, but I’m not sure where I should go from there. I understand I need to add more components, but does anyone have resources I could look at to develop the thought process behind them? I wouldn’t want to just copy the completed reference project. Thanks!

I copied the complete project and then analysed each file and the workings of the game. Helped me plenty…

Hi there , is there any chance that you guys make a course or a book around GamePlayKit , except Apple’s documentation I found really hard to find any content on the subject.

This tutorial is more than six months old so questions regarding it are no longer supported for the moment. We will update it as soon as possible. Thank you! :]