Tuesday, December 17, 2013

Entity Component System

After long time of no post, i decided (read: this is a school assignment ), to post something about this new phenomenon, that is trying to take over the game industry. It's called Entity Component System. In my eyes, it deserves all the hype around it. After playing around with it, i don't think i can ever go back to OOP approach, in game developing. So, let me tell you a bit about it, then i tell you what i did

Entity Component System


Entity Component System, ECS, is THE new way to create games. In ECS you separate data and functionality. Instead of using deep inheritance that object oriented programming, OOP, offers, you just composite new entities to your world. Entity is nothing but an ID and container for components. ECS allows you to create truly re-usable code.
With ECS, you just add functionality to entity. This means, that difference between player entity and AI entity is just an AIComponent. If entity has AI component, then the system will drive it towards it's goals, if its player component, then it uses input to drive the entity.

Entity


Entity in this system is just an ID to refer from code and container to hold components. The functionality of the entity is composed with components. In game perspective, everything is entities. Items, players, enemies, world, you name it.

Component


Components are data holders, data templates. Position component in 2D game, would have two integers, x and y. That's it. Component is data holder for all the data that is needed to produce the functionality of the component. In case of position component, that would be the location, a point in space. It's very important to understand, that components don't contain any information about implementation. It just keeps the data of this component for entity that it's attached to.

System


Systems build up the game engine. These provide the wanted functionality. Systems modify the data inside the components of entities. System can be some major part of the engine. There could be 2DRenderSystem, that looks for entities that has 2DSprite-component and position-component attached or there could be MovementSystem, that looks for entities that has position- and movement-components. Systems are only interested in those entities that has all the needed components for prosessing. 
System is the part of your code, that does the work. That's what makes things happen.

My testings


I have done some testings with this kind of system and it indeed looks good. I like the idea, that i can with one component, add the collision detection for certain item or that by setting Health-component to entity i can make it mortal. I don't have to write classes for all the different mobs and players nor i don't have to come up with some clever inheritance system.


world.RegisterRenderSystem(new TiledMapRenderSystem());
world.RegisterSystem(new BasicControllingSystem());
world.RegisterSystem(new TiledMapCollisionSystem());
world.RegisterSystem(new MovementSystem());
world.RegisterSystem(new ColliderUpdateSystem());
world.RegisterRenderSystem(new CollisionBoxRenderSystem());
world.RegisterSystem(new Camera2DSystem());  
world.RegisterRenderSystem(new Render2DSystem());


Entity mapEntity = world.CreateEntity();
mapEntity.AddComponent(new TiledMapComponent("Content/rpgtest.tmx"));

Entity player = world.CreateEntity();
player.AddComponent(new PositionComponent(0, 0));
player.AddComponent(new MovementComponent());
player.AddComponent(new SpriteComponent("Blue-Monster"));
player.AddComponent(new BasicControllerComponent());
player.AddComponent(new Camera2D(true, "", new Vector2(320, 320), new Rectangle(0, 0, 1600, 1600)));
player.AddComponent(new ColliderComponent());

That's example code from my test environment. It loads map, renders map and player entity. Allows movement, sprite, camera following, colliding with map..

Some links to check out!
http://www.gamedev.net/page/resources/_/technical/game-programming/understanding-component-entity-systems-r3013
http://en.wikipedia.org/wiki/Entity_component_system
http://gamadu.com/artemis/
https://github.com/thelinuxlich/artemis_CSharp

Tuesday, July 6, 2010

How to: Lidgren Network

Lidgren Networking
Homepage: LidgrenHome

Lidgren Network is quite easy to use networking library. It can be used for anykind of software.
I started to work with this library about month ago. Before that, i had never ever done anything that relates to networking.

Setting up chat server was quite easy. After that, i started to think about game server architechture, handling of multiple different kinds of packets, worldState handling, client updating, bandwidth usage.. To be honest, its big task, to build working multiplayer game server and client. You have to handle ever growing amount of different kind of packets. Keep updating clients, with data they need etc. It took me a while to understand all this.
I thought first, that internet must be full of example code, but no.. it's not. Atleast i did not find any. Or if i did, it was so professional code, that i could not even read it.

Things are about to change :)

I created small console based "game", that works over internet, but you can test it on your localhost. When it ask's for IP, just type "localhost". It's very simple example and i tryed to comment it, but feel free to ask help, if something is confusing.
This example covers the very basics of game networking. Message handling, movement, server world state handling etc. Hope you learn something from it.
Here it is: Game Network Example

Monday, July 5, 2010

How to: Mercury Particle Engine

I was looking for good, fast and reliable particle engine, for XNA and after a while, i decided that i will use Mercury Particle Engine. It looks fantastic and its fast and it comes with graphical particle effect editor. How cool is that? Theres really no limit, on what kinds of effect you could create.

