Scripting with Artemis Entity System Framework

Artemis is a component based entity system framework made in Java by Arni Arent (appel) and Tiago Costa (spiegel).

The core of Artemis is small and it is based on four main concepts. The Component, where all information about an Entity is stored. The Entity, which represents a game object. The EntitySystem, where custom logic, for entities with one or more component, is processed. And finally the World, where Entities and EntitySystems live. The idea is to grow over those concepts by making your own Components and EntitySystems.

The problem

There are some cases where it depends a lot how are you using Artemis, to feel it is helping you or not. For example, when you want an Entity to have some custom behavior on a specific level you could follow the next approach.

First, create an SpecificBehaviorForLevelComponent to declare that an Entity with that component will have that custom behavior and then create the SpecificBehaviorForLevelSystem which performs the logic.

The problem with this approach is that you will have an empty Component used only as a mark to let the EntitySystem work over it. Also, you will have an EntitySystem that will perform logic only in a specific moment of the game and then it will be useless.

If you have to create more than just one custom behavior, then you are a bit doomed and you will hate Artemis. However, it is not Artemis, or any component based entity framework, fault.

Script Framework, a possible solution

To simplify working with cases like the one mentioned before, we created a scripting framework over Artemis, composed by three main concepts: the Script, the ScriptComponent and the ScriptSystem.

Script

This is an interface which provides the following three methods:

  • init(world, entity) : called once when the entity enters the world.
  • update(world, entity) : called in each world update
  • dispose(world, entity) : called once when the entity leaves the world.

It should be implemented and added to the ScriptComponent of an Entity in order to perform the behavior.

ScriptComponent

This is an Artemis Component which contains a collection of Scripts and it is used by the ScriptSystem in order to know which Scripts to process on each Entity.

ScriptSystem

This is an Artemis EntitySystem responsible of calling, for each Entity, the init/update/dispose methods of each Script declared in the ScriptComponent of that Entity.

For the previous example, where we wanted a specific behavior for an entity in one level, we could create a SpecificBehaviorForLevelScript and, when loading that level, add that script to the ScriptComponent. So, instead creating Components and EntitySystems, we can create only Scripts.

Pros

  • Clear and simple API.
  • Easy to add/remove a Script vs add/remove a System.
  • Scripts are easily reusable (when you want two or more entities with a custom behavior).
  • Logic localized in one place.
  • Allow different implementations of the Script interface, for example, a Groovy implementation.

Cons

  • Not good for global stuff like rendering, an EntitySystem keeps being better for those cases.
  • Hard to know the right balance between which code should be on Scripts and which one on Systems.

Conclusion

The Script Framework is some point in the middle between Entities with no logic at all and Entities with all the logic. It should be used with care, learning to balance which code should be on Scripts and which code on Systems is not easy.

Artemis is only a small core, a foundation of classes to work with. The real power comes from the custom Components and Systems you create over it. For example, in our case we created several Systems that we reuse between games, some of them are the RenderSystem responsible of rendering the entities with the RenderComponent using libGDX and the PhysicsSystem responsible of processing the Box2d world.

References

There are a lot of resources about components based entity systems, however the best/correct solution between all these references is not so clear.

Hope you like it.

VN:F [1.9.22_1171]
Rating: 4.2/5 (5 votes cast)
Scripting with Artemis Entity System Framework, 4.2 out of 5 based on 5 ratings

Tags: , , ,

  • Bob Hansons

    Very interesting article!! 

    I found it after reading http://www.ziggysgames.com/artemis-and-gemserk-a-couple-of-new-finds and Im planning to play around with Artemis/libgdx/android.Can I find the code (especially the one that combines Artemis and LibGDX) on github?

  • http://twitter.com/rgarat Ruben Garat

    Hi, we are really glad you liked the article.

    You can find our our commons-gdx (artemis + libgdx) code in here https://github.com/gemserk/commons-gdx

    Just be aware that this code is what we are using in our games so if we see that we need to change it and can't keep backward compatibility we will still do the change.

    I think what ziggy does is maintaining a local fork, so that he can be more careful with the updates.

    If you have any problems with the code, or doubts don't hesitate to ask us.

  • Pingback: Area triggers using Box2D, Artemis and SVG paths « Gemserk

  • Eason Pai

    Damn!(sorry) You are the man! There are even Ads service supports and your awesome VirtualViewport in the commons-gdx.

  • relu

    Does groovy run also on android ? , great article btw , it was just what i looking for! :)

  • gemserk

    We never tested Groovy on Android but I suppose it depends a lot on how you use it.

    When we were using Groovy for desktop games it was a bit cpu heavy for some stuff and we started to work directly on Java to improve performance, for that reason we never tested it on Android. However that was long ago and we also never tried to optimize how we used it.