Mercury project page can be found from here!

( Source of this tutorial and compiled exe )
Download: MercuryExample.Zip


Only thing that bothers me, is the lack of tutorials for it. There really isnt anything, that helps "newbie" coders to get those beatifull particles from editor, to XNA. Well, i made a short example project, that loads and draws the "BasicExplosion", that comes as an example with the editor.

Its quite easy to get particles to drawn on screen. You need to add that particle xml file and all the textures it needs, to content of your XNA project. After that, its just coding.

// Add MercuryProject.dll reference to your project
// Add using statements
using ProjectMercury;
using ProjectMercury.Emitters;
using ProjectMercury.Modifiers;
using ProjectMercury.Renderers;
Inside the Game class, add:

 // Renderer that draws particles to screen
 Renderer myRenderer;
 // Particle effect object to store the info about particle
 ParticleEffect myEffect;
 Inside the Game constructor add:

 // Create new renderer and set its graphics devide to "this" device
 myRenderer = new SpriteBatchRenderer
 {
         GraphicsDeviceService = graphics
  };

 myEffect = new ParticleEffect();
Inside LoadContent add:
  myEffect = Content.Load("BasicExplosion");
  myEffect.LoadContent(Content);
  myEffect.Initialise();
  myRenderer.LoadContent(Content);

Inside Update add:
  // get the latest mouse state
  MouseState ms = Mouse.GetState();
  // Check if mouse left button was presed
  if (ms.LeftButton == ButtonState.Pressed)
 {
         // Add new particle effect to mouse coordinates
         myEffect.Trigger(new Vector2(ms.X, ms.Y));
  }
  // "Deltatime" ie, time since last update call
  float SecondsPassed = (float)gameTime.ElapsedGameTime.TotalSeconds;
  myEffect.Update(SecondsPassed);
And finally, add to Draw:
 myRenderer.RenderEffect(myEffect);


Now, compile.
Remember to add the dll to Content references too, or else, it wont know how to import those files. Now, if its running, just click with mouse and enjoy.

Saturday, July 3, 2010

How to: Lua + XNA

Binding Lua to XNA, may sound hard, but its not. However, you can't easily create fully scripted game logic. I guess the reason is the fact, how XNA works. Internals of XNA are calling, update(), loadContent(), Draw() etc and if you dont let it call ( ie. stay in while(scriptrunning) or something ), it messes things up.

Lua can still be used as scripting language, but its only used to load, save and such. You cant just run some script, you have to create it event based. That means, that you check in update() loop, if something needs to be done, then do it and let the XNA do the magic again, until next update() comes.


In order to get Lua working, in your XNA app, you have to do few easy steps.
Step1: Download LuaInterface
Step2: Set it as reference dll in your current project
Step3: Add some code:
Lua LuaVM = new Lua();  // Create new Lua instance// Register Function params 1= name you use IN lua, to call this method
// 2 = object target, the object, whos function you are registering
// 3 = Method you want to call LuaVM.RegisterFunction("luaName",this,GetType().GetMethod("MyFunction"));
LuaVM.DoString("LuaName()"); // Executes Lua code. This example calls MyFunction()
So, this is all you need, to get started with Lua in your own projects. What ever is your method, to get the command input from user, its fine, as long it can be changed to string and passed to DoString(); Notice thou, that when registering method, you must call it in Lua as method, so "LuaName" turns to "LuaName()". You can use parameters in there and do what ever you want. 

Other important funtion in Lua is "DoFile()". You pass a file path/name and it executes it as a Lua code. With this, you could ( for example ) load starting settings for game levels, or different kind of characters or something. I use Lua in my current project as a quake style console. I can set engine variables and call engine funtions while game is running, witch makes testing phase a lot easyer.
LuaVM.DoFile(string path); // Execute file, full of Lua stuff
You can also set references of objects to Lua. You can do it like this:
LuaVM["MyLuaObject"] = MyCsharpObject;
LuaVM.DoString("MyLuaObject.Property = 100"); // This changes MyCsharpObject Varible named Property to 100;
If that object has methods inside it, you can call em easily from Lua
LuaVM.DoString("MyLuaObject.Method()");
Simple, eh?
Thats all you need to know, in order to get started with Lua. I might add some working example project, if its needed.
-R

Friday, July 2, 2010

Hello World!

Hi,
My name is Riku Koskinen and i have coded for fun, for few years. My main language is C# and for games i add XNA to it. I love to code. Im not much into game logic coding, but creating "engines" and such, is what i like. Im not expert, in any way, but i know my way around.

Im 27 years old, im studying Business Information Technology at HAMK University of Applied Sciences, in Finland. Im not expert at english language either, but i hope you understand, what im trying to say.

I certainly hope, that i can provide some usefull info, that might help someone, when troubles come.

First real blog entry will come.. soon.

-